1、配置式開發html
在咱們以前的學習中,springmvc使用配置式開發模式,須要在覈心配置文件中springmvc.xml註冊大量的bean來注入controller控制器,工做繁瑣容易出錯,下面咱們學習一下註解式開發來簡化咱們的工做。java
。。。web
案例:spring
1.控制器spring-mvc
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註冊mvc
<?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.訪問app
localhost:8080/hello.do?action=doFirstjsp
-----------------------------------------------------------------------------------學習
<?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>
訪問:spa
localhost:8080/first.do | second.do
----------------------------------------------------
2、註解式開發
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>
3、註解式開發中引入命名空間來避免重名方法的混淆
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; } }