spring mvc 方法名分离器的使用例子

首先是web.xml的编写:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVCdemo</display-name>
  
  
  <servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

 

引入JAR包:spring.jar,spring-webmvc.jar,commons-logging.jar

 

创建mvc-servlet.xml

<?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-2.0.xsd">
	
	<!--
    <bean id="indexController" class="com.mvc.action.IndexController" />
    <bean id="parametrizableIndexController" class="com.mvc.action.ParametrizableIndexController" >
      <property name="viewName">
        <value>product1</value>
      </property>
    </bean>
    -->
    
    <!-- 
    <bean id="internalPathMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
	   <property name="suffix" value="Yes"/>
   </bean>
    -->
    
    <bean id="internalPathMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
      <property name="paramName">
         <value>method</value>
      </property>
    </bean>
    
    
    <bean id="productController" class="com.mvc.action.ProductController">
      <property name="methodNameResolver" ref="internalPathMethodNameResolver"></property>
    </bean>

	
	<bean id="urlMapping"  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
		<!--
			<value>
				/index.html=indexController
				/about.html=parametrizableIndexController
				/product/view.html=productController
			</value>
		-->
		
		<props>
		  <prop key="view.html">productController</prop>
		</props>
		
		</property>
	</bean>
</beans>

 

然后,创建一个控制器ProductController

package com.mvc.action;

import java.io.IOException;

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 ProductController extends MultiActionController{

	public ModelAndView view(HttpServletRequest request,HttpServletResponse response) throws Exception {
		System.out.println("view !!!!");
		return null;
	}
	
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response) throws Exception {
		System.out.println("Add !!!!");
		return null;
	}
	
}
 

你可能感兴趣的:(spring,mvc,bean,servlet,javaee)