【发布时间】:2014-10-30 06:07:01
【问题描述】:
我在让我的 Spring MVC 项目为我的本地主机提供视图时遇到了一些麻烦。我使用 New -> Spring Project -> Spring MVC Project 选项从 Spring Tool Suite 中创建了一个项目。我根本没有修改此代码,因为我相信它应该可以按原样工作(但显然不是)。
这是我的项目结构和HomeController.java
这理论上应该会在我访问 localhost 时调出 home.jsp,而是调出基本的 Pivotal 服务器页面:
在我的HomeController.java 文件中,如果我将@RequestMapping(value) 更改为“/testing”,我会收到404 错误:
最后,这是我的ServletContext.xml(其中包含我的 ViewResolver 随 Spring MVC 项目模板一起提供):
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="pear.pear.pear" />
</beans:beans>
还有我的 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
</web-app>
我该如何解决这个问题?我究竟做错了什么?另外,顺便说一句,我注意到在许多其他 Spring 教程中他们使用 Application.java 驱动程序类 - 有必要吗?
非常感谢。
【问题讨论】:
-
@Darshan 刚刚编辑了我的帖子并添加了那个文件。
-
您没有索引、默认或任何其他欢迎文件,因此当您触发
localhost:8080\testing时,它找不到任何内容,因此出现错误。
标签: java spring spring-mvc