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
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值