SpringMVC的学习之注解
2018/5/27 21:02:06
SpringMVC身为Spring大家庭的一员,Spring所使用的注解在springMVC中也最为常用。
所谓注解可以简单理解为标签,一般来说我们平常喜欢给别人贴标签,比如说当你的朋友说门外有个美女,这样你的脑海中会形成一个皮肤比较好,身材姣好的女生,这个美女就是一个标签,它是一个单词,描述或者说定义了一些行为或者事务,而注解一般也是一个单词,只不过首字符都是一个@符号,注解声明这个方法或者类具有某些功能,行为和属性,使用注解可以减少我们的代码量,更有助于我们开发。
1,@Controller
spring MVC当中,Controller是应用的控制层,他接受用户的请求信息然后进行处理,@Controller负责声明一个bean到spring的上下文,类似于在spring配置文件声明一个
<bean id="helloControl" class="src.main.controller.HelloWorlld"></bean>
bean的ID默认是类名称开头首字母小写,同时也可为指定ID的值@Controller(“bean_id”)。但是只是一个@Controller还无法无法完成任务,还需要@RequestMapping的配合使用才可以
2,@RequestMapping
RequestMapping是一个用于处理请求地址映射的注解,可以用在类的上面,也可以用在方法上面,用法如下:
@RequestMapping("/url_value")
当浏览器发送”/url_value”,会进入到对应的方法中。
实际运用
这里开始搭建环境,以一个小例子讲解下:本例子是依照上一篇spring MVC(一)为背景,这此基础上修改,第一步修改控制类:
原本类为
public class HelloWorlld implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mav = new ModelAndView("index.jsp");
mav.addObject("message","hello SpringMVC");
return mav;
}
}
修改为
@Controller
@RequestMapping("/hello")
public class HelloWorlld{
@RequestMapping("/handle")
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mav = new ModelAndView("index.jsp");
mav.addObject("message","hello SpringMVC");
return mav;
}
}
第二步修改配置文件
添加context命名空间以及标签规范,代码如下:
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
注意添加到对应位置。
然后使用开启扫描包中使用注解的类,然后把对应根据对应注解创建对应的配置存入spring上下文,原本配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
<!-- HandlerMapping声明 -->
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.
SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello">helloControl</prop><!-- key为url,value为控制器-->
</props>
</property>
<!-- 控制器的配置 -->
<bean id="helloControl" class="src.main.controller.HelloWorlld"></bean>
</bean>
修改后如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
<!-- HandlerMapping声明 -->
<context:component-scan base-package="src.main"/>
</beans>
配置完
当浏览器发送http:localhost:8089/项目名/hello/handle 即可跳转到对应的控制方法,在控制方法中就可以对请求做出处理,如果是需要使用Servlet对象,例如HttpServletRequest或者HttpServletResponse,则可以在本方法添加方法参数HttpServletRequest request,HttpServletResponse response,代码如下:
public String select(HttpServletRequest request,HttpServletResponse response,HttpSession session){
request.getParameter("id");
return "index";
}
返回值即是需跳转的视图名称。
多知道点
使用注解的方式可以不需要再实现Controller接口,配置的方法更加的灵活,同时可以为控制方法添加不同的形参以调用对应对象的方法。
如果需要获取请求参数可以设置HttpServletRequest参数,如下:
public String select(HttpServletRequest request){
return "index";
}
如果需要重定向也可以添加HttpServletResponse参数,例如获取session也可以添加HttpSession session对象为参数
public String select(HttpServletRequest request,HttpServletResponse response,HttpSession session){
}