Skip to main content

Posts

Showing posts with the label Spring MVC

SpringBoot : What are Profiles in Spring Boot Application

What are Profiles in Spring Boot Profiles can be seen as different environments in spring boot application. Suppose you are working on an application where you have different staging environments like Dev, QA, UAT, Production etc. So you will have different configurations for each environment, for this kind of applications what you need is having different values that can be switched depending upon some flags. In early days, people used to write configurations for all environments and comment the unused environments. But with Spring Boot we can do this without commenting any code by use of Profiles . Suppose we have database name configurations for our application as follows Dev     - ekiras_dev QA    - ekiras_qa UAT  - ekiras_uat Prod  - ekiras Now, we can use profiling in this case. How we will do it ? lets see Creating Profiles in Spring Boot  We will create different files for different environments and call then as profile in rest of the blog...

SpringBoot : How to allow cross origin ajax calls to Spring application

How to allow cross origin ajax calls to Spring application Add the following class to your Spring or Spring Boot application. After adding this class you will be able to make ajax calls to your application. package com.ekiras.filter; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class CorsFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with"...

Spring Boot Maven Dependencies to create basic CRUD application

Spring Boot Maven to create basic CRUD application. You need to add the following dependencies to the pom.xml to make the basic CRUD application spring-boot-starter-data-jpa spring-boot-starter-jdbc spring-boot-starter-web jstl mysql-connector-java spring-boot-starter-tomcat tomcat-embed-jasper So your pom.xml may look like the following pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ekiras</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springboot</name> <description>Basic Springboot CRUD applicat...

Spring Boot Gradle MVC sample CRUD project

This is a sample Spring Boot Application that uses  JDBC Mysql JPA MVC Gradle Create Basic CRUD for Person Entity with JPA and Mysql Download from GitHub Project Structure  build.gradle View Maven Dependencies (pom.xml) here. buildscript { ext { springBootVersion = '1.2.5.RELEASE' } repositories { maven { url "http://repo.spring.io/libs-milestone" } mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath("io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'war' war { baseName = 'springboot' version = '0.0.1-SNAPSHOT' } sourceCompatibility = 1.7 targetCompatibility = 1.7 rep...

Spring MVC handle Exceptions in Controller using @ExceptionHandler

Points To Remember You can handle all types of runtime exceptions and custom exceptions using  @ExceptionHandler  annotation.  You just need to define the class to the annotation to handle exceptions of that class. Spring MVC handle Exceptions in Controller Here we have a simple Spring MVC application with the following. HomeController wih three actions  throw1 - throws Custom exception throw2 - throws Custom exception throw3 - throws Runtime exception Home Controller package com.ekiras.controller; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; im...

Pagination in Spring Hibernate Mvc application

Points To Remember Create a Pagination Taglib using this example . Add Bootstrap css and js in the jsp you want to do pagination and include the taglib in the jsp. Pagination in Spring Hibernate Application using Bootstrap Create a Domain Person. Create a Service PersonService Create a Dao PersonDao . Create a Controller PersonController . Create a Taglib PaginationTaglib. Person.java package com.ekiras.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="person") public class Person { public Person(){}; public Person(String name,Integer age){ this.name = name; this.age = age; } @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private Long id; @Column(name="name") private String name; @Column(name="age") private Integer age; ...

Maven Spring MVC Hibernate Sitemesh Project Setup

Points To Remember You need to add the following dependencies to pom.xml spring-core spring-context spring-web spring-webmvc spring-tx spring-orm mysql-connector-java jstl hibernate  Create  Maven Spring Hibernate Sitemesh Annotation based Hello World project pom.xml pom.xml should contain the following dependencies <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework.samples.service.service</groupId> <artifactId>ekiras</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.7</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncodi...

Custom 404 Error Page in Spring MVC

Points To Remember You need to first add the errors in the web.xml  and redirect them to a url. Then you need to map these url to controller to handle them and take necessary actions.  You can either handle exceptions in your controllers as explained in the link. Custom Error Pages Here in this example we are going to create custom error pages in our spring mvc application. So will first of all configure the error codes in web.xml like the following. web.xml <error-page> <error-code>400</error-code> <location>/400</location> </error-page> <error-page> <error-code>404</error-code> <location>/404</location> </error-page> <error-page> <error-code>500</error-code> <location>/500.jsp</location> </error-page> Now when we have configured the error codes and mapped then with the respective url's, we will now create a Controller that will map these error cod...