前两天发了个spring的mvc,简单xml配置实现的,这次写个注解的。为了便于理解,也是姥姥敲键盘就能实现的。
还以这个例子http://shuadawang-yahoo-com-cn.iteye.com/blog/707599为基础进行展开,改变一下,就实现了注解的mvc。
四个文件 web.xml,test-servlet.xml,java,jsp表现
对于web.xml,原封不动,不改变,参照上篇就可以,jsp表现咱也不变了,一切从简。
test-servlet.xml配置改变一下,改为声明注释的,清单
<context:component-scan base-package="com.test" /> 指明了扫描的包,他会在
这个包里扫描相关注释。,AnnotationMethodHandlerAdapter启动注释功能,看字面意思能猜个差不多吧,要注意一下的是开头的几个引用,不然context不被认。Xml配置就这样了,意思就是说,告诉我一个包,我扫注释去,扫到了符合我要求的就给你注入。
然后看一下之前的Controller ,这次不用显示的写Controller的继承 了,自己随意写,反正xml那边已经发话了,有注释我就认。看下清单
两个注解,分别是 @Controller 和 @RequestMapping
@RequestMapping出现两次,一个是类级别,一个方法级别。意思就是用这个方法,实现/hello的请求,return “hello”就是用hello这个字符串去匹配xml里配置的jsp那段,运行一下
启动服务,在浏览器输入
http://localhost:8080/test/hello.do 出来了
hello, fan ok le!
xia lai mi xi ba!
哦,对了,这个处理多请求也挺方便,control里加个请求,清单如下
贾君鹏也想吃饭了,看了吧,对于吃饭这件事有两个方法,普通人就说 hello, fan ok le! xia lai mi xi ba!,请求贾君鹏吃饭有其他的转向,jia jun peng! your mother call you go home and eat!
由于我太懒了,把转向都指向同一个jsp了,大家别误会,这里随意写,你想转哪都行,执行一下
http://localhost:8080/test/hello/nomal.do
hello, fan ok le!
xia lai mi xi ba!
http://localhost:8080/test/hello/jjp.do
jia jun peng!
your mother call you go home and eat!
哦了,又是姥姥级别的,照着敲一遍领会一下精神吧,要想深入理解,建议多看看reference,源码之类的。
写完了扫一遍才发现,还有个误导人的地方,方法的参数也是随意传,不用拘泥于当初返回类型为ModelAndView的传值,按需取即可。
还有个事儿就是url传参也没写,有点懒了,@RequestParam就可以,也很简单 实践一下吧
ps一下:我用了3.0的包,3.0jar包都加了-3.0.0.RELEASE,直接导入发现了找不到对应的class,后来把后缀去掉就找到了,可能确实不认。
还以这个例子http://shuadawang-yahoo-com-cn.iteye.com/blog/707599为基础进行展开,改变一下,就实现了注解的mvc。
四个文件 web.xml,test-servlet.xml,java,jsp表现
对于web.xml,原封不动,不改变,参照上篇就可以,jsp表现咱也不变了,一切从简。
test-servlet.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="com.test" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
<context:component-scan base-package="com.test" /> 指明了扫描的包,他会在
这个包里扫描相关注释。,AnnotationMethodHandlerAdapter启动注释功能,看字面意思能猜个差不多吧,要注意一下的是开头的几个引用,不然context不被认。Xml配置就这样了,意思就是说,告诉我一个包,我扫注释去,扫到了符合我要求的就给你注入。
然后看一下之前的Controller ,这次不用显示的写Controller的继承 了,自己随意写,反正xml那边已经发话了,有注释我就认。看下清单
package com.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping
public String Eat(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setAttribute("hello", "hello, fan ok le!");
request.setAttribute("eat", "xia lai mi xi ba!");
return "hello";
}
}
两个注解,分别是 @Controller 和 @RequestMapping
@RequestMapping出现两次,一个是类级别,一个方法级别。意思就是用这个方法,实现/hello的请求,return “hello”就是用hello这个字符串去匹配xml里配置的jsp那段,运行一下
启动服务,在浏览器输入
http://localhost:8080/test/hello.do 出来了
hello, fan ok le!
xia lai mi xi ba!
哦,对了,这个处理多请求也挺方便,control里加个请求,清单如下
package com.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/hello/nomal")
public String Eat(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setAttribute("hello", "hello, fan ok le!");
request.setAttribute("eat", "xia lai mi xi ba!");
return "hello";
}
@RequestMapping("/hello/jjp")
public String Eats(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setAttribute("hello", "jia jun peng!");
request.setAttribute("eat", "your mother call you go home and eat!");
return "hello";
}
}
贾君鹏也想吃饭了,看了吧,对于吃饭这件事有两个方法,普通人就说 hello, fan ok le! xia lai mi xi ba!,请求贾君鹏吃饭有其他的转向,jia jun peng! your mother call you go home and eat!
由于我太懒了,把转向都指向同一个jsp了,大家别误会,这里随意写,你想转哪都行,执行一下
http://localhost:8080/test/hello/nomal.do
hello, fan ok le!
xia lai mi xi ba!
http://localhost:8080/test/hello/jjp.do
jia jun peng!
your mother call you go home and eat!
哦了,又是姥姥级别的,照着敲一遍领会一下精神吧,要想深入理解,建议多看看reference,源码之类的。
写完了扫一遍才发现,还有个误导人的地方,方法的参数也是随意传,不用拘泥于当初返回类型为ModelAndView的传值,按需取即可。
还有个事儿就是url传参也没写,有点懒了,@RequestParam就可以,也很简单 实践一下吧
ps一下:我用了3.0的包,3.0jar包都加了-3.0.0.RELEASE,直接导入发现了找不到对应的class,后来把后缀去掉就找到了,可能确实不认。