以下示例顯示如何使用Spring Web MVC框架來實現(xiàn)多動作控制器的可參數(shù)化視圖控制器??蓞?shù)化視圖允許將請求映射到網(wǎng)頁。
所下所示配置 -
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) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Home");
return model;
}
}
URL映射配置文件如下 -
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
index.html=userController
</value>
</property>
</bean>
<bean id="userController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="index"/>
</bean>
例如,使用上面的配置,如果URI:
/index.html
請求,DispatcherServlet
會將請求轉(zhuǎn)發(fā)到UserController
控制器,并將viewName
設(shè)置為index.jsp
。首先,讓我們使用Eclipse IDE,并按照以下步驟使用Spring Web Framework開發(fā)基于動態(tài)表單的Web應(yīng)用程序:
com.yiibai.springmvc
包下創(chuàng)建一個Java類UserController
。jsp
子文件夾下創(chuàng)建一個視圖文件:index.jsp
。完整的項目文件目錄結(jié)構(gòu)如下所示 -
UserController.java 的代碼如下所示 -
package com.yiibai.springmvc;
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) throws Exception {
ModelAndView model = new ModelAndView("user");
model.addObject("message", "Home");
return model;
}
}
ParameterizableViewController-servlet.xml 的代碼如下所示 -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-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.view.InternalResourceViewResolver">
<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=userController
</value>
</property>
</bean>
<bean id="userController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="index"/>
</bean>
</beans>
index.jsp 的代碼如下所示 -
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>index.jsp Page</title>
</head>
<body>
<h2>This is index.jsp page.</h2>
</body>
</html>
完成創(chuàng)建源和配置文件后,發(fā)布應(yīng)用程序到Tomcat服務(wù)器。
現(xiàn)在啟動Tomcat服務(wù)器,當(dāng)訪問URL => http://localhost:8080/ParameterizableViewController/index.html , 如果Spring Web應(yīng)用程序沒有問題,應(yīng)該看到以下結(jié)果: