SpringMVC笔记:annotation注解式开发
一、配置式开发
在我们之前的学习中,springmvc使用配置式开发模式,需要在核心配置文件中springmvc.xml注册大量的bean来注入controller控制器,工作繁琐容易出错,下面我们学习一下注解式开发来简化我们的工作。
。。。
案例:
1.控制器
public class MyMultiController extends MultiActionController { /*第一个方法*/ public String doFirst(HttpServletRequest request, HttpServletResponse response){ System.out.println("执行方法一!!!"); return "first"; } /*第二个方法*/ public String doSecond(HttpServletRequest request, HttpServletResponse response){ System.out.println("执行方法二!!!"); return "second"; } }
2.spring注册
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--控制器映射--> <bean class="cn.springmvc.day02handler.MyPropertiesController" id="myPropertiesController"> <property name="methodNameResolver" ref="methodNameResolver"></property> </bean> <!--方法名称解析器--> <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" id="methodNameResolver"> <!--<property name="paramName" value="action"></property>--> </bean> <!--处理器映射器--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hello.do">myPropertiesController</prop> </props> </property> </bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.访问
localhost:8080/hello.do?action=doFirst
-----------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--porperties--> <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver" id="propertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/first.do">doFirst</prop> <prop key="/second.do">doSecond</prop> </props> </property> </bean> <!--控制器映射器--> <bean class="cn.springmvc.day02handler.MyPropertiesController" id="myPropertiesController"> <property name="methodNameResolver" ref="propertiesMethodNameResolver"></property> </bean> <!--映射器配置--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/*.do">myPropertiesController</prop> </props> </property> </bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀 prefix--> <property name="prefix" value="/"></property> <!--后缀 suffix--> <property name="suffix" value=".jsp"></property> </bean> </beans>
访问:
localhost:8080/first.do | second.do
----------------------------------------------------
二、注解式开发
1.在控制器的类和方法上添加注解
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controllerpublic class MyAnnontation { /*方法一*/ @RequestMapping("/first.do") public ModelAndView getModel(HttpServletRequest request, HttpServletResponse response){ System.out.println("注解方法一!"); ModelAndView mv = new ModelAndView("first"); return mv; } /*方法一*/ @RequestMapping("/second.do") public ModelAndView getView(HttpServletRequest request, HttpServletResponse response){ System.out.println("注解方法二!"); ModelAndView mv = new ModelAndView("second"); return mv; } }
2.配置文件中添加注解驱动<mvc:annotation-driven/>
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描controller--> <context:component-scan base-package="cn.springmvc.annotation"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!--注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
3.简单访问
。。。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Annotation</title> </head> <body> <h1>未标明命名空间</h1> <form action="/first.do" method="get"> <input type="submit" value="执行方法一"> </form> <form action="/second.do" method="get"> <input type="submit" value="执行方法一"> </form> <hr/> <h1>已标明命名空间</h1> <form action="/annotation/first.do" method="get"> <input type="submit" value="执行方法一"> </form> <form action="/annotation/second.do" method="get"> <input type="submit" value="执行方法一"> </form> </body> </html>
三、注解式开发中引入命名空间来避免重名方法的混淆
1.在类名上添加RequestMapping("/name")注解即可区分
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller /*命名空间*/ @RequestMapping("/annotation") public class MyAnnontation { /*方法一*/ @RequestMapping("/first.do") public ModelAndView getModel(HttpServletRequest request, HttpServletResponse response){ System.out.println("注解方法一!"); ModelAndView mv = new ModelAndView("first"); return mv; } /*方法一*/ @RequestMapping("/second.do") public ModelAndView getView(HttpServletRequest request, HttpServletResponse response){ System.out.println("注解方法二!"); ModelAndView mv = new ModelAndView("second"); return mv; } }