Spring MVC - Bean 名称 Url 处理程序映射示例


以下示例演示如何使用 Spring Web MVC 框架使用 Bean 名称 URL 处理程序映射。BeanNameUrlHandlerMapping类是默认的处理程序映射类,它将URL 请求映射到配置中提到的 bean 的名称。

<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.BeanNameUrlHandlerMapping"/>

   <bean name = "/helloWorld.htm" 
      class = "com.tutorialspoint.HelloController" />

   <bean name = "/hello*" 
      class = "com.tutorialspoint.HelloController" /> 

   <bean name = "/welcome.htm"
      class = "com.tutorialspoint.WelcomeController"/>   
</beans>

例如,使用上面的配置,如果 URI

  • 请求 /helloWorld.htm 或 /hello{any letter}.htm 时,DispatcherServlet 会将请求转发给HelloController

  • 请求 /welcome.htm 时,DispatcherServlet 会将请求转发给WelcomeController

  • 请求 /welcome1.htm 时,DispatcherServlet 将找不到任何控制器,服务器将抛出 404 状态错误。

首先,让我们准备一个可用的 Eclipse IDE,并考虑以下步骤来使用 Spring Web 框架开发基于动态表单的 Web 应用程序。

描述
1 按照 Spring MVC - Hello World 章节中的说明,在 com.tutorialspoint 包下创建一个名为 TestWeb 的项目。
2 在 com.tutorialspoint 包下创建 Java 类 HelloController、WelcomeController。
3 在jsp子文件夹下创建视图文件hello.jsp、welcome.jsp。
4 最后一步是创建所有源文件和配置文件的内容并导出应用程序,如下所述。

HelloController.java

package com.tutorialspoint;

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;
   }
}

欢迎控制器.java

package com.tutorialspoint;

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;
   }
}

TestWeb-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.BeanNameUrlHandlerMapping"/>

   <bean name = "/helloWorld.htm" 
      class = "com.tutorialspoint.HelloController" />

   <bean name = "/hello*" 
      class = "com.tutorialspoint.HelloController" /> 

   <bean name = "/welcome.htm"
      class = "com.tutorialspoint.WelcomeController"/>   
</beans>

你好.jsp

<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h2>${message}</h2>
   </body>
</html>

欢迎.jsp

<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Welcome</title>
   </head>
   <body>
      <h2>${message}</h2>
   </body>
</html>

创建源文件和配置文件后,导出您的应用程序。右键单击您的应用程序,使用Export → WAR File选项并将TestWeb.war文件保存在 Tomcat 的 webapps 文件夹中。

现在,启动 Tomcat 服务器并确保您能够使用标准浏览器从 webapps 文件夹访问其他网页。尝试使用 URL - http://localhost:8080/TestWeb/helloWorld.htm,如果 Spring Web 应用程序一切正常,我们将看到以下屏幕。

Spring Bean 名称 Url 处理程序映射 1

尝试使用 URL - http://localhost:8080/TestWeb/hello.htm,如果 Spring Web 应用程序一切正常,我们将看到以下屏幕。

Spring Bean 名称 Url 处理程序映射 2

尝试输入 URL http://localhost:8080/TestWeb/welcome.htm,如果 Spring Web 应用程序一切正常,我们将看到以下屏幕。

Spring Bean 名称 Url 处理程序映射 3

尝试输入 URL http://localhost:8080/TestWeb/welcome1.htm,如果 Spring Web 应用程序一切正常,我们将看到以下屏幕。

Spring Bean 名称 Url 处理程序映射 4