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

2022-09-01 22:04:19

我有一个弹簧控制器,其中包含针对不同URI的几个请求映射。我的服务是“ui”。servlet 的基本 URI 仅适用于尾部斜杠。我希望我的用户不必输入尾部斜杠。

此 URI 的工作原理:

http://localhost/myapp/ui/

这个没有:

http://localhost/myapp/ui

它给了我一个HTTP状态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);
    }

}

答案 1

使用Springboot,我的应用程序可以通过将@RequestMapping的“value”选项设置为空字符串来回复带或不带尾随斜杠的斜杠:

@RestController
@RequestMapping("/some")
public class SomeController {
//                  value = "/" (default) ,
//                  would limit valid url to that with trailing slash.
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Collection<Student> getAllStudents() {
        String msg = "getting all Students";
        out.println(msg);
        return StudentService.getAllStudents();
    }
}

答案 2

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

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

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

控制器本身中,您可以将@RequestMapping(value = “/*”)用于mainPage()方法,这将导致两者都被路由到mainPage()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 文件 at 和 。WEB-INF/views/index.jspWEB-INF/views/other.jsp

结果:

  • http://localhost/myapp/->目录列表
  • http://localhost/myapp/ui和 -> 索引.jsphttp://localhost/myapp/ui/
  • http://localhost/myapp/ui/other和->其他.jsphttp://localhost/myapp/ui/other/

希望这有帮助!


推荐