spring mvc: 可参数化的视图控制器(在配置中指定jsp文件)MultiActionController/SimpleUrlHandlerMapping/ParameterizableViewController

spring mvc: 可参数化的视图控制器(在配置中指定jsp文件)MultiActionController/SimpleUrlHandlerMapping/ParameterizableViewController

访问地址如下:

http://localhost:8080/项目/index.html

注意:变量不能解析,不知道为啥。

StudentController.java是在springtest包下面

xml中用到的类,

org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
org.springframework.web.servlet.mvc.ParameterizableViewController

java-controller中用到的类

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

  

配置文件web.xml, applicationContext.xml,xxxx-servlet.xml

web.xml

  <!--配置文件路径-->
<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>springtest</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>springtest</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>    
  

  

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">


<!-- 默认:注解映射支持 -->		
<mvc:annotation-driven/>
<!-- 静态资源配置 -->
<mvc:resources location="/pages/**" mapping="/pages/"/>

<!-- 自动扫描包名,controller -->
<context:component-scan base-package="springmvc"/>
			



</beans>

  

springtest-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">
					
			
			
			
<!-- 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>					



<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
     <value>index.html=studentController</value>
  </property>
</bean>
<bean id="studentController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
  <property name="viewName" value="student_index"/>
</bean>

					
</beans>

  

StudentController.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 StudentController extends  MultiActionController {

	
	public ModelAndView home(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView model = new ModelAndView("student_index");
		model.addObject("message", "home");
		model.addObject("title", "student");
		return model;
	}
	
	
	
}

  

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-student</title>
</head>
<body>

${title}
<br>

hi,
${message}

</body>
</html>

  

  

原文地址:https://www.cnblogs.com/achengmu/p/8985592.html