spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping

根据地址栏上的url的参数action来解析相应的控制器下的方法名,例如:

http://localhost:8080/项目名/user/index.html?action=remove

http://localhost:8080/项目名/user/*(可以是任意字符).html?action=remove

xml配置中会用到的类:

  1. org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping
  2. org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver 

java-controller中,会用到:

  1. org.springframework.web.servlet.mvc.multiaction.MultiActionController;

  

本例中用到了3个配置文件:web.xml, applicationContext, xxxx-servlet.xml

web.xml:

  1. <!--配置文件路径-->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>/WEB-INF/applicationContext.xml</param-value>
  5. </context-param>
  6.  
  7. <!-- 字符过滤器 -->
  8. <filter>
  9. <filter-name>encodingFilter</filter-name>
  10. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  11. <init-param>
  12. <param-name>encoding</param-name>
  13. <param-value>UTF-8</param-value>
  14. </init-param>
  15. </filter>
  16. <filter-mapping>
  17. <filter-name>encodingFilter</filter-name>
  18. <url-pattern>/*</url-pattern>
  19. </filter-mapping>
  20.  
  21. <!-- 监听转发 -->
  22. <listener>
  23. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  24. </listener>
  25. <servlet>
  26. <servlet-name>springmvc</servlet-name>
  27. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  28. <load-on-startup>1</load-on-startup>
  29. </servlet>
  30. <servlet-mapping>
  31. <servlet-name>springmvc</servlet-name>
  32. <url-pattern>/</url-pattern>
  33. </servlet-mapping>

applicationContext.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc
  8. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd">
  11.  
  12. <!-- 默认:注解映射支持 -->
  13. <mvc:annotation-driven/>
  14. <!-- 静态资源配置 -->
  15. <mvc:resources location="/pages/**" mapping="/pages/"/>
  16.  
  17. <!-- 自动扫描包名,controller -->
  18. <context:component-scan base-package="springmvc"/>
  19.  
  20. </beans>

springmvc-servlet.xml

  1. <beans
  2. xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd">
  12.  
  13. <!-- ViewResolver -->
  14. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  16. <property name="prefix" value="/WEB-INF/jsp/"/>
  17. <property name="suffix" value=".jsp"/>
  18. </bean>
  19.  
  20. <!-- controller名解析 -->
  21. <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
  22. <property name="caseSensitive" value="true"/>
  23. </bean>
  24. <bean class="springmvc.UserController">
  25. <property name="methodNameResolver">
  26. <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
  27. <property name="paramName" value="action"/>
  28. </bean>
  29. </property>
  30. </bean>
  31.  
  32. </beans>

  

UserController.java代码

  1. import javax.servlet.http.HttpServletRequest;
  2. import javax.servlet.http.HttpServletResponse;
  3.  
  4. import org.springframework.web.servlet.ModelAndView;
  5. import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
  6.  
  7. public class UserController extends MultiActionController {
  8.  
  9. public ModelAndView home(HttpServletRequest request, HttpServletResponse response)
  10. {
  11. ModelAndView model = new ModelAndView("user_home");
  12. model.addObject("message", "home");
  13. return model;
  14.  
  15. }
  16.  
  17. public ModelAndView add(HttpServletRequest request, HttpServletResponse response)
  18. {
  19. ModelAndView model = new ModelAndView("user_add");
  20. model.addObject("message", "add");
  21. return model;
  22.  
  23. }
  24.  
  25. public ModelAndView remove(HttpServletRequest request, HttpServletResponse response)
  26. {
  27. ModelAndView model = new ModelAndView("user_remove");
  28. model.addObject("message", "remove");
  29. return model;
  30.  
  31. }
  32.  
  33. }

  

jsp代码,大同小异:

  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
  2. <%@ page isELIgnored="false" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>home</title>
  8. </head>
  9. <body>
  10.  
  11. hi,${message}
  12.  
  13. </body>
  14. </html>

  

  

  

spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping的更多相关文章

  1. jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址

    jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据.

  2. yii2.0 访问控制器下的方法时出现 Object Not Found! 解决办法

    yii2.0  访问控制器下的方法时出现 Object Not Found! 时 可以查看(apache)  入口文件index.php 的同级有没有 .htaccess 文件 没有.htaccess ...

  3. SpringMVC03 ParameterMethodNameResolver(参数方法名称解析器) And XmlViewResolver(视图解析器)

    参数方法名称解析器 1.配置依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  4. Spring MVC全局异常处理与拦截器校检

    在使用Spring MVC进行开发时,总是要对系统异常和用户的异常行为进行处理,以提供给用户友好的提示,也可以提高系统的安全性. 拦截系统响应错误 首先是拦截系统响应错误,这个可以在web.xml中配 ...

  5. Spring MVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  6. Spring MVC基础知识整理➣拦截器和自定义注解

    概述 Spring MVC中通过注解来对方法或者类进行动态的说明或者标注,类似于配置标识文件的属性信息.当标注的类或者方式被使用时候,通过提取注解信息来达到对类的动态处理.在 MVC中,我们常用的注解 ...

  7. Spring MVC(十)--通过表单序列化传递参数

    通过表单序列化传递参数就是将表单数据转化成字符串传递到后台,序列化之后参数请求变成这种模式param1=value1&&param2=value2,下面用代码实现. 1.创建表单 &l ...

  8. spring mvc 用cookie和拦截器实现自动登录(/免登录)

    Cookie/Session机制详解:http://blog.csdn.net/fangaoxin/article/details/6952954 SpringMVC记住密码功能:http://blo ...

  9. spring mvc redirect 重定向 跳转并传递参数

    在项目中做form表单功能提交时,防止用户客户端后退或者刷新时重复提交问题,需要在服务端进行重定向跳转,具体跳转方式有以下几种方式: 公用代码: @RequestMapping(value=" ...

随机推荐

  1. BDC批量修改物料描述

    一.定义变量 type-POOLs:TRUXS,slis. TYPES: BEGIN OF ty_input , matnr TYPE mara-matnr , " 物料号 maktx TY ...

  2. 深入理解Flink核心技术(转载)

    作者:李呈祥 Flink项目是大数据处理领域最近冉冉升起的一颗新星,其不同于其他大数据项目的诸多特性吸引了越来越多的人关注Flink项目.本文将深入分析Flink一些关键的技术与特性,希望能够帮助读者 ...

  3. HDU3074: Multiply game(线段树单点更新,区间查询)

    题目: 传送门 题解:线段树模板题目. 对递归的题目始终理解不好,我的痛啊,在水的题目都要写很长时间. #include <iostream> #include <string.h& ...

  4. R中遇到的部分问题

    在Rstdio使用的是3.5.1的64位R版本中遇到问题:The Perl script 'WriteXLS.pl' failed to run successfully. 首先使用 Sys.whic ...

  5. Spring整合jdbc编程

    一.Spring对Jdbc的支持    Spring为了提供对Jdbc的支持,在Jdbc API的基础上封装了一套实现,以此建立一个 JDBC 存取框架. 作为 Spring JDBC 框架的核心, ...

  6. matplotlib中文乱码解决方法

    每次编写代码时进行参数设置 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 ...

  7. Jenkins系统+独立部署系统

    原文出自:http://os.51cto.com/art/201601/504846.htm 有了Jenkins,为什么还需要一个独立的部署系统? 现在已经有Jenkins,它自身提供了丰富的部署插件 ...

  8. 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  9. atheros无线驱动之:数据接收流程

    1:数据处理函数tasklet,workqueue在之前的初始化代码中的函数__ath_attach()中,有如下的代码: #ifndef ATH_SUPPORT_HTC#ifdef ADF_SUPP ...

  10. HBase1.2.6 javaapi查看rowkey 所在分区等信息

    Connection connection = HBaseFactory.getIns().getHbaseConn(); RegionLocator r= connection.getRegionL ...