如何将Spring MVC控制器映射到带有和不带斜杠的uri? [英] How do I map Spring MVC controller to a uri with and without trailing slash?
问题描述
我有一个Spring Controller,它有几个RequestMappings用于不同的URI。我的servlet是ui。 servlet的基URI仅适用于尾部斜杠。我希望我的用户不必输入尾部斜杠。
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.
此URI有效:
http://localhost/myapp/ui/
这个不是:
http://localhost/myapp/ui
它给我一个HTTP状态404消息。
It gives me a HTTP Status 404 message.
来自我的web.xml的servlet和映射是:
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>
我的经理人:
@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);
}
}
推荐答案
如果您的Web应用程序存在于Web服务器的webapps目录中,例如 webapps / myapp /
,则可以在 http:// localhost:8080 / myapp /
假设默认的Tomcat端口。这应该使用或不使用尾部斜杠,我认为默认情况下 - 当然是Jetty v8.1.5中的情况
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
一旦你点击 / myapp
Spring DispatcherServlet 接管,将请求路由到 web中配置的
,在您的情况下是< servlet-name>
。 xml / ui / *
。
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/*
.
DispatcherServlet然后将来自 http:// localhost / myapp / ui /
的所有请求路由到 @Controller
s。
The DispatcherServlet then routes all requests from http://localhost/myapp/ui/
to the @Controller
s.
在 Controller 中你可以使用 @RequestMapping(value =/ *)
用于 mainPage()方法,这将导致 http:// localhost / myapp / ui /
和 http:// localhost / myapp / ui
被路由到 mainPage()。
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().
注意:你也应该使用Spring> = v3.0.3由于 SPR-7064
Note: you should also be using Spring >= v3.0.3 due to SPR-7064
完整性,这是我测试过的文件:
For completeness, here are the files I tested this with:
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
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
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>
以及 WEB-INF / views / index.jsp中的2个JSP文件
和 WEB-INF / views / other.jsp
。
结果:
-
http:// localhost / myapp /
- >目录列表 -
http:// localhost / myapp / ui
和http:// localhost / myapp / ui /
- > index.jsp -
http:// localhost / myapp / ui / other
和http :// localhost / myapp / ui / other /
- > other.jsp
http://localhost/myapp/
-> directory listinghttp://localhost/myapp/ui
andhttp://localhost/myapp/ui/
-> index.jsphttp://localhost/myapp/ui/other
andhttp://localhost/myapp/ui/other/
-> other.jsp
希望这会有所帮助!
这篇关于如何将Spring MVC控制器映射到带有和不带斜杠的uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!