我确信我在某个地方设置错误,但我无法弄清楚在哪里,因为我尝试了多个教程和解决方案( viralpatel.net , stackoverlow question , another stackoverflow question ),但我无法部署一个简单的 HelloWorld Spring MVC 到 tomcat。
我在多台机器(Windows 7 和 os x 10.7)上尝试了多个教程,但最终遇到了同样的问题。我遵循的主要教程是 mykong Spring 3 MVC hello World example ,我什至尝试下载提供的源代码,但仍然无法让它工作(这变得非常令人沮丧)。
我的服务器是 apache-tomcat-6.0.36,它似乎正确部署到 tomcat,但我无法访问任何页面(我什至尝试在根目录中放置一个纯 html 页面,但仍然给出 404 错误)但如果我在 webapps 目录中手动创建一个新文件夹“test”并在其中放入一个 index.html,那么 localhost:8081/test/就可以正常工作。我也尝试过 Tomcat 7,但没有什么区别。
这是我的项目结构:
这是我的 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
<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>
这是 Controller :
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";
}
}
你好.jsp:
<html>
<body>
<h1>Message : ${message}</h1>
</body>
</html>
解决方案
正如 duffymo 指出的那样,我的应用程序没有正确部署,因此我在 eclipse 中右键单击该项目并将其作为“WAR”文件导出到我的 tomcat webapps 目录,现在它正在运行
最佳答案
您没有正确打包和部署您的应用程序。
我建议您将应用程序打包到适当的 WAR 文件中,将其放入/webapps 文件夹中,然后启动 Tomcat。
如果包名为 foo.war,您的 URL 将为:
http://localhost:8080/foo/welcome
如果您的回答是“我这样做了,但没有成功”,那么答案是“您仍然做错了。”
关于maven - Spring应用程序未映射到Tomcat中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16299066/