Spring 3.1 MVC应用程序上的HTTP状态404
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring 3.1 MVC应用程序上的HTTP状态404,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5533字,纯文字阅读大概需要8分钟。
内容图文
![Spring 3.1 MVC应用程序上的HTTP状态404](/upload/InfoBanner/zyjiaocheng/697/bf3ea3db568b4dbd8e4316ff042a73fa.jpg)
我正在尝试创建并运行一个简单的Spring 3.1 mvc Web应用程序,其中我定义了一个控制器,它只使用下面的类在响应体中返回“hello”:
package com.jr.freedom.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Hello {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String helloWorldInJson() {
return "hello";
}
}
我试过这个URL,但我一直都回复404错误?
http://localhost:8080/FreedomSpring/hello
这是我的servlett:
<?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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<import resource="mvc-config.xml" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<context:component-scan base-package="com.jr.freedom.controllers"></context:component-scan>
</beans>
这是web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>FreedomSpring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FreedomSpring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>
最后是我的mvc-config.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<mvc:annotation-driven />
<!-- the application context definition for the springapp DispatcherServlet -->
<mvc:view-controller path="/" view-name="index.jsp" />
</beans>
顺便说一句,我在Windows 7机器上使用tomcat 7.我的日志文件没有从tomcat中找到错误:
13-Feb-2012 12:37:36 org.apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'FreedomSpring'
13-Feb-2012 12:37:36 org.apache.catalina.core.ApplicationContext log
INFO: Shutting down log4j
13-Feb-2012 12:37:36 org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
13-Feb-2012 12:37:37 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
13-Feb-2012 12:37:37 org.apache.catalina.core.ApplicationContext log
INFO: Set web app root system property: 'webapp.root' = [C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FreedomSpring\]
13-Feb-2012 12:37:37 org.apache.catalina.core.ApplicationContext log
INFO: Initializing log4j from [C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FreedomSpring\WEB-INF\log4j.xml]
13-Feb-2012 12:37:37 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'FreedomSpring'
0:0:0:0:0:0:0:1 - - [13/Feb/2012:12:37:45 +0000] "GET /FreedomSpring HTTP/1.1" 302 -
0:0:0:0:0:0:0:1 - - [13/Feb/2012:12:37:45 +0000] "GET /FreedomSpring/ HTTP/1.1" 404 997
0:0:0:0:0:0:0:1 - - [13/Feb/2012:12:37:51 +0000] "GET /FreedomSpring/hello HTTP/1.1" 404 1012
有任何想法吗?
解决方法:
您的DispatcherServlet映射到* .htm:
<servlet-mapping>
<servlet-name>FreedomSpring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
但是您通过不匹配的URL访问控制器:http://localhost:8080/FreedomSpring/hello.您有三种选择:
>将您的调度程序servlet更改为:
<servlet-mapping>
<servlet-name>FreedomSpring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
但在这种情况下,所有请求都将通过Spring MVC处理,包括图像和其他静态资源
>也可以在控制器中使用扩展:
<url-pattern>/*.htm</url-pattern>
并将Hello控制器映射到.htm,以便您使用:http://localhost:8080/FreedomSpring/hello.htm.
>将控制器映射到不同的子目录:
<url-pattern>/mvc/*</url-pattern>
要访问它们,您必须使用:http://localhost:8080/FreedomSpring/mvc/hello而不对控制器本身进行任何更改.
内容总结
以上是互联网集市为您收集整理的Spring 3.1 MVC应用程序上的HTTP状态404全部内容,希望文章能够帮你解决Spring 3.1 MVC应用程序上的HTTP状态404所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。