2014-09-26 89 views
0

我爲index.htm獲取404。您可以看到index.htm已在服務器中註冊。404在Spring MVC中爲index.htm返回

Server日誌

[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" /> 

是否有y配置問題?如何修復404 for index.htm

+0

你的春天背景在哪裏? – Desorder 2014-09-26 03:22:26

+0

爲什麼要進入「0.0.0.0:8080」? – 2014-09-26 03:22:34

+0

我在上面添加了spring上下文文件。 @Desorder – 2014-09-26 03:27:42

回答

0

謝謝大家。我已經想通了。它是多個viewresolver問題。我沒有index.jsp文件,但另一個jsp servces /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" /> 

春天。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/

1

要解決這些404問題,您需要了解Spring如何發現並加載您的JSP。

在一個簡短的說明... 你打電話時,在這種情況下,/index.htm,DispatchServlet將尋找具有/index.htm映射的方法,並調用該方法。該方法將返回一些東西。在這種情況下,你應該返回一個ModelAndView。像這樣:

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

然後Spring來到ViewResolvers列表,並按順序,它會檢查它可以找到「myPage」的位置。

再次,在你的情況下,因爲你只有一個,它會查找/WEB_INF/jsp/myPage.jsp(p:prefix =「/ WEB-INF/jsp /」p:suffix =「。jsp」 )。

您的問題似乎是春天無法找到你的頁面(這我不知道,你不發表您的initGET方法體。

確保,無論您是在設置/返回有可以發現/ WEB_INF/jsp /以.jsp結尾

0

爲了解決這個問題的答案,你需要改變你的/到/*.htm 我認爲這就是問題所在。