gpt4 book ai didi

Spring MVC 简单项目 - 404

转载 作者:行者123 更新时间:2023-11-28 22:33:54 26 4
gpt4 key购买 nike

我刚接触 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 的本地主机日志:

*

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

*

我已经筋疲力尽了,我从昨天开始就在为这个项目而战,请帮助我! :)

最佳答案

<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>

关于Spring MVC 简单项目 - 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15988552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com