如何将 Spring MVC 控制器映射到带和不带斜杠的 uri? [英] How do I map Spring MVC controller to a uri with and without trailing slash?

查看:19
本文介绍了如何将 Spring MVC 控制器映射到带和不带斜杠的 uri?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring 控制器,其中包含针对不同 URI 的多个 RequestMapping.我的 servlet 是ui".servlet 的基本 URI 仅适用于尾部斜杠.我希望我的用户不必输入尾部斜杠.

此 URI 有效:

http://localhost/myapp/ui/

这个没有:

http://localhost/myapp/ui

它给了我一个 HTTP 状态 404 消息.

来自我的 web.xml 的 servlet 和映射是:

<servlet-name>ui</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><启动时加载>1</启动时加载></servlet><servlet-mapping><servlet-name>ui</servlet-name><url-pattern>/ui/*</url-pattern></servlet-mapping>

我的控制器:

@Controller公共类 UiRootController {@RequestMapping(value={"","/"})公共 ModelAndView mainPage() {数据模型模型 = initModel();模型.setView("介绍");return new ModelAndView("main", "model", model);}@RequestMapping(value="/other"})公共 ModelAndView otherPage() {数据模型模型 = initModel();model.setView("otherPage");return new ModelAndView("other", "model", model);}}

解决方案

如果您的 Web 应用程序存在于 Web 服务器的 webapps 目录中,例如 webapps/myapp/ 那么此应用程序上下文的根目录可以假设默认 Tomcat 端口,可以在 http://localhost:8080/myapp/ 访问.这应该可以使用或不使用尾部斜杠,我认为默认情况下 - 当然在 Jetty v8.1.5 中就是这种情况

一旦你点击 /myapp,Spring DispatcherServlet 接管,将请求路由到 <servlet-name> 在您的 web.xml 中配置,在您的案例是/ui/*.

DispatcherServlet 然后将来自 http://localhost/myapp/ui/ 的所有请求路由到 @Controllers.

Controller 本身中,您可以将 @RequestMapping(value = "/*") 用于 mainPage() 方法,这将导致 http://localhost/myapp/ui/http://localhost/myapp/ui 都被路由到 mainPage().

注意:由于 SPR-7064= v3.0.3>

为了完整起见,以下是我测试过的文件:

src/main/java/controllers/UIRootController.java

包控制器;导入 org.springframework.stereotype.Controller;导入 org.springframework.web.bind.annotation.RequestMapping;导入 org.springframework.web.servlet.ModelAndView;@控制器公共类 UiRootController {@RequestMapping(value = "/*")公共 ModelAndView mainPage() {返回新的 ModelAndView("index");}@RequestMapping(value="/other"})公共 ModelAndView otherPage() {返回新的 ModelAndView("other");}}

WEB-INF/web.xml

<小服务程序><servlet-name>ui</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><启动时加载>1</启动时加载><!-- spring 自动发现/WEB-INF/<servlet-name>-servlet.xml --></servlet><servlet-mapping><servlet-name>ui</servlet-name><url-pattern>/ui/*</url-pattern></servlet-mapping></web-app>

WEB-INF/ui-servlet.xml

<context:component-scan base-package="controllers"/><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:订单=2"p:viewClass="org.springframework.web.servlet.view.JstlView"p:prefix="/WEB-INF/views/"p:suffix=".jsp"/></豆类>

还有 2 个 JSP 文件位于 WEB-INF/views/index.jspWEB-INF/views/other.jsp.

结果:

  • http://localhost/myapp/ -> 目录列表
  • http://localhost/myapp/uihttp://localhost/myapp/ui/ -> index.jsp
  • http://localhost/myapp/ui/otherhttp://localhost/myapp/ui/other/ -> other.jsp

希望这有帮助!

I have a Spring Controller with several RequestMappings for different URIs. My servlet is "ui". The servlet's base URI only works with a trailing slash. I would like my users to not have to enter the trailing slash.

This URI works:

http://localhost/myapp/ui/

This one does not:

http://localhost/myapp/ui

It gives me a HTTP Status 404 message.

The servlet and mapping from my web.xml are:

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

<servlet-mapping>
    <servlet-name>ui</servlet-name>
    <url-pattern>/ui/*</url-pattern>
</servlet-mapping>    

My Controller:

@Controller
public class UiRootController {

    @RequestMapping(value={"","/"})
    public ModelAndView mainPage() { 
        DataModel model = initModel();
        model.setView("intro");     
        return new ModelAndView("main", "model", model);
    }

    @RequestMapping(value={"/other"})
    public ModelAndView otherPage() { 
        DataModel model = initModel();
        model.setView("otherPage");     
        return new ModelAndView("other", "model", model);
    }

}

解决方案

If your web application exists in the web server's webapps directory, for example webapps/myapp/ then the root of this application context can be accessed at http://localhost:8080/myapp/ assuming the default Tomcat port. This should work with or without the trailing slash, I think by default - certainly that is the case in Jetty v8.1.5

Once you hit /myapp the Spring DispatcherServlet takes over, routing requests to the <servlet-name> as configured in your web.xml, which in your case is /ui/*.

The DispatcherServlet then routes all requests from http://localhost/myapp/ui/ to the @Controllers.

In the Controller itself you can use @RequestMapping(value = "/*") for the mainPage() method, which will result in both http://localhost/myapp/ui/ and http://localhost/myapp/ui being routed to mainPage().

Note: you should also be using Spring >= v3.0.3 due to SPR-7064

For completeness, here are the files I tested this with:

src/main/java/controllers/UIRootController.java

package controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UiRootController {
  @RequestMapping(value = "/*")
  public ModelAndView mainPage() {
    return new ModelAndView("index");
  }

  @RequestMapping(value={"/other"})
  public ModelAndView otherPage() {
    return new ModelAndView("other");
  }
}

WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
  version="3.0" metadata-complete="false">
  <servlet>
    <servlet-name>ui</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <!-- spring automatically discovers /WEB-INF/<servlet-name>-servlet.xml -->
  </servlet>

  <servlet-mapping>
    <servlet-name>ui</servlet-name>
    <url-pattern>/ui/*</url-pattern>
  </servlet-mapping>
</web-app>

WEB-INF/ui-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">

<context:component-scan base-package="controllers" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:order="2"
  p:viewClass="org.springframework.web.servlet.view.JstlView"
  p:prefix="/WEB-INF/views/"
  p:suffix=".jsp"/>
</beans>

And also 2 JSP files at WEB-INF/views/index.jsp and WEB-INF/views/other.jsp.

Result:

  • http://localhost/myapp/ -> directory listing
  • http://localhost/myapp/ui and http://localhost/myapp/ui/ -> index.jsp
  • http://localhost/myapp/ui/other and http://localhost/myapp/ui/other/ -> other.jsp

Hope this helps!

这篇关于如何将 Spring MVC 控制器映射到带和不带斜杠的 uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆