java

位置:IT落伍者 >> java >> 浏览文章

Spring2.5注释驱动与基于注释的MVC


发布日期:2018年09月17日
 
Spring2.5注释驱动与基于注释的MVC

Spring

Foojava FooStringname

package comtonytest;

publicclass Foo {

private String name;

public String toStirng(){

returnFoo Name is : + thisname;

}

Setget

}

Barjava BarStringadd

package comtonytest;

publicclass Bar {

private String add;

public String toStirng(){

returnBar Add is : + thisadd;

}

Setget

}

Mainjava MainFooBar

package comtonytest;

publicclass Main {

private Foo foo;

private Bar bar;

public String toString(){

returnMain : [ + thisfootoStirng() + + thisbartoStirng() + ];

}

Setget

}

Springconfigbeansxml

<bean id=main class=comtonytestMain>

<property name=foo ref=foo></property>

<property name=bar ref=bar></property>

</bean>

<bean id=foo class=comtonytestFoo>

<property name=name value=Foo></property>

</bean>

<bean id=bar class=comtonytestBar>

<property name=add value=Bar></property>

</bean>

Testjava TestSpringmain

package comtonytest;

import orgntextApplicationContext;

import orgntextsupport

ClassPathXmlApplicationContext;

publicclass Test {

publicstaticvoid main(String[] args) {

String[] locations = {springconfigbeansxml};

ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);

Main main = (Main) ctxgetBean(main);

systemoutprintln(main);

}

}

Test

Main : [Foo Name is :Foo Bar Add is :Bar]

)Spring package comtonytest;

import orgspringframeworkbeansfactoryannotationAutowired;

publicclass Main {

@Autowired

private Foo foo;

@Autowired

private Bar bar;

public String toString(){

returnMain : [ + thisfootoStirng() + + thisbartoStirng() + ];

}

}

Spring

<! BeanPostProcessor @Autowired Bean >

<bean class=orgspringframeworkbeansfactoryannotation

AutowiredAnnotationBeanPostProcessor/>

<!— main Bean >

<bean id=main class=comtonytestMain></bean>

<bean id=foo class=comtonytestFoo>

<property name=name value=Foo></property>

</bean>

<bean id=bar class=comtonytestBar>

<property name=add value=Bar></property>

</bean>

)Spring @Qualifier(foo)

publicclass Main {

@Autowired

@Qualifier(foo)

private Foo foo;

@Autowired

private Bar bar;

public String toString(){

returnMain : [ + thisfootoStirng() + + thisbartoStirng() + ];

}

}

<bean class=orgspringframeworkbeansfactoryannotation

AutowiredAnnotationBeanPostProcessor/>

<bean id=main class=comtonytestMain></bean>

<bean id=foo class=comtonytestFoo>

<property name=name value=Foo></property>

</bean>

<bean id=foo class=comtonytestFoo>

<property name=name value=Foo></property>

</bean>

<bean id=bar class=comtonytestBar>

<property name=add value=Bar></property>

</bean>

Testjava:

Main : [Foo Name is :Foo Bar Add is :Bar]

Springfoomain

)<context:annotationconfig/>Spring <context:annotationconfig/>

<context:annotationconfig/>

<bean id=main class=comtonytestMain></bean>

<bean id=foo class=comtonytestFoo>

<property name=name value=Foo></property>

</bean>

<bean id=foo class=comtonytestFoo>

<property name=name value=Foo></property>

</bean>

<bean id=bar class=comtonytestBar>

<property name=add value=Bar></property>

</bean>

<bean class=orgspringframeworkbeansfactoryannotation

AutowiredAnnotationBeanPostProcessor/>

<context:annotationconfig/>

<context:annotationconfig/> ) @Component

publicclass Foo {

private String name = Foos name;

public String toStirng(){

returnFoo Name is : + thisname;

}

}

@ComponentBean

Mainjava

@Component(main)

publicclass Main {

@Autowired

private Foo foo;

@Autowired

private Bar bar;

……

@Component <context:componentscan basepackage=comtonytest/>

SpringSpring 也为 Spring MVC引入了注释驱动功能现在我们无须让Controller继承任何接口无需在XML配置文件中定义请求和Controller的映射关系仅仅使用注释就可以让一个POJO具有Controller的绝大部分功能 —— Spring MVC框架的易用性得到了进一步的增强

) 基于注解的

由于Spring MVC的Controller必须事先是一个Bean所以 @Controller注解是不可缺少的请看下面的代码清单

代码清单

@Controller //Controller

publicclass FooController {

@Autowired

private FooService fooService;

@RequestMapping(/listdo) //URL

public String[] list() {

String[] list = fooServicegetAll();

systemoutprintln(list);

return list;

}

@RequestMapping(/deldo) //URL

publicvoid del(HttpServletRequest request HttpServletResponse response) {

fooServicedoDel(requestgetParameter(id));

}

}

@ControllerFooControllerjavaFooControllerjavaURLURL

) 一个Controller对应一个URL由请求参数决定请求处理方法

@Controller

@RequestMapping(/doFoodo)// URL

publicclass FooController {

@Autowired

private FooService fooService;

//listURL /doFoodo?mode=list

@RequestMapping(params = mode=list)

public String[] list() {

String[] list = fooServicegetAll();

systemoutprintln(list);

return list;

}

//delURL /doFoodo?mode=del

@RequestMapping(params = mode=del)

publicvoid del(HttpServletRequest request

HttpServletResponse response) {

fooServicedoDel(requestgetParameter(id));

}

}

让请求处理方法处理特定的HTTP请求如POST类型的请看下面的代码清单

) 让请求处理方法处理特定的 HTTP 请求方法

@Controller

@RequestMapping(/doFoodo)// URL

publicclass FooController {

//POST

@RequestMapping(params = mode=submit

method = RequestMethodPOST)

public String submit(HttpServletRequest request

HttpServletResponse response){

systemoutprintln( submit );

returnsuccess;

}

}

submitPOSTURL

) 处理方法入参绑定 URL 参数

代码清单

@Controller

@RequestMapping(/doFoodo)// URL

publicclass FooController {

@Autowired

private FooService fooService;

//delURL /doFoodo?mode=del&id=

@RequestMapping(params = mode=del)

public String del(int id) {

fooServicedoDel(id);

returnsuccess;

}

}

/doFoodo?mode=del&id=URL

Spring del() id del() id del() String Spring ModelAndView Spring MVC

@Controller

@RequestMapping(/doFoodo)// URL

publicclass FooController {

@Autowired

private FooService fooService;

//delURL /doFoodo?mode=del&id=

@RequestMapping(params = mode=del)

public String del(@RequestParam(id)int id) {

fooServicedoDel(id);

returnsuccess;

}

}

del() id @RequestParam(id) id URL

               

上一篇:用Struts框架开发Java应用

下一篇:基于Struts的权限实现