(Updated for Spring 3.0)
I am coming with Spring MVC .
You need to download Spring from here
To configure your web application to use Spring, add the following servlet to your web.xml
<web-app> <servlet> <servlet-name>spring-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
Then you need to create the Spring configuration file /WEB-INF/spring-dispatcher-servlet.xml
Your first version of this file may be simple:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.acme.foo" /> <mvc:annotation-driven /> </beans>
Spring will automatically detect classes annotated with @Controller
Simple controller:
package com.acme.foo; import java.util.logging.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/person") public class PersonController { Logger logger = Logger.getAnonymousLogger(); @RequestMapping(method = RequestMethod.GET) public String setupForm(ModelMap model) { model.addAttribute("person", new Person()); return "details.jsp"; } @RequestMapping(method = RequestMethod.POST) public String processForm(@ModelAttribute("person") Person person) { logger.info(person.getId()); logger.info(person.getName()); logger.info(person.getSurname()); return "success.jsp"; } }
And details.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <form:form commandName="person"> <table> <tr> <td>Id:</td> <td><form:input path="id" /></td> </tr> <tr> <td>Name:</td> <td><form:input path="name" /></td> </tr> <tr> <td>Surname:</td> <td><form:input path="surname" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Save Changes" /></td> </tr> </table> </form:form>
This is just the tip of the iceberg as to what Spring can do ...
Hope this helps.
toolkit Sep 22 '08 at 20:47 2008-09-22 20:47
source share