spring 2.5 mvc annotation使用

项目中一种在用spring2.0,虽然2.5出来了很多新特性,但是一直没有时间实现,最近抽空做了一次尝试。spring mvc在系统中使用最多的就是Controller、MultiActionController和自定义的View了,Controller annotation的配置很简单,如下:

 

Service的配置

@Component
public class FooServiceImpl implements FooService {
     //服务实现
}

Controller的配置

@Controller
@RequestMapping("/findStudent.do")
public class FindStudent {

private FooService fooService;

	@Autowired
	public FindStudent(FooService fooService) {
	                this.fooService = fooService;
	}

	@RequestMapping(type = "GET")
	public String doGet(ModelMap model) {
		//do Get
	}

                @RequestMapping(type = "POST")
	public  String doPost(@ModelAttribute("student") Student student, BindingResult result, ModelMap model) {
		//do Post
	}
	
	@ModelAttribute("students")
	public Collection<Student> allStudent(){
		//返回collection
	}

}

 

Controller的方法可以返回4种类型void,String,ModelMap,ModelAndView,其中String是ViewName,ModelMap是一个LinkHashMap,在Spring处理annotation的时候会利用ModelMap来构建一个ModelAndView往上返回。单一的Controller主要是用于Form Controller的,其它的一些使用和以前的功能一样,这里说下@ModelAttribute,它可以把标记返回的数据存储到以指定value为attributeName的request中,这样可以在页面里面使用<form:form modelAttribute="student">中快速获取值,不过我不建议在视图层使用spring tag,这个看个人情况了 ^ ^

 

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">

<context:component-scan base-package="blog.andyj.spring.annotation" />

</beans>

 

context:component-scan还包括一些更详细的配置,比如过滤包等等,这里就不详细描述了。以上是Controller的配置,这样系统中的xml文件是不是干净很多了,呵呵!其实我本人还是比较多的使用MultiActionController的,下面说下它的实现:

 

MultiActionController配置

 

@Controller
public class TestController {
	
	@Autowired
	private TestService testService;
	
	@RequestMapping("/testHello.do")	
	public String hello(@RequestParam(value="name",required=true)String name,ModelMap model){
		model.addAttribute("message",name);
		return "forward:hello.jsp";
	}
	
	@RequestMapping("/testView.do")
	public String go(){
		return "testView";
	}

	public TestService getTestService() {
		return testService;
	}

	public void setTestService(TestService testService) {
		this.testService = testService;
	}

}

多数的配置都是一样的,@RequestParam有两个值,value是从request来的parameterName这样我们就不用写request.getParameter("name"),直接在controller使用我们的变量name就可以了;required默认为false,允许request中没有name参数,如果改为true,当request.getParameter("name")为空的时候会抛出org.springframework.web.bind.MissingServletRequestParameterException异常,我们可以用exceptionViewResolver接住异常转到相应视图上。还有关于自定义的View实现也是和以前的实现一样
,在上面go()方法中我返回了"testView"的viewName,您只要像以前一样在xml文件里定义好View和ViewResolver就可以了。

 

最后,这里只是涉及了spring2.5中相关内容中非常少的一点,如果大家有兴趣深入了解可以参考spring reference。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值