404返回Spring MVC中的index.htm

时间:2014-09-26 03:15:39

标签: spring spring-mvc

我为index.htm获得404。如您所见,index.htm已在服务器中注册。

服务器日志

[0m[0m10:00:57,643 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (ServerService Thread Pool -- 201) Mapped URL path [/index.htm] onto handler 'indexController'
[0m[0m10:00:57,644 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (ServerService Thread Pool -- 201) Root mapping to handler 'indexController'
...
[0m[0m10:07:31,827 INFO  [stdout] (http-/0.0.0.0:8080-1) Error message=404 returned for /EAFUIWeb/index.htm with message Not Found : [Fri Sep 26 10:07:31 ICT 2014]Not Found

控制器

@Controller
public class IndexController {
    @RequestMapping(value={"/index.htm","/"}, method = RequestMethod.GET)
    public ModelAndView initGET(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ...
    }

    @RequestMapping(value={"/index.htm","/"}, method=RequestMethod.POST)
    public ModelAndView loadEntity(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ..
    }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
...
</web-app>

的index.jsp

<c:redirect url="/index.htm" />

的applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sec="http://www.springframework.org/schema/security" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">


<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:sec="http://www.springframework.org/schema/security" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/mvc  
       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
       http://www.springframework.org/schema/security 
       http://www.springframework.org/schema/security/spring-security-3.2.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <sec:global-method-security
        pre-post-annotations="enabled" proxy-target-class="true" />

    <mvc:annotation-driven />
    <mvc:resources mapping="/config/**" location="/config/" />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:resources mapping="/tmpls/**" location="/tmpls/" />
    <mvc:resources mapping="/media/**"
        location="file:${user.home}/uploads/avatar/" />

    <context:component-scan base-package="com.bara.j2ee.pattern.control.spring,com.master.util" />

    <mvc:default-servlet-handler/>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <context:property-placeholder location="classpath:ad.properties"
        ignore-unresolvable="true" />

有配置问题吗?如何为index.htm修复404

3 个答案:

答案 0 :(得分:1)

要解决这些404问题,您需要了解Spring如何查找和加载JSP。

简短描述...... 在这种情况下调用/index.htm时,DispatchServlet将查找具有/index.htm映射并调用该方法的方法。该方法将返回一些东西。在这种情况下,您应该返回ModelAndView。类似的东西:

modelAndView.setViewName("myPage");
return modelAndView;

然后Spring进入ViewResolvers列表,按顺序,它将检查它可以找到的位置&#34; myPage&#34;。

同样,在你的情况下,因为你只有一个,它会寻找/WEB_INF/jsp/myPage.jsp(p:prefix =&#34; / WEB-INF / jsp /&#34; p:后缀=&#34; .JSP&#34;。)

您的问题似乎是Spring无法找到您的页面(我不知道,因为您没有发布您的initGET方法正文。

确保无论你设置/返回什么,都可以找到/ WEB_INF / jsp /并以.jsp结尾

答案 1 :(得分:0)

谢谢大家。我已经弄清楚了。这是多个viewresolver问题。我没有index.jsp文件,但是另一个jsp服务于/config/index.jsp,我在spring-view.xml中添加了。根据mkyong,这个顺序应该保留。

<bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location">
        <value>/WEB-INF/springapp-view.xml</value>
    </property>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

spring-view.xml

<bean id="index" class="org.springframework.web.servlet.view.JstlView">
    <property name="url" value="/config/index.jsp" />
</bean>

参考: http://www.mkyong.com/spring-mvc/configure-multiple-view-resolvers-priority-in-spring-mvc/

答案 2 :(得分:0)

要解决此答案,您需要将/更改为/*.htm 我认为这是问题所在。