Spring应用程序未在Tomcat中映射 [英] Spring application not mapping in Tomcat

查看:124
本文介绍了Spring应用程序未在Tomcat中映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我在某处设置有问题,但是由于尝试了多个教程和解决方案,我无法弄清楚哪里( stackoverlow问题

I'm convinced I have something setup wrong somewhere but I can't figure out where as I have tried following multiple tutorials and solution (viralpatel.net, stackoverlow question, another stackoverflow question) but I cannot deploy a simple HelloWorld Spring MVC to tomcat.

我已经在多台计算机(Windows 7和OS x 10.7)上尝试了多个教程,但最终遇到了相同的问题.我关注的主要教程是 mykong Spring 3 MVC你好世界示例,我什至尝试下载提供的源代码,但仍然无法使它正常工作(变得非常令人沮丧).

I have tried multiple tutorials on multiple machines (windows 7 and os x 10.7), and I end up with the same issue. The main tutorial I was following was the mykong Spring 3 MVC hello World example , I even tried to download the supplied source code and still cannot get it to work ( it is becoming very Frustrating).

我的服务器是apache-tomcat-6.0.36,它似乎可以正确部署到tomcat,但是我无法访问任何页面(我什至尝试仅将纯HTML页面放在根目录中,但仍然会出现404错误)但是,如果我在webapps目录中手动创建一个新文件夹"test"并在其中放置index.html,则localhost:8081/test/可以正常工作.我也尝试过Tomcat 7,但没有任何区别.

My Server is apache-tomcat-6.0.36 and it appears to deploy to tomcat correctly but I cannot get to any pages (I have even tried just putting a plain html page in the root but that still gives a 404 error) but if I manually create a new folder "test" in the webapps directory and put a index.html in there then localhost:8081/test/ works fine. I have also tried Tomcat 7 but made no difference.

这是我的项目结构:

这是我的web.xml:

this is my web.xml:

 <web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

这是我的mvc-dispatcher-servlet.xml

This is my mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        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">

    <context:component-scan base-package="com.mkyong.common.controller" />

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

</beans>

这是控制器:

package com.mkyong.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }

}

hello.jsp:

hello.jsp:

<html>
<body>
    <h1>Message : ${message}</h1>   
</body>
</html>

解决方案

正如duffymo所指出的,我的应用程序部署不正确,因此我右键单击了eclipse中的项目,并将其作为"WAR"文件导出到我的tomcat webapps目录中,并且现在可以正常使用

Solution

As duffymo pointed out, my application wasn't being deployed correctly, So I right clicked on the project in eclipse and exported as a "WAR" file to my tomcat webapps directory and it is working now

推荐答案

您没有正确打包和部署应用.

You are not packaging and deploying your app correctly.

我建议您将应用程序打包到适当的WAR文件中,并将其放在/webapps文件夹中,然后启动Tomcat.

I'd recommend that you package your app into a proper WAR file, put that in the /webapps folder, and start Tomcat.

如果该程序包名为foo.war,则您的URL将为:

If the package is named foo.war, your URL will be:

http://localhost:8080/foo/welcome

如果您的回答是我做到了,但没有成功",答案是您仍然做错了."

If your response is "I did that and it didn't work", the answer is "You still did it wrong."

这篇关于Spring应用程序未在Tomcat中映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆