前两天发了个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配置改变一下,改为声明注释的,清单
<?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,后来把后缀去掉就找到了,可能确实不认。
分享到:
相关推荐
IT学习者Spring MVC注解实例.pdf
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
基于jpa+hibernate+spring+spring mvc注解方式项目案例带整体架构及一个页面操作小流程适合新手
http://blog.csdn.net/penngo/archive/2010/11/27/6038991.aspx这篇文章“spring mvc 注解实现”的附件代码
主要介绍Spring MVC 的使用注解来配置的使用实例
让我们更好的了解 spring mvc 注解方面的知识 适合入门
spring mvc 注解 拦截器 对一个数据库表进行了增删改查操作 ,初学spring mvc框架的 可以参考 代码下载直接可以运行!~~
spring mvc 注解 easyui 分页
spring mvc注解方式整合jdbctemplate
基于spring mvc注解方式,整合mybatis的项目框架哈。
spring mvc 注解 拦截器 对一个数据库表进行了增删改查操作 ,初学spring mvc框架的 可以参考 代码下载直接可以运行!~~ 包含jar和数据库sql语句
spring MVC 注解
Spring mvc 注解Demo,适合初学者。下载即可用
基于spring mvc注解的增删改实例,orm采用hibernate。 资源来自csdn,添加了修改功能和readme说明,让初学者更简单的运行起来。
最全的Spring MVC注解例子,异步请求,错误处理,是基于maven工程,很帮的一个Spring mvc注解例子。
对spring mvc注解的实例
spring3.0MVC注解(附实例). spring3.0MVC注解(附实例). spring3.0MVC注解(附实例).
Spring mvc注解.m4a
spring现在用的相当广泛,注解也非常方便,对于不算太大的项目注解非常省力
Spring3_MVC注解教程.ppt