以下示例显示如何使用Spring Web MVC框架来实现一个简单URL处理程序映射。 SimpleUrlHandlerMapping
类分别显式地将URL
映射到相应的控制器上。
所下所示配置 -
<beans> <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"> <props> <prop key="/welcome.html">welcomeController</prop> <prop key="/helloWorld.html">helloController</prop> </props> </property> </bean> <bean id="helloController" class="com.zyiz.springmvc.HelloController" /> <bean id="welcomeController" class="com.zyiz.springmvc.WelcomeController"/> </beans>
例如,使用上面的配置,如果URI:
/helloWorld.html
请求,DispatcherServlet
将请求转发到HelloController
类。/welcome.html
,DispatcherServlet
会将请求转发给WelcomeController
类。首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:
com.zyiz.springmvc
包下创建两个Java类HelloController
和 WelcomeController
。jsp
子文件夹下创建两个视图文件:hello.jsp
和 welcome.jsp
。完整的项目文件目录结构如下所示 -
HelloController.java 的代码如下所示 -
package com.zyiz.springmvc; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class HelloController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView model = new ModelAndView("hello"); model.addObject("message", "Hello World!"); return model; } }
WelcomeController.java 的代码如下所示 -
package com.zyiz.springmvc; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class WelcomeController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView model = new ModelAndView("welcome"); model.addObject("message", "Welcome!"); return model; } }
SimpleUrlHandlerMapping-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"> <props> <prop key="/welcome.html">welcomeController</prop> <prop key="/helloWorld.html">helloController</prop> </props> </property> </bean> <bean id="helloController" class="com.zyiz.springmvc.HelloController" /> <bean id="welcomeController" class="com.zyiz.springmvc.WelcomeController"/> </beans>
hello.jsp 的代码如下所示 -
<%@ page contentType="text/html; charset=UTF-8" %> <html> <head> <title>Hello World</title> </head> <body> <h2>${message}</h2> </body> </html>
welcome.jsp 的代码如下所示 -
<%@ page contentType="text/html; charset=UTF-8" %> <html> <head> <title>Welcome</title> </head> <body> <h2>${message}</h2> </body> </html>
完成创建源和配置文件后,发布应用程序到Tomcat服务器。
现在启动Tomcat服务器,现在尝试访问URL => http://localhost:8080/SimpleUrlHandlerMapping/helloWorld.html ,如果Spring Web应用程序没有问题,应该看到以下结果:
当访问URL => http://localhost:8080/SimpleUrlHandlerMapping/welcome.html , 如果Spring Web应用程序没有问题,应该看到以下结果: