Following spring boot web applications tutorial will show you how to create a spring based web application using spring boot.

Spring boot web application configuration

package com.candidjava.spring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource("classpath:app-config.xml") public class SpringBootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } }

Spring boot dispatcher servlet XML configuration

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="com.candidjava.spring.controller"></context:component-scan> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:resources location="/resources/" mapping="/resources/**"/> <mvc:annotation-driven></mvc:annotation-driven> </beans>

Spring boot maven dependency for spring web application pom.xml

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.candidjava.spring</groupId> <artifactId>Spring-Boot-Xml</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Spring-Boot-Xml Maven Webapp</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- web starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- JSTL for JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- Need this to compile JSP --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

Spring controller

package com.candidjava.spring.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; import com.candidjava.spring.entity.User; @Controller public class UserController { @GetMapping("/") public ModelAndView register(User user) { return new ModelAndView("register"); } @PostMapping("/register") public ModelAndView create(@ModelAttribute("user") User user) { ModelAndView model = new ModelAndView("view"); // user bean will be automatically binded to view refer @ModelAttribute System.out.println(user.getName()); model.addObject("name",user.getName()); model.addObject("gender",user.getGender()); model.addObject("email", user.getEmail()); model.addObject("country", user.getCountry()); model.addObject("password", user.getPassword()); return model; } @GetMapping("/register") public ModelAndView viewData(User user) { ModelAndView model = new ModelAndView("register"); return model; } }

Bean Class

package com.candidjava.spring.entity; import java.util.List; public class User { private String name; private List<String> language; private String country; private String gender; private String password; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getLanguage() { return language; } public void setLanguage(List<String> language) { this.language = language; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }

JSP — register.jsp

<%@page import="org.springframework.validation.ObjectError"%> <%@ page import="com.candidjava.spring.entity.User" %> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> .err { color:red; } </style> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>Simple spring form handling</h2> <form action="register" method="POST" modelAttribute="user"> Name:<input type="text"name="name"/><br> Gender : Male<input type="radio" name="gender" value="male" path="gender"/> Female<input type="radio" name="gender" value="female" path="gender"/><br> Email : <input type="text" name="email" path="email"/><br> Country : <select name="country" path="country"> <option value="">select country</option> <option value="india">INDIA</option> <option value="usa">USA</option> <option value="china">CHINA</option> </select> <br> Password:<input type="password" name="password" path="password"/><br> <input type="submit" value="submit" /> </form> </body> </html>

JSP — view.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.candidjava.spring.entity.User" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>User Info</h2> <strong>User Name </strong>: ${name} <br> <strong>Gender </strong> : ${gender}<br> <strong>Email </strong> : ${email}<br> <strong>Country </strong>: ${country} <br> <strong>Password</strong>: ${password}<br> </body> </html>

Screenshot

Download

Spring boot XML @ImportResources – Download

