一尘不染

如何将Spring MVC控制器映射到带有和不带有斜杠的uri?

spring-mvc

我有一个带有几个针对不同URI的RequestMapping的Spring控制器。我的servlet是“
ui”。Servlet的基本URI仅可使用斜杠。我希望我的用户不必输入斜杠。

此URI起作用:

http://localhost/myapp/ui/

这不是:

http://localhost/myapp/ui

它给了我一条HTTP Status 404消息。

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

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

我的控制器:

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

}

阅读 426

收藏
2020-06-01

共1个答案

一尘不染

例如,如果您的Web应用程序位于Web服务器的webapps目录中,webapps/myapp/则可以使用http://localhost:8080/myapp/默认的Tomcat端口访问此应用程序上下文的根。
我认为 默认情况下,无论是否有斜杠,这都可以使用-肯定是Jetty v8.1.5中的情况

按下/myappSpring
DispatcherServlet接管之后,<servlet- name>按照您的配置将请求路由到web.xml,在您的情况下为/ui/*

然后,DispatcherServlet将所有请求从路由http://localhost/myapp/ui/@Controller

控制器 本身可以使用 @RequestMapping(value = "/*")炫魅()
方法,这将导致两个http://localhost/myapp/ui/http://localhost/myapp/ui被路由到 炫魅()

注意:由于SPR-7064,您还应该使用Spring> =
v3.0.3

为了完整起见,以下是我对此进行测试的文件:

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>

而且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

希望这可以帮助!

2020-06-01