Spring 3 Demo with annotation-driven
Create web dynamic java project with "Spring3Demo"WEB-INF\web.xml
=============================================================
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Hello Program</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/hello-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
WEB-INF\hello-servlet.xml
=============================================================
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.xyz.controller" />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
com\xyz\controller\HelloController.java
=============================================================
package com.xyz.controller;
import java.util.Date;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xyz.bo.Customer;
@Controller
@RequestMapping("hello")
public class HelloController {
@RequestMapping(value ="/welcome", method = RequestMethod.GET)
public String welcome(Model model) {
Date date = new Date();
System.out.println("here in welcome");
model.addAttribute("dateVisited", date);
// takes the default return view as hello
return "hello";
}
@RequestMapping(value = "/customerform", method = RequestMethod.GET)
public String customerForm(Map model) {
Date date = new Date();
System.out.println("here in welcome");
Customer customer = new Customer();
model.put("customer", customer);
// takes the default return view as hello
return "custform";
}
@RequestMapping(value="/customerProcess", method = RequestMethod.POST)
public String addCustomer(@Valid Customer customer, BindingResult result, Map model) {
customer = (Customer) model.get("customer");
if (result.hasErrors()) {
System.out.println("hasErrors:::::::::::hasErrors");
return "custform";
} else {
model.put("customer", customer);
System.out.println("customersuccess:::::::::::customersuccess");
return "customersuccess";
}
}
}
com\xyz\bo\Customer.java
=============================================================
package com.xyz.bo;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
public class Customer {
@NotEmpty //make sure name is not empty
@Size(max=16)
String customerName;
@NotEmpty
String location;
@Range(min = 1, max = 150) //age need between 1 and 150
int customerCode;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getCustomerCode() {
return customerCode;
}
public void setCustomerCode(int customerCode) {
this.customerCode = customerCode;
}
}
index.jsp
=============================================================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Welcome To Spring3</h2>
<BR/>
<a href="/Spring3Demo/hello/welcome.htm">Click welcome</a><br/>
<a href="/Spring3Demo/hello/customerform.htm">Customer form</a>
</body>
</html>
jsp\custform.jsp
=============================================================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring"
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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<BR/>
<a href="/Spring3Demo/hello/welcome.htm">Click welcome</a><br/><a href="/Spring3Demo/hello/customerform.htm">Customer form</a>
<BR/>
<spring:form action="customerProcess.htm" method="post" commandName="customer">
<table>
<tr>
<td>Enter Customer Name :</td>
<td><spring:input path="customerName" /><spring:errors
path="customerName" /></td>
</tr>
<tr>
<td>Enter Location :</td>
<td><spring:input path="location" /><spring:errors
path="location" /></td>
</tr>
<tr>
<td>Enter Location :</td>
<td><spring:input path="customerCode" /><spring:errors
path="customerCode" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</spring:form>
</body>
</html>
jsp\hello.jsp
=============================================================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome to Spring MVC training on ${dateVisited}</h1>
<BR/>
<a href="/Spring3Demo/hello/welcome.htm">Click welcome</a><br/>
<a href="/Spring3Demo/hello/customerform.htm">Customer form</a>
</body>
</html>
jsp\customersuccess.jsp
=============================================================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Customer Success!!!!!
<BR/>
<a href="/Spring3Demo/hello/welcome.htm">Click welcome</a><br/>
<a href="/Spring3Demo/hello/customerform.htm">Customer form</a>
</body>
</html>
src\messages.properties
=============================================================
NotEmpty.cusromer.customerName=Customer name is required
Size.cusromer.customerName=Customer name max size is 16
Size.cusromer.location=Customer location is required
Range.Size.cusromer.customerCode= Customer code must between 1 to 150
No comments:
Post a Comment