spring mvc 一个controlller对应多个请求 其中包含 不同请求对应不同目录下的不同视图

***************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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
  <!-- 使用filter过来请求,保证请求过来的参数以UTF-8的编码传入 -->
  <filter>
  	<filter-name>springmvc002-filter</filter-name>
  	<!-- 编码处理过滤器 -->
  	<filter-class>
  		org.springframework.web.filter.CharacterEncodingFilter
  	</filter-class>
	<!-- 给过滤器的encoding属性设置编码格式名称 -->
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
	<!-- 给过滤器设置强制转换编码的属性 -->
  	<init-param>
  		<param-name>forceEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>springmvc002-filter</filter-name>
  	<!-- 对所有请求进行编码过滤处理 -->
  	<url-pattern>/*</url-pattern> 
  </filter-mapping>
  
  <!-- 将所有的请求交给springmvc容器的DispatcherServlet处理 -->
  <servlet>
  	<servlet-name>springmvc002-servlet</servlet-name>
  	<!-- springmvc处理请求的核心servlet类 -->
  	<servlet-class>
  		org.springframework.web.servlet.DispatcherServlet
  	</servlet-class>
  	<!-- 加载springmvc的主配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		
  		<param-value>classpath:*.xml</param-value>
  	</init-param>
  	<!-- 服务器启动就加载springmvc的servlet -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc002-servlet</servlet-name>
  	<!-- 注意这里只能写 /  而不能写 /*  否则出错 -->
	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>


***************Java代码***************

package com.springmvc002.controller;

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

	//注意方法名称 要和前端jsp页面的请求参数m的值保持一致,否则找不到method
	//这里方法的两个参数 request 和 response 必须同时存在,缺一不行,否则方法无法被找到
	public ModelAndView get1(HttpServletRequest request , HttpServletResponse response){
		System.out.println("----------------get1-----------");
		return new ModelAndView("get1");
	}
	
	
	public ModelAndView get2( HttpServletRequest request , HttpServletResponse response){
		System.out.println("----------------get2-----------");
		//前端传递过来的其他参数,可以通过request对象拿到,和servlet相同
		System.out.println(request.getParameter("m"));
		System.out.println("---------" + request.getParameter("username"));
		return new ModelAndView("get2");
	}
	public ModelAndView get3( HttpServletRequest request , HttpServletResponse response){
		System.out.println("----------------get2-----------");
		//前端传递过来的其他参数,可以通过request对象拿到,和servlet相同
		System.out.println(request.getParameter("m"));
		System.out.println("---------" + request.getParameter("username"));
		return new ModelAndView("get2");
	}
}

***************spring mvc 主配置文件代码***************

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 定义一个请求beng 并制定多参数对应方法名称解析器 -->
    <bean name="/get" class="com.springmvc002.controller.MultiController">
    	<property name="MethodNameResolver">
    		<ref bean="parameterMethodName" />
    	</property>
    </bean>
    <!-- 配置多参数对应方法名称解析器 -->
    <bean id="parameterMethodName" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
		<property name="paramName" value="m"></property>    	
    </bean>
    
    <!-- 配置内部资源视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    	<property name="contentType" value="text/html"></property>
    	<property name="prefix" value="/get1/"></property>
    	<property name="suffix" value=".jsp"></property>
    	<!-- 这里的情况可以参照之前的一个文档中的注释说明 
    	<property name="viewNames" value="get1*"></property>-->
    </bean>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    	<property name="contentType" value="text/html"></property>
    	<property name="prefix" value="/get2/"></property>
    	<property name="suffix" value=".jsp"></property>
    	<!-- <property name="viewNames" value="get2*"></property> -->
    </bean>

</beans>

***************jsp代码***************

路径为:/springmvc002/WebContent/get1/get1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <title>标题</title>        
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">    
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
 </head>
 <body>
 	get1.jsp
 </body>
</html>

路径为:/springmvc002/WebContent/get1/get2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <title>标题</title>        
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">    
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
 </head>
 <body>
 get2.jsp-----jack
 </body>
</html>

路径为:/springmvc002/WebContent/get2/get2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <title>标题</title>        
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">    
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
 </head>
 <body>
 get2.jsp
 </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值