Spring MVC控制器之ParameterizableViewController使用

这几天在看walker讲的Spring MVC视频教程,在网上没有找到相应的实例,自己整出来让大家做个参考。

1、新建一个web项目,名字为test。

2、在web.xml配制如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name></display-name>

	<servlet>
		<servlet-name>test</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>test</servlet-name>
		<url-pattern>*.test</url-pattern>
	</servlet-mapping>



	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

2、在WEB-INF下新建一个test-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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="login.test">loginController</prop>
			</props>
		</property>
	</bean>
	<bean id="loginController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
		<property name="viewName" value="/WEB-INF/jsp/login.jsp"></property>
	</bean>
</beans>

 

 3、在WEB-INF下新建一个jsp文件夹,在jsp文件夹下放入一个login.jsp,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'index.jsp' starting page</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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is login jsp. <br>
  </body>
</html>

 

 

 然后直接在tomcat上部署运行及可。

ParameterizableViewController的主要作用是对url直接跳转,例如,在做一个登录页面时,并不需要对登录页面做任何的模型处理,所以可以用 ParameterizableViewController对页面进行进行跳转。

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(spring mvc)