例如,如果您的 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.jsp
WEB-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/
希望这有帮助!