【问题标题】:Strange behavior in the creation of new Spring MVC project using STS template project, the URL is not correct and I have to rebuild the project使用 STS 模板项目创建新 Spring MVC 项目的奇怪行为,URL 不正确,我必须重建项目
【发布时间】:2012-12-03 11:50:26
【问题描述】:

我对正确创建 Spring MVC 项目有疑问。

当我运行导入的项目(例如可从 STS 仪表板下载的 Spring MVC Showcase 示例)时,在浏览器 URL 栏中,该项目的 URL 如下:

http://localhost:8080/project-name/ 

(例如,Web MVC Showcase 示例具有项目名称:“spring-mvc-showcase”并具有以下 URL:

http://localhost:8080/spring-mvc-showcase/ )

现在,我的疑问与在 STS\Eclipse 中使用 Spring 模板创建新的 Spring MVC 项目有关。

我在STS里面做了如下操作:

文件 --> 新建 --> 项目,在我看来是一个向导,我可以在其中选择项目类型。

所以我选择了 Spring 模板项目,现在出现在我的另一个向导窗口中,我可以在其中选择 Spring 项目的特定类型,我选择了 “Spring MVC 项目”

好的,现在我需要在其中设置另一个向导窗口:

项目名称,然后我插入:my-spring-project

顶级包,我插入了:org.mycompany.foo

好的,现在我在服务器上运行我的项目,这不运行,并且在浏览器的 URL 栏中我没有

http://localhost:8080/my-spring-project/ (as I would expect) 

但我有:

http://localhost:8080/foo/

在堆栈跟踪中我有以下错误消息:

警告:org.springframework.web.servlet.PageNotFound - 在名为“appServlet”的 DispatcherServlet 中找不到具有 URI [/foo/] 的 HTTP 请求的映射

然后,如果我对我的项目进行项目清理,STS 会重建项目,如果我再次尝试运行它,项目运行良好并且 URL 地址仍然存在:

http://localhost:8080/foo/

我认为这是一种奇怪的行为,我不知道这是什么原因......

所以:

1) 为什么网址不是

http://localhost:8080/my-spring-project/ 

?

2) 为什么我必须清理并重建我的项目才能运行它?

3) 我在创建标准 Spring MVC 项目时出错了?

【问题讨论】:

  • 你在哪个 servlet 容器上运行?
  • @abalogh vmWare vFabric 是一个拥有更多东西的 Tomcat,为什么?
  • 你能发布你的 web.xml,至少是相关部分吗?
  • @abalogh web.xml 在我的新答案中发布
  • stackoverflow.com/questions/7285162/… -> 可能想看看这是否有帮助

标签: java eclipse spring spring-mvc sts-springsourcetoolsuite


【解决方案1】:

Eclipse > 属性 > Web 项目设置:设置为上下文根my-spring-project 然后像在 Eclipse 中正常运行一样运行项目。它应该以 http://localhost:8080/my-spring-project/ 开头。也像展示项目一样进行映射:<mvc:view-controller path="/" view-name="home"/>

【讨论】:

    【解决方案2】:

    这是我的 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">
    
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 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>
    

    【讨论】:

    • 如果您想添加更多细节,您应该扩展您的问题,而不是将其作为答案发布!
    【解决方案3】:

    我想你忘记在你的服务器上部署新项目了。

    • 1) 打开“服务器”视图(Ctrl+3,键入“服务器”)
    • 2) 右键单击​​您的服务器实例(例如“本地主机上的 tomcat 7”)
    • 3) 选择“添加和删除...”
    • 4) 从“available”中选择“my-spring-project”并“add>”它们
    • 5) 启动服务器并访问“localhost:8080/my-spring-project/”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多