SpringMVC 使用注解配置基本使用(2)

主要是针对配置(1)进行优化:
SpringMVC xlm可左如下配置,从Spring3.0开始,可以用<mvc:annotation-driven/>标签,代替AnnotationHandlerMapping和MethodHandlerAdapter两个bean,Spring3.0后对其进行了封装,自动注入,省得麻烦每次自己手写一遍,因为这两个类每次都需要用到,一个是用来映射请求对应的Controller,一个是映射Controller里对应的方法。
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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="test"></context:component-scan>
<!-- spring3.0开始,可用以下方式直接替代下面两个bean,因为每次都需要用到HandlderMapping和MethodHandlerAdapter这两个类,spring干脆把它们封装起来-->
<mvc:annotation-driven/>
<!-- handleMapping类,用于对发来的请求映射到对应的Controller -->
<!-- <bean id="handleMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
方法映射适配器,用于映射请求对应的方法
<bean id="methodMapping" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->
<!-- 对静态资源进行过滤 -->
<mvc:resources location="/imp/" mapping="/imp/**"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

以下是对Controller注解进行优化,通过在RequestMapping加上“/User”,则可以统一为下面方法加上一个识别根目录,用以区分处理不同业务逻辑的相同方法,不需要再想以前那样,在方法上面一个个加上"/User/addUser"这样;
package test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

@Controller
@RequestMapping(value="/User")
public class MyAnnotationController {
//假如 RequestMapping不写value,默认输入的是value值,
@RequestMapping("/addUser")
// 注解方式不需要输入HttpServletRequest和HttpServletResponse这两个参数
public String addUser() {
System.out.println("addUser--------");
String result = "addUser";
// 跳转到annotation.jsp
return "annotation";
}

/*
* @RequestMapping(value="/User/addUser",method=RequestMethod.GET)
* //注解方式不需要输入HttpServletRequest和HttpServletResponse这两个参数 public
* ModelAndView addUser(){ System.out.println("addUser--------"); String
* result = "addUser"; //跳转到annotation.jsp return new
* ModelAndView("annotation","result",result); }
*/

}

如果需要再JSP页面输出result的值,则可以在方法传入HttpServletRequest实例,用我们最原始的方式,放到request域中
package test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

@Controller
@RequestMapping(value="/User")
public class MyAnnotationController {
//假如 RequestMapping不写value,默认输入的是value值,
@RequestMapping("/addUser")
// 注解方式不需要输入HttpServletRequest和HttpServletResponse这两个参数
public String addUser(HttpServletRequest request) {
System.out.println("addUser--------");
String result = "addUser";
request.setAttribute("result", result);
// 跳转到annotation.jsp
return "annotation";
}

/*
* @RequestMapping(value="/User/addUser",method=RequestMethod.GET)
* //注解方式不需要输入HttpServletRequest和HttpServletResponse这两个参数 public
* ModelAndView addUser(){ System.out.println("addUser--------"); String
* result = "addUser"; //跳转到annotation.jsp return new
* ModelAndView("annotation","result",result); }
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值