spring mvc: 多动作控制器(Controller下面实现多个访问的方法)MultiActionController / BeanNameUrlHandlerMapping

时间:2023-06-18 10:46:56

spring mvc: 多动作控制器(Controller下面实现多个访问的方法)

比如我的控制器是UserController.java,下面有home, add, remove等多个方法

访问地址:

http://localhost:8080/项目名/user/add.html

http://localhost:8080/项目名/user/home.html

http://localhost:8080/项目名/user/remove.html

需要用到的类:org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

配置文件中:

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

  

代码类中:

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

  

目前有applicationContext.xml , springmvc-servlet.xml, web.xml三个配置文件

具体来看代码:

web.xml

<web-app>
<display-name>Archetype Created Web Application</display-name> <!--配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <!-- 字符过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 监听转发 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

  

springmvc-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 静态资源 -->
<mvc:resources mapping="/pages/**" location="/pages/"/>
<!-- 自动扫描的包名 -->
<context:component-scan base-package="springmvc" /> </beans>

  

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- spring mvc上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> <!-- 多动作控制器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/home.html" class="springmvc.UserController" />
<bean name="/user/*.html" class="springmvc.UserController" /> </beans>

  

UserController.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;; public class UserController extends MultiActionController { public ModelAndView home(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView("user_home");
mv.addObject("message", "home");
return mv; } public ModelAndView add(HttpServletRequest request, HttpServletResponse response)
{
ModelAndView mv = new ModelAndView("user_add");
mv.addObject("message", "add");
return mv;
} public ModelAndView remove(HttpServletRequest request, HttpServletResponse response)
{
ModelAndView mv = new ModelAndView("user_remove");
mv.addObject("message", "remove");
return mv;
}
}

  

jsp代码基本上差不多:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>home</title>
</head>
<body> ${message} </body>