————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-
或许曾经的我们也见过一种方式http://localhost:8080/项目名/后台servlet?actionName=login&uname=admin&upwd=123
这种方式调度servlet并且传参数,这里我要表达什么呢?就是?后面可以拼接内容,
所以,此处的ParameterMethodNameResolver就是通过这种方式来访问到的方法名的
说一下案例使用步骤
一,定义ParameterMethodNameResolver参数方法名称解析器****
二,将上方一定义的方法名称解析器注入自己定义的处理器的bean中
三,在处理器映射器SimpleUrlHandlerMapping中将那个访问的key值,由/*,改为具体值,不能再通配符的方式
案例源码
处理器类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1package cn.dawn.day05multiActioncontroller;
2
3import org.springframework.web.servlet.ModelAndView;
4import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
5
6import javax.servlet.http.HttpServletRequest;
7import javax.servlet.http.HttpServletResponse;
8
9/**
10 * Created by Dawn on 2018/3/23.
11 */
12public class MyMultiActionController extends MultiActionController{
13
14 public String doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
15 ModelAndView me=new ModelAndView();
16 me.setViewName("first");
17 return "first";
18 }
19
20 public ModelAndView doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
21 ModelAndView me=new ModelAndView();
22 me.setViewName("second");
23 return me;
24 }
25}
26
自己定义的xml文件中:(三个步骤)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
5
6 <!--配置bean处理器-->
7 <bean id="myMultiActionController" class="cn.dawn.day05multiActioncontroller.MyMultiActionController">
8 <!--第二步,将参数方法名称解析器注入-->
9 <property name="methodNameResolver" ref="parameterMethodNameResolver"></property>
10 </bean>
11 <!--视图解析器-->
12 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
13 <property name="prefix" value="/"></property>
14 <property name="suffix" value=".jsp"></property>
15 </bean>
16
17 <!--第一步,参数方法名称解析器-->
18 <bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
19 <!--这个actionName就是url问号?后面等于号=左边的那个参数名-->
20 <property name="paramName" value="actionName"></property>
21 </bean>
22
23 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
24 <!--第一种方式-->
25 <property name="urlMap">
26 <map>
27 <!--第三步,这儿需要改成/*,而不是之前写死的那种-->
28 <entry key="/doFirst">
29 <value>myMultiActionController</value>
30 </entry>
31 </map>
32 </property>
33
34 </bean>
35
36</beans>
37
按照此配置方法,你的访问url为http://ip地址:tomcat端口号/项目名/处理器映射器配的key值?actionName=你处理器中的方法名
注意检查你有没有我上面的first.jsp和second.jsp页面,和web.xml有没有将自己最后写的xml配置文件引用上