spring mvc: multi-action controller (methods that implement multiple accesses under the Controller) MultiActionController / BeanNameUrlHandlerMapping

spring mvc: multi-action controller (methods that implement multiple accesses under the Controller)

For example, my controller is UserController.java, and there are multiple methods such as home, add, remove, etc.

address:

http://localhost:8080/projectname/user/add.html

http://localhost:8080/projectname/user/home.html

http://localhost:8080/projectname/user/remove.html

 

The class you need to use: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

In the configuration file:

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

  

In the code class:

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

  

 

Currently there are three configuration files: applicationContext.xml, springmvc-servlet.xml, web.xml

Specifically look at the code:

web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>  
  
  <!--Configuration file path-->
<context-param>
 	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<!-- character filter -->  
<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>

<!-- Monitor forwarding-->
<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">
      
 

<!-- Default annotation mapping support -->
<mvc:annotation-driven />
<!-- Static resources -->
<mvc:resources mapping="/pages/**" location="/pages/"/>
 <!-- Automatically scanned package name-->
<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 upload file -->
<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

<!-- Multi-Motion Controller-->
<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;
	}
}

  

The jsp code is basically the same:

<%@ 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>

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324890845&siteId=291194637