此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); } }
解决方法
一旦你打了/ myapp,Spring DispatcherServlet接管,路由请求到< servlet-name>在您的web.xml中配置,在您的情况下是/ ui / *.
DispatcherServlet然后将所有请求从http:// localhost / myapp / ui /路由到@Controller.
在Controller本身中,您可以使用@RequestMapping(value =“/ *”)作为mainPage()方法,这将导致http:// localhost / myapp / ui /和http:// localhost / myapp / ui路由到mainPage().
注意:由于SPR-7064,您还应该使用Spring> = v3.0.3
为了完整,这里是我测试的文件:
的src /主/爪哇/控制器/ 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.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
希望这可以帮助!