And*_*nko 5 java jsp spring-mvc http-status-code-404
我尝试配置简单的控制器.
我有:
在web.xml中
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
在mvc-dispatcher-servlet.xml中
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/index.jsp">
<ref bean="mainPage"/>
</entry>
</map>
</property>
</bean>
<bean name="mainPage" class="ru.mypack.TBController" />
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
public class TBController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
System.out.println("It is here");
ModelAndView model = new ModelAndView("index");
return model;
}}
Run Code Online (Sandbox Code Playgroud)
我在Tomcat 6上运行,在这个配置中它(/index.jsp)运行完美!
但是,如果我像这样更改url-pattern
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
它返回404访问/index.jsp
我在控制台中看到"它就在这里",这意味着url-pattern工作正常,但ModelAndView没有初始化
奇怪的是它看起来像是试图访问空资源(Chrome发挥不了我" HTTP状态404 - ")
请帮助我理解发生了什么..可能是我错过了url-pattern规范中的一些内容?
UPD:
感谢Pavel Horal,找到了解决方案.
我刚用web.xml替换了我的url-pattern
<url-pattern>/test/*</url-pattern>
Run Code Online (Sandbox Code Playgroud)
现在它通过/test/index.jsp响应
Spring正在处理如何定义servlet映射的信息。如果使用后缀映射(*.something),则Spring仅使用第一部分(不带后缀)。这意味着您仅/index在您的url模式(不带后缀)中将shuold映射。
Spring的JavaDoc UrlPathHelper#getPathWithinServletMapping更好地描述了映射过程中正在使用的内容:
返回给定请求的servlet映射内的路径,即,请求URL的超出调用servlet的部分,如果整个URL已用于标识servlet,则返回“”。
如果在RequestDispatcher包含中调用,则检测包含请求URL。
例如:servlet映射=“ / test / *”; 请求URI =“ / test / a”->“ / a”。
例如:servlet映射=“ / test”; 请求URI =“ / test”->“”。
例如:servlet映射=“ /*.test”; 请求URI =“ /a.test”->“”。
| 归档时间: |
|
| 查看次数: |
20004 次 |
| 最近记录: |