Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
实现一个控制器ShirdrnController,如下所示:
package org.shirdrn.spring.mvc;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ShirdrnController implements Controller {
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
Log logger = LogFactory.getLog(getClass());
Date date = new Date();
logger.info("信息 : ["+date.toLocaleString()+"]调用控制器ShirdrnController,转向index.jsp页面.");
return new ModelAndView("index.jsp");
}
}
当给控制器接收到请求后,转发向index.jsp页面。
在 目录/WEB-INF/下面创建一个Spring配置文件,格式为<SERVLET-NAME>-servlet.xml,例如 shirdrn-servlet.xml,其中shirdrn对应于web.xml中配置的<servlet-name>。在 shirdrn-servlet.xml中装配上面实现的控制器org.shirdrn.spring.mvc.ShirdrnController,配 置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="shirdrnController"
class="org.shirdrn.spring.mvc.ShirdrnController" abstract="false"
singleton="true" lazy-init="default" autowire="default"
dependency-check="default">
</bean>
<bean id="shirdrnUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<property name="mappings">
<props>
<prop key="/shirdrn.abc">shirdrnController</prop>
</props>
</property>
</bean>
</beans>
上面,如果请求为shirdrn.abc的时候,则调用shirdrnController控制器,将转向index.jsp页面,生成视图。
在web.xml中配置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>shirdrn</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>shirdrn</servlet-name>
<url-pattern>*.abc</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
就是,当请求的URL以.abc为后缀的时候,都交由Servlet名称为shirdrn的org.springframework.web.servlet.DispatcherServlet控制视图转发。
编写一个简单的JSP页面index.jsp,如下所示:
<%@ page language="java" import="java.util.*" 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>第一个Spring MVC应用</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>
<h1>我的第一个Spring MVC应用</h1>
<hr><hr>
<hr><hr>
<center>当前时间为 : <%=new java.util.Date() %></center>
</body>
</html>
启动Tomcat Web 服务器,在浏览器地址栏中键入地址:
http://localhost:8080/SpringMVC/shirdrn.abc
可以看到输出index.jsp页面的内容,如图所示: