【发布时间】:2017-07-10 16:35:06
【问题描述】:
我是 Spring MVC 的新手。我正在尝试使用 Spring MVC 和 Tomcat 作为服务器创建一个简单的 Web 应用程序。但是当我部署我的项目时,我得到错误 404 not found。 请你帮助我好吗? 这是我的 web.xml
<web-app 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"
version="2.5">
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-web-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>/main/index</welcome-file>
</welcome-file-list>
</web-app>
这是我的控制器:
@Controller
@RequestMapping(value = "/main")
public class RootController {
@RequestMapping(value = "/index")
public String printHello(){
return "index";
}
}
这是我的 spring-web-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="com.controllers" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:annotation-driven />
</beans>
【问题讨论】:
-
服务器启动后可以访问tomcat服务器页面吗?通常
http://localhost:8080/如果可以去管理器应用程序并看到您的应用程序在那里列出。 -
@Rajith Pemabandu,不,我在那里看不到我的应用程序,我在localhost:8080 上什么也得不到
-
如果您只想让 Spring MVC Web 应用程序快速启动并运行,您是否考虑过 Spring Boot?您可以从 start.spring.io 快速设置项目。如果不能,请发布 tomcat 日志。
-
你也可以发布你的jsp文件吗?顺便说一句,您不需要在 Welcome-file-list 中列出 /main/index
标签: java spring spring-mvc tomcat