首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

Spring MVC HTTP ERROR 404,该怎么处理

2012-06-07 
Spring MVC HTTP ERROR 404在学习Spring mvc 的时候遇到这个问题:请求能够到达Controller,但是Controller

Spring MVC HTTP ERROR 404
在学习Spring mvc 的时候遇到这个问题:

请求能够到达Controller,但是Controller在return的时候, 找不到对应的视图文件, 报错

HTTP ERROR 404

Problem accessing /views/index.jsp. Reason:

  NOT_FOUND

但是当我不配置Spring MVC 的时候 用localhost:8888/views/index.jsp 是可以打开这个页面的。

请各位不舍赐教。

web.xml 代码:

XML code
<?xml version="1.0" encoding="utf-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">        <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath*:/spring/*.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>        <listener>        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    </listener>        <servlet>        <servlet-name>spring</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>        <init-param>            <description>加载/WEB-INF/目录下的所有XML作为Spring MVC的配置文件</description>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/spring/spring-servlet.xml</param-value>        </init-param>    </servlet>        <servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>/*</url-pattern>    </servlet-mapping>         <filter>        <filter-name>encoding-filter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>encoding-filter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>            <welcome-file-list>        <welcome-file>index.html</welcome-file>    </welcome-file-list></web-app>


dispatcher-servlet.xml 代码:
XML code
<?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"    xmlns:oxm="http://www.springframework.org/schema/oxm"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd                                          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd                                          http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">    <!-- SpringMVC相关Bean配置 -->         <context:component-scan base-package="com.gae.controllers" />            <!-- View Resolver -->     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />         <property name="prefix" value="/views/" />         <property name="suffix" value=".jsp" />     </bean>     <bean id="viewNameTranslator"     class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>     <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>     </beans>  



Java code
package com.gae.controllers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HomeController {    @RequestMapping(value="/home")    public String Index(){        return "index";    }}


web根目录是:war

war
|-views
| |-index.jsp
|-WEB-INF

[解决办法]
localhost:8888/views/index.jsp ??

那应用的地址是多少?
[解决办法]
你没加静态文件访问,。jsp被拦截了
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<description>加载/WEB-INF/目录下的所有XML作为Spring MVC的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-servlet.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

两种方法解决:
1,你用的是/* ,你改成.do 或.htm 就好了;,
2.你在mvc配置文件里加静态文件访问
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/views/**" location="/views/" />

热点排行