【问题标题】:Basic Spring MVC project not serving view基本 Spring MVC 项目不提供视图
【发布时间】:2014-10-30 06:07:01
【问题描述】:

我在让我的 Spring MVC 项目为我的本地主机提供视图时遇到了一些麻烦。我使用 New -> Spring Project -> Spring MVC Project 选项从 Spring Tool Suite 中创建了一个项目。我根本没有修改此代码,因为我相信它应该可以按原样工作(但显然不是)。

这是我的项目结构和HomeController.java

这理论上应该会在我访问 localhost 时调出 home.jsp,而是调出基本的 Pivotal 服务器页面:

在我的HomeController.java 文件中,如果我将@RequestMapping(value) 更改为“/testing”,我会收到404 错误:

最后,这是我的ServletContext.xml(其中包含我的 ViewResolver 随 Spring MVC 项目模板一起提供):

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">

    <annotation-driven />

    <resources mapping="/resources/**" location="/resources/" />

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="pear.pear.pear" />
</beans:beans>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

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

</web-app>

我该如何解决这个问题?我究竟做错了什么?另外,顺便说一句,我注意到在许多其他 Spring 教程中他们使用 Application.java 驱动程序类 - 有必要吗?

非常感谢。

【问题讨论】:

  • @Darshan 刚刚编辑了我的帖子并添加了那个文件。
  • 您没有索引、默认或任何其他欢迎文件,因此当您触发 localhost:8080\testing 时,它找不到任何内容,因此出现错误。

标签: java spring spring-mvc


【解决方案1】:

您的应用程序可能未在 Tomcat (/) 的根 URL 上运行。默认值与项目名称相同,例如http://localhost:8080/hello/testing.

【讨论】:

  • 感谢您的回答!那么我假设 Spring 应用程序不需要 Application.java 驱动程序类是正确的吗?
  • 没有必要,如果您要部署到这样的外部容器中,则不使用。这是一个有用的选项(至少在开发时)。
【解决方案2】:

尝试 - 1: 将 Project-->Properties-->Web Project Settings 中定义的上下文根更改为“/”并重新启动服务器(这不起作用)

尝试 - 2: 找到 vfabric 服务器使用的 server.xml 并更改您的文档库的值“/”并重新启动服务器 viola !

截图

当使用 STS 创建 Spring MVC 项目时,该工具会自动分配您输入的包结构的最后几个字符,同时将顶层包创建为 Web 应用程序的上下文路径,如果您访问该 Web 项目,这很有趣使用 http://localhost:8080/pear 您将能够加载主页。

【讨论】:

  • 啊,多么愚蠢的错误!谢谢你。所以我认为 Application.java 驱动程序类是不必要的?
猜你喜欢
  • 2016-04-13
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 2018-04-01
  • 2013-03-05
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
相关资源
最近更新 更多