想搞一个趁手的快速开发框架,maven2,spring2.5,hibernate3.3肯定是少不了的。展现层在Struts2和Spring MVC间犹豫了好久,也看了javaeye上对2个框架的很多讨论。spring2.5 mvc号称是基于annotation的,controller可以是pojo,而且可以极大的减少配置。这可是挺吸引人的功能,于是花点时间学习下,顺便记下自己的学习过程。
web.xml配置就是标准的spring配置:
<?xml version="1.0" encoding="UTF-8"?> <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>test</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- Character Encoding filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Spring ApplicationContext 载入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>simple</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>simple</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
applicationContext.xml 只是空的schema定义:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> </beans>
重点是simple-servlet.xml,这个文件名需要和web.xml中定义的name匹配
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- - The controllers are autodetected POJOs labeled with the @Controller annotation. --> <context:component-scan base-package="test"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> </beans>
为了简单,我只做了test包,所以base-package="test"。
我做了2个页面,一个MultiController,这个对应springmvc早期版本的MultiActionController,就是多个url映射到一个controller中.代码简化了很多:
package test; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MultiController { @RequestMapping("/hello.html") public String sayHello(Model model) { model.addAttribute("say", "hello"); return "test"; } @RequestMapping("/nihao.html") public String sayHello2(Model model) { model.addAttribute("say", "nihao"); return "test"; } }
通过RequestMapping 注解,可以方便的匹配url,再也不需要xml和java两边修改了。
还有一个PersonForm,就是通常的对象的编辑
package test; import javax.servlet.http.HttpServletRequest; 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 org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.support.SessionStatus; @Controller @RequestMapping("/person.html") public class PersonForm { private static Person person = new Person(); @RequestMapping(method = RequestMethod.GET) public String setupForm(@RequestParam("id") long id, ModelMap model, HttpServletRequest request) { String s = request.getParameter("id"); System.out.println("getId " + s); model.addAttribute("person", getPerson(id)); return "personForm"; } @RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("person") Person person, BindingResult result, SessionStatus status) { if (result.hasErrors()) { return "personForm"; } else { PersonForm.person = person; System.out.println("person name set to:" + person.getName()); status.setComplete(); return "redirect:person.html?id=" + person.getId(); } } private Person getPerson(long id) { return person; } }
setupForm方法名无所谓,关键是@RequestMapping注解,String s = request.getParameter("id");其实是多余的,我是为了证实可以在方法中加入多种参数,具体参看spring2.5 reference 13.11.4. Supported handler method arguments and return types
附件是源码,使用的maven,只要下载下来,解压到任意目录,cmd进入目录,mvn jetty:run就可以运行了, http://localhost:8080/ 即可访问。
代码中加了一个RunJetty类,可以方便的在IDE中直接运行Jetty,相关最小依赖包已经在pom.xml中定义,jetty-config.xml是它的简单配置,可以修改端口号和app,目前是8080和src/main/webapp。 cmd中运行mvn eclipse:eclipse就可以生成项目,然后eclipse import项目,就可以直接在RunJetty类上 run和debug,访问路径和使用mvn jetty:run相同。