Spring MVC 简单项目 - 404
Posted
技术标签:
【中文标题】Spring MVC 简单项目 - 404【英文标题】:Spring MVC Simple Project - 404 【发布时间】:2013-04-05 23:53:43 【问题描述】:我是 Spring 新手,我正在尝试创建一个简单的项目,但我想不通!
我的 java JDK 7 是最新的。
Apache Tomcat 7 已更新。
集成了 Spring 3 框架/Eclipse JUNO 4。
HERE 这是我的项目结构(对不起,我没有 10 个声誉点的外部链接)
我的 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我的 servlet-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Scans within the base package of the application for @Components to configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="controller" />
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我的 HomeController.java 文件:
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController
@RequestMapping(value = "/home")
public String home()
System.out.println("HomeController: Passing through...");
return "home";
我的问题是,当我从浏览器调用 servlet 时(例如以这种方式):
http://localhost:8080/SpringMVC/home
我遇到了 HTTP 400 错误 - 说明请求的资源不可用。
我怀疑是库问题,但我将所有 Spring 库(以及更多)放在 WEB-INF/lib 中。在 Eclipse 项目中,显然我在类路径中添加了所有内容。
我粘贴 Tomcat 的 localhost 日志:
*
apr 13, 2013 3:44:43 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextDestroyed()
apr 13, 2013 3:44:43 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextDestroyed()
apr 13, 2013 3:44:47 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
apr 13, 2013 3:44:47 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
apr 13, 2013 3:44:47 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@189b904')
*
和 Tomcat strERR 日志:
*
2013-04-13 15:44:46 Commons Daemon procrun stderr initialized
apr 13, 2013 3:44:47 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\AMD APP\bin\x86;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files\NVIDIA Corporation\PhysX\Common;C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Windows Live\Shared;C:\Program Files\Autodesk\Backburner\;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;;.
apr 13, 2013 3:44:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
apr 13, 2013 3:44:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 397 ms
apr 13, 2013 3:44:47 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
apr 13, 2013 3:44:47 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.39
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\.metadata
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\docs
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\examples
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\host-manager
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\manager
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\Servers
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\SpringMVC
apr 13, 2013 3:44:47 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
apr 13, 2013 3:44:47 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 540 ms
*
我筋疲力尽,我从昨天开始就在为这个项目而战,请帮帮我! :)
【问题讨论】:
我也是从 Spring 开始的。但我希望这个link 也能帮助你。我在创建我的起始项目时使用了该链接。 您访问的是什么网址?例如localhost:8080???? 【参考方案1】:根据您的代码结构,一切看起来都很完美。 如果我是你,我会检查 System.out.println() 值以查看控制器是否真的被调用,然后我会确保 home.jsp 的位置是正确的。
【讨论】:
【参考方案2】:10 次中有 9 次在 Spring MVC 中看到 404 错误,这通常是因为我粗手指了控制器返回的视图名称/路径。
【讨论】:
【参考方案3】:“C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\SpringMVC”目录中究竟是什么?有webapp目录结构吗?
根据stdErr日志,Tomcat似乎没有检测到你的appServlet(org.springframework.web.servlet.DispatcherServlet)。
如果您正确部署了 SpringMVC 应用程序,您应该会在日志文件中看到以下消息:
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
...
...
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 725 ms
【讨论】:
【参考方案4】:这可能是由于缺少所需的库引起的。以下是 spring 包含在其模板项目中的库列表(库版本可能不同)。
在继续前进时,您可能会感到复制库很痛苦,必须有更好的方法来做到这一点。
我准备了一个blog post,关于如何在eclipse的spring toolsuit插件的帮助下轻松设置springMVC项目。希望对你有很大帮助。
【讨论】:
【参考方案5】:尝试将控制器方法的请求映射更改为:
@Controller
public class HomeController
@RequestMapping(value = "home")
public String home()
System.out.println("HomeController: Passing through...");
return "home";
从/home
中删除/
。
你的web.xml
似乎也没有上下文监听器。
<context-param>
<param-name>contextConfigLocation</param-name><param-value>
/WEB-INF/spring/appServlet/spring-security.xml,
/WEB-INF/spring/appServlet/hibernate-config.xml
</param-value>
</context-param>
<!-- you seem to be missing this -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
【讨论】:
【参考方案6】:<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
【讨论】:
您的设置看起来正确,即使用正确的 xml 文件。你确定tomcat在8080上吗? 是 8080。当我调用 localhost:8080 时启动默认的 Tomcat 页面 可能值得在 erequets 映射中指定“method = RequestMethod.GET”,尽管我确信这是默认设置。我认为该错误来自 jsp 不可用,而不是请求映射失败。项目是否正在正确构建和部署 - jsp 部署在应该在 tomcat/webapps 内的位置 在 Tomcat 中,Java 类路径设置为:“C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\bootstrap.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\ bin\tomcat-juli.jar" - 正常吗? 应用部署成功,调高日志级别,以调试模式部署,检查消息,让用户在浏览器中进行硬刷新(ctrl+f5)以上是关于Spring MVC 简单项目 - 404的主要内容,如果未能解决你的问题,请参考以下文章
转转转![Spring MVC] - 500/404错误处理-SimpleMappingExceptionResolver
Spring-Boot MVC 模板未加载(未找到 404)
Mac intellj idea开发Spring MVC,404网页错误
Spring mvc annotation配置。升级到弹簧4.3.9时出错404
Spring 4 MVC,警告:在 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射,名称为 [...] 和 HTTP 404 错误