1

我是 Spring 新手,我正在尝试创建一个简单的项目,但我无法弄清楚!

我有最新的 java JDK 7。

Apache Tomcat 7 是最新的。

Spring 3 框架/Eclipse JUNO 4 集成。

是我的项目结构(对不起,我没有 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

*

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

4

6 回答 6

0

10 次中有 9 次在 Spring MVC 中看到 404 错误,这通常是因为我粗手指了控制器返回的视图名称/路径。

于 2013-04-15T12:32:48.353 回答
0
<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>
于 2013-04-13T14:08:12.913 回答
0

“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
于 2013-04-14T16:32:51.177 回答
0

这可能是由于缺少所需的库而导致的。以下是 spring 包含在其模板项目中的库列表(库版本可能不同)。

SpringMVC模板项目中的默认库列表

继续前进时,您可能会体验到复制库是痛苦的,必须有更好的方法来做到这一点。

我准备了一篇关于如何借助 eclipse 的 spring toolsuit 插件轻松设置 springMVC 项目的博文。希望它对您有帮助。

于 2013-04-14T11:34:31.777 回答
0

根据您的代码结构,一切看起来都很完美。
如果我是你,我会检查 System.out.println() 值以查看控制器是否真的被调用,然后我会确保 home.jsp 的位置是正确的。

于 2015-05-28T02:26:48.077 回答
-1

Try changing the request mapping of controller method to :

@Controller
public class HomeController {

    @RequestMapping(value = "home")
    public String home() {
        System.out.println("HomeController: Passing through...");
        return "home";
    }
}

Remove / from /home.

Also your web.xml does not seem to have context listener.

<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>
于 2013-04-14T07:29:13.983 回答