Softwares Used
1. Eclipse Juno
2. Tomcat 7
3. Spring 3.0.3
Example
Create one Web application and follow below steps
1. Create a class to define form attributes
package com.pretech.form;public class SimpleForm {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}private String address;}
2.Create a Controller class
package com.pretech;import java.util.Map;import javax.validation.Valid;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.pretech.form.SimpleForm;@Controller@RequestMapping("simpleform.html")public class SimpleFormController {@RequestMapping(method = RequestMethod.GET)public String showForm(Map<String, SimpleForm> model) {SimpleForm simpleForm = new SimpleForm();model.put("simpleForm", simpleForm);return "simpleform";}@RequestMapping(method = RequestMethod.POST)public String processForm(@Valid SimpleForm simpleForm, BindingResult result,Map model) {if (result.hasErrors()) {return "simpleform";}simpleForm = (SimpleForm) model.get("simpleForm");model.put("simpleForm", simpleForm);return "simpleform";}}
2.Create a JSP Page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><%@ 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><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Spring mvc simple form example</title></head><body><h3>Simple Form</h3><h3>Welcome ${simpleForm.name} </h3><form:form action="simpleform.html" commandName="simpleForm"><table><tr><td>Enter your Name:</td></tr><tr><td><form:input path="name" /> <FONT color="red"><form:errors path="name" /></FONT></td></tr><tr><td>Enter your address:<FONT color="red"><form:errors path="address" /></FONT></td></tr><tr><td><form:input path="address" /></td></tr><tr><td><input type="submit" value="Submit" /></td></tr></table></form:form></body></html>
4. Create spring config file (dispatcher-servlet.xml)
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><mvc:annotation-driven /><context:component-scan base-package="com.pretech" /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/" /><property name="suffix" value=".jsp" /></bean><bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><property name="basename" value="/WEB-INF/messages" /></bean><!-- Configure the multipart resolver --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean></beans>
5. Add Spring Dispatcher servlet details in to web.xml
<servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/dispatcher-servlet.xml</param-value></context-param>
5. Run the application on Tomcat Server
http://localhost:8080/Spring3SimpleFormExample/simpleform.html
No comments:
Post a Comment