Springbootand MVC
Springbootand MVC
Front End-Internationalization
home.jsp
<body style="background-color:lavender">
<table>
<tr><td id="id1"><spring:message code="label.custName"/></td><td><form:input
name="customerName" path="customerName"/></td></tr>
<tr><td id="id2"><spring:message code="label.emailId"/></td><td><form:input
name="emailid" path="emailid"/></td></tr>
<tr><td id="id3"><spring:message code="label.address"/></td><td><form:input
name="address" path="address"/></td></tr>
<tr><spring:message code="label.submit" var="valSubmit"/>
<td><input type="submit" value="${valSubmit}" name="submit"/></td>
<td><input type="reset" value="Cancel"/></td>
</tr>
</table>
</form:form>
</body>
</html>
-------------
InternationalizationConfig.java
package controller;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class InternationalizationConfig extends WebMvcConfigurerAdapter {
}
}
-----------
InternationalizationApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.*,controller,model")
public class InternationalizationApplication {
---------------------------------------------
end------------------------------------------------------------------
2. ZeeZeeLogin
LoginController.java
package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;
import com.bean.LoginBean;
import com.service.LoginService;
@Controller
public class LoginController {
@Autowired
private LoginService service;
if (result.hasErrors()) {
return new ModelAndView("login");
}
if (!isValidUser) {
return new ModelAndView("invalid");
}
return new
ModelAndView("success","userName",loginBean.getUserName());
}
}
-----------
LoginAppApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = { "com.controller",
"com.service","com.bean" })
public class LoginAppApplication {
package com.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bean.LoginBean;
@Service
public class LoginService {
public boolean validate(LoginBean bean) {
return (bean.getUserName().equals(bean.getPassword()));
}
}
------------
invalid.jsp
<html>
<body bgcolor="lavender">
<h2>Sorry!! Invalid Credentials. Try again</h2>
</body>
</html>
---------
login.jsp
---------
success.jsp
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
---------
Application.properties
#Do Not change the key. Value that is port number can be changed
#include other necessary configurations
server.port=8085
spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**
--------------------------------------
end---------------------------------------------------------------
pom.xml
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
---------------
TicketController.java
package com.controller;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.service.TicketService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.model.Ticket;
@Controller
public class TicketController {
@Autowired
private TicketService ticketService;
@RequestMapping(value="/showpage",method=RequestMethod.GET)
public String showPage(@ModelAttribute("ticket") Ticket ticket){
ticket=new Ticket();
return "showpage";
}
@RequestMapping(value="/calculateCost",method=RequestMethod.POST)
public String calculateTotalCost(@ModelAttribute("ticket") Ticket
ticket,ModelMap model,BindingResult result) {
double ans=ticketService.calculateTotalCost(ticket);
model.put("cost", ans);
return "result";
}
@ModelAttribute("circleList")
public Map<String,String> buildState(){
Map<String, String> map=new HashMap<String, String>();
map.put("Queen", "Queen");
map.put("King", "King");
return map;
}
----------------
MovieTicketBookingApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"com.controller","com.model","com.service"})
public class MovieTicketBookingApplication {
}
------------
TicketService.java
package com.service;
import org.springframework.stereotype.Service;
import com.model.Ticket;;
@Service
public class TicketService {
}
---------
Application.properties
#Do not change the key server.port
#Its value port number can be changed
#Include other required configurations here
server.port=9066
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
--------------
result.jsp
<table>
<tr>
<td><sf:label path="movieName">Movie Name</sf:label></td>
<td><sf:input path="movieName" id="movieName" /></td>
</tr>
<tr>
<td><sf:label path="circleType">Choose
Circle:</sf:label></td>
<td><sf:select path="circleType" id="circleType"
items="${circleList}" /></td>
</tr>
<tr>
<td><sf:label path="noOfTickets">Number of
Tickets</sf:label></td>
<td><sf:input path="noOfTickets" id="noOfTickets" /></td>
</tr>
<tr>
<td><input type="submit" value="CalculateCost" id="submit"
name="submit"></td>
</tr>
</table>
</form:form>
</body>
</html>
---------------------------end-----------------------------------------------------
4.
pom.xml
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-------
RegistrationController.java
package com.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.model.RegistrationBean;
import com.validate.CustomValidator;
@Controller
public class RegistrationController {
@Autowired
private CustomValidator custValidator;
@RequestMapping(value="/registerPage",method=RequestMethod.GET)
public String registerPage(@ModelAttribute("register") RegistrationBean
registrationBean){
registrationBean= new RegistrationBean();
return "registrationpage";
}
@RequestMapping(value="/register", method=RequestMethod.POST)
public String performRegistration(@ModelAttribute("register") @Valid
RegistrationBean registrationBean,BindingResult result) {
custValidator.validate(registrationBean , result);
if(result.hasErrors()){
return "registrationpage";
}
else{
//model.addAttribute("userName", registrationBean.getUserName());
//model.addAttribute("emailId", registrationBean.getEmailId());
return "thankyou";
}
}
}
---------
HolidayPartyNewApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.*")
public class HolidayPartyNewApplication {
---------
RegistrationBean.java
package com.model;
import com.model.RegistrationBean;
@Service
public class CustomValidator implements Validator{
if(registrationBean.getUserName().length()<=0){
arg1.rejectValue("userName", "", "User Name cannot be blank");
}
boolean status1=true;
boolean status2=true;
------------
Application.properties
------------
registrationpage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<body style="background-color:lavender">
</body>
</html>
----------------------
thankyou.jsp