我是Spring和Web MVC模块的新手。基本上,我有以下内容:
web.xml
<servlet> <servlet-name>abc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>abc-dispatcher</servlet-name> <url-pattern>/user/*</url-pattern> </servlet-mapping>
abc-dispatcher-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: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="myPkg" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean>
我有一个控制器,相关的部分是:
@Controller public class ABCController { @RequestMapping("/user/welcome") public String printWelcome(ModelMap model) { //code }
现在每当我尝试访问 http://localhost:8080/myapp/user/welcome
它给我404。
日志说“将url’/ user / welcome’映射到处理程序’ABCController’,但是它未能在DispatcherServlet中将名称为’abc-dispatcher’的URI [/ MYAPP / user / welcome] 映射。
完全困惑。我已经检查了所有我们两次指定映射的线程,但是这里不是这种情况。我肯定错过了什么!
谢谢你的帮助!
网址应为http://localhost:8080/myapp/user/user/welcome。确实,除非将处理程序的alwaysUseFullPath属性设置为true,否则servlet映射将附加到请求映射URL之前以形成完整路径。
http://localhost:8080/myapp/user/user/welcome
alwaysUseFullPath
如果为true,则Spring使用当前Servlet上下文中的完整路径来查找适当的处理程序。如果为false(默认值),则使用当前Servlet映射中的路径。例如,如果使用/ testing / *映射Servlet,并且alwaysUseFullPath属性设置为true,则使用/testing/viewPage.html,而如果将该属性设置为false,则使用/viewPage.html。
/ testing / *