【发布时间】:2010-12-21 08:05:04
【问题描述】:
嗨 我正在学习 Spring 3 的第一堂课。我在 Eclipse 中创建了一个具有以下结构的动态 Web 应用程序。
spring3mvc \src\my.spring.HelloWorldController.java
\WebContent
|
|-----WEB-INF\jsp\hello.jsp
|-----index.jsp
|-----WEB-INF\web.xml
|-----WEB-INF\spring-servlet.xml
|-----WEB-INF\lib\...*.jar files
我创建了如下的 spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/context
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="my.spring" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp">
<property name="contentType" value="text/html; charset=utf-8" />
</bean>
</beans>
并对控制器进行编码
package my.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
index.jsp 有一个指向 hello 视图的链接
<html>
<body>
<a href="hello.html">Say Hello</a>
</body>
</html>
我终于在 hello.jsp 中放了
<html>
<body>
${message}
</body>
</html>
我的 web.xml 有
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
当我在 Tomcat6 服务器(通过 Eclipse)上运行应用程序时,我可以在以下位置看到索引页面 http://localhost:8080/Spring3MVC/ 。它显示到 hello 页面的链接。当我点击它时(http://localhost:8080/Spring3MVC/hello.html),我得到一个 404 错误。
message /Spring3MVC/hello.html
description The requested resource (/Spring3MVC/hello.html) is not available.
知道如何解决这个问题吗?
谢谢
标记。
【问题讨论】:
-
我有一个想法,请检查我的增强答案。
-
添加
后是否出现映射消息? -
感谢 Ralph 始终如一的帮助。我能够让应用程序运行.. xsi:schemaLocation 中的条目顺序存在一些问题,但我能够把事情做好。非常感谢到 org.life.java 获取详细的配置信息和建议..
-
主要错误是 web.xml 位于 WebContent 文件夹中,而不是 WebContent/WEB-INF。这很愚蠢..在学习教程时应该更加小心
标签: java spring spring-mvc