【问题标题】:Deploying to tomcat via eclipse and maven2 plugin doesn't seem to be working通过 eclipse 和 maven2 插件部署到 tomcat 似乎不起作用
【发布时间】:2011-01-15 08:57:34
【问题描述】:

我有一个简单的 spring 3 mvc 应用程序,当有人浏览到 http://localhost:8080/ 时它只会输出 index.aspx

当我执行 RunAs 并在服务器选项上运行时(与 tomcat 6 挂钩),它会打开浏览器到 http://localhost:8080/springmvc2/(其中 springmvc2 是应用程序名称)。

我已经使用 netbeans 构建了同样的简单测试应用程序,使用 url http://localhost:8080/ 可以正常工作

我的 web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Springmvc2</display-name>
    <description>Springmvc2 web application</description>

    <servlet>
        <servlet-name>Springmvc2</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>Springmvc2</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

而我的 HomeController 是:

package com.springmvc2.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;



@Controller
public class HomeController {


    @RequestMapping("/")
    public String Index(){

        return "index";
    }


    @RequestMapping("/test")
    public String Index2(){

        return "index";
    }


}

你可以在这里查看我的 pom.xml 文件:Using eclipse with maven plugin, how should I setup my build so it deploys to tomcat?

这也是生成的目标文件夹:

/target
/target/springmvc2/meta-inf
/target/springmvc2/web-inf
/target/springmvc2/test.html
/target/war/springmvc2.war
/target/pom.xml

我创建 test.html 只是为了看看是否至少可以渲染,但 http://localhost:8080/test.html 不起作用,http://localhost:8080/springvc2/test.html 也不起作用。

我收到 404 错误,所以我猜它没有正确部署到 tomcat,尤其是因为 test.html 文件甚至没有呈现。

但也许 test.html 没有呈现,因为我的 springmvc2-servlet.xml 中有这个:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"
            p:suffix=".jsp" p:order="2"/>

我不得不承认,Java 开发充斥着配置问题,尤其是对于新手来说!!

更新

编译并执行 runas 服务,打开浏览器,我从 tomcat 收到此消息:

WARNING: No mapping found for HTTP request with URI [/springmvc2/] in DispatcherServlet with name 'springmvc2'

【问题讨论】:

    标签: java spring tomcat maven-2 spring-mvc


    【解决方案1】:

    网址应为http://localhost:8080/springmvc2/test.html(您粘贴的网址中缺少m)。

    更新:试试&lt;url-pattern&gt;/*&lt;/url-pattern&gt;而不是&lt;url-pattern&gt;/&lt;/url-pattern&gt;

    【讨论】:

    • 但是当我在 netbeans 中进行测试时,为什么 localhost:8080 工作正常?
    • @Blankman 因为 webapp 在 NetBeans 中部署在根上下文 / 下,而在 Eclipse 下部署在 /springmvc2 下。
    • @Blankman 在服务器视图中,双击 Tomcat 服务器,选择Modules 选项卡并Edit Web Module 来更改 Path
    • 好的,我做到了,但仍然无法正常工作,但至少我修复了该设置,谢谢!
    猜你喜欢
    • 2023-03-21
    • 2011-08-21
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    相关资源
    最近更新 更多