【问题标题】:Help with first spring MVC page using annotations使用注释帮助第一个 Spring MVC 页面
【发布时间】:2010-02-09 15:44:14
【问题描述】:

我正在尝试让http://localhost:8080/test 映射到 index.jsp 页面。

jsp文件存放在文件夹中:

/web-inf/jsp/index.jsp

到目前为止我所拥有的:

@Controller
public class HomeController {


    @RequestMapping(value = "/test")
    public String Test(){

        return "index";
    }



}

我的 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">

        <!-- Handles all requests into the application -->
    <servlet>
        <servlet-name>springmvc1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Maps all /app requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springmvc1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

还有我的 springmvc1-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    - DispatcherServlet application context for PetClinic's web tier.
-->
<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.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">



    <context:component-scan base-package="com.example.web"/>



    <!--
        - The BeanNameViewResolver is used to pick up the visits view name (below).
        - It has the order property set to 2, which means that this will
        - be the first view resolver to be used after the delegating content
        - negotiating view resolver.
     -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1"/>
    <!--

        - This bean configures the 'prefix' and 'suffix' properties of
        - InternalResourceViewResolver, which resolves logical view names
        - returned by Controllers. For example, a logical view name of "vets"
        - will be mapped to "/WEB-INF/jsp/vets.jsp".
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"
            p:suffix=".jsp" p:order="2"/>



</beans>

我收到错误:

HTTP Status 404 - Servlet springmvc1 is not available

type Status report

message Servlet springmvc1 is not available

description The requested resource (Servlet springmvc1 is not available) is not available.

可能是什么问题?

Apache Tomcat/6.0.24

这是服务器给出的输出:

Feb 9, 2010 11:14:03 AM 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\Java\jdk1.6.0_17\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;E:\Program Files\CodeSmith\v4.1\;C:\WINDOWS\system32\WindowsPowerShell\v1.0;E:\Program Files\QuickTime\QTSystem\;E:\Program Files\TortoiseSVN\bin;C:\Documents and Settings\Administrator\My Documents\Downloads\nant-0.85-rc4-bin\nant-0.85-rc4;C:\Program Files\CodeSmith\v5.1\;%JAVA_HOME%\bin;%M2%;e:\program files\jetbrains\intellij idea 9.0.1\jre\jre\bin
Feb 9, 2010 11:14:03 AM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Feb 9, 2010 11:14:03 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 377 ms
Feb 9, 2010 11:14:03 AM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Feb 9, 2010 11:14:03 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.24
Feb 9, 2010 11:14:03 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor ROOT.xml
Feb 9, 2010 11:14:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory docs
Feb 9, 2010 11:14:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Feb 9, 2010 11:14:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory host-manager
Feb 9, 2010 11:14:04 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory manager
Feb 9, 2010 11:14:04 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Feb 9, 2010 11:14:04 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Feb 9, 2010 11:14:04 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/31  config=null
Feb 9, 2010 11:14:04 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 623 ms
Connected to server

更新tomcat日志

tomcat 日志文件好像没有立即更新,我猜几分钟就被转储了?

以下是问题:

Feb 9, 2010 11:14:03 AM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet  threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1484)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1329)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4187)

字符串是,我在 IntelliJ 的“外部库”文件夹中有 spring 3.0 rc3。此外,如果我输入 org.springframework.web.servlet.DispatcherServlet intellisense 会自动完成,我认为这意味着该库正在被作为项目的一部分。

【问题讨论】:

    标签: java servlets spring-mvc


    【解决方案1】:
    Servlet springmvc1 不可用

    归根结底

    HttpServlet springmvc1 = new DispatcherServlet(); 
    

    在服务器启动过程中失败。

    阅读服务器启动日志以获取与此相关的任何信息/警告/错误/详细信息。我在 ClassNotFoundExceptions 或 NoClassDefFoundErrors 上的分数,这反过来表明运行时类路径中缺少 JAR。

    【讨论】:

    • 是的,我试着寻找。我正在使用 IntelliJ IDEA,“tomcat log”选项卡是空的,/tomcat/logs 也是空的。我一定是找错地方了?
    • intellij 控制台选项卡是怎么回事,是否显示上下文测试初始化​​失败?
    • @Teja 窗口顶部显示“输出”,不确定它是否是控制台。有关输出,请参阅上面更新的问题。
    • 你得到了一个ClassNotFoundException,完全符合我的预期。运行时类路径中缺少 Struts JAR 文件。从 Struts 主页下载它并放入 /WEB-INF/lib。就是这样。
    • 在 intellij 的模块设置中,转到应用程序的 Web 方面 -> 在右侧您将看到要打包的模块和库,确保您已选择部署所有库
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    相关资源
    最近更新 更多