Spring2.5 MVC annoation使用



Spring2.5 MVC annoation使用。Spring2.5比Spring2.0改善最大的亮点就在于annoation应用于MVC部分,大大减少了配置文件,刚刚完成一个小例子,用起来蛮舒服的。

运行环境 :JDK1.5  TOMCAT5.5

 

先来看看配置文件

1.applicationContext.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: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“>     <annotation-config /><!–annoation的配置,不写包名默认搜索全部–> </beans>

2.web.xml ,这个文件看起来似乎没有多大的变化,不过要注意模板~~

 

<?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>springapp</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.CharacterEnco                                  dingFilter</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>*.do</url-pattern>     </servlet-mapping> </web-app>

 

3.springapp-servlet.xml ,这个配置文件是不是看起来清爽了很多,不像2.0那么复杂了。

 

<?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.     –>     <component-scan base-package=”test”/> <!–指定解析该包名下的controller–>

<bean                 p:prefix=”/WEB-INF/jsp/” p:suffix=”.jsp” /> </beans>

 

4.下边看看使用annoation风格的Controller: 这个相当于原来的simpleFormController

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.do”) 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(“getIdddddd ” + 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.do?id=” + person.getId();   } }

private Person getPerson(long id) {   return person; }

}

5.在来看个muliController的例子

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.do”) 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”;//直接返回view层名称 –>personForm.jsp }

@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.do?id=” + person.getId();   } }

private Person getPerson(long id) {   return person; }

}

6.在看看view层:

personForm.jsp

test.jsp

7.所需要加载的jar包

spring.jar    spring-agent.jar  spring-aop.jar spring-aspects.jar spring-beans.jar spring-context.jar

spring-context-support.jar    spring-core.jar  spring-web.jar  spring-webmvc.jar

8.如果页面使用了jstl,还需要使用两个额外的jar

jstl.jar   standard.jar

http://ioio.javaeye.com/blog/344919