Spring2.5注释驱动与基于注释的MVC
百度广告
考试吧Java站整理收集
Spring2.5
package com.tony.test;
private String name;
return"Foo Name is :" + this.name;
Setget
2 Bar.java BarStringadd.
publicclass Bar {
public String toStirng(){
}
}
package com.tony.test;
private Foo foo;
public String toString(){
}
}
5 Test.java TestSpringmain
import org.springframework.context.ApplicationContext;
ClassPathXmlApplicationContext;
publicstaticvoid main(String args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
system.out.println(main);
}
Main : [Foo Name is :Foo Bar Add is :Bar]
import org.springframework.beans.factory.annotation.Autowired;
@Autowired
@Autowired
public String toString(){
}
Spring
||| 2)Spring @Qualifier("foo1")
@Autowired
private Foo foo;
private Bar bar;
return"Main : [" + this.foo.toStirng() +" "+ this.bar.toStirng() + "]";
}
Test.java:
Springfoo1main
publicclass Foo {
public String toStirng(){
}
@Component,Bean
@Component("main")
@Autowired
@Autowired
……
8.4.2 Spring2.5Spring 2.5也为 Spring MVC引入了注释驱动功能。现在我们无须让Controller继承任何接口,无需在XML配置文件中定义请求和Controller的映射关系,仅仅使用注释就可以让一个POJO具有Controller的绝大部分功能 -- Spring MVC框架的易用性得到了进一步的增强。
由于Spring MVC的Controller必须事先是一个Bean,所以 @Controller注解是不可缺少的。请看下面的代码清单
@Controller //Controller
@Autowired
@RequestMapping("/list.do") //URL
String list = fooService.getAll();
return list;
@RequestMapping("/del.do") //URL
fooService.doDel(request.getParameter("id"));
}
2
@RequestMapping("/doFoo.do")// URL
@Autowired
//listURL /doFoo.do?mode=list
public String list() {
system.out.println(list);
}
@RequestMapping(params = "mode=del")
HttpServletResponse response) {
}
2让请求处理方法处理特定的HTTP请求如POST类型的,请看下面的代码清单。
@Controller
publicclass FooController {
@RequestMapping(params = "mode=submit",
public String submit(HttpServletRequest request,
system.out.println(" submit .");
}
submitPOSTURL
代码清单4
@RequestMapping("/doFoo.do")// URL
@Autowired
//delURL /doFoo.do?mode=del&id=10
public String del(int id) {
return"success";
}
Spring del() id del() id del() String Spring ModelAndView Spring MVC 4
@Controller
publicclass FooController {
private FooService fooService;
@RequestMapping(params = "mode=del")
fooService.doDel(id);
}
5 del() id @RequestParam("id") id URL
编辑推荐:
温馨提示:因考试政策、内容不断变化与调整,长理培训网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准! (责任编辑:长理培训)
点击加载更多评论>>