【发布时间】:2013-11-29 08:08:39
【问题描述】:
我想在我的项目中使用 spring MVC 从一个页面导航到另一个页面。
我有两个 JSP 页面和一个控制器。 第一页是欢迎页面,当我使用 tomcat 启动项目时打开。 我的项目能够显示欢迎页面。但它找不到映射。
欢迎页面代码:
<a href="hello.html">Say Hello</a>
第二页代码:
<body> ${message} </body>
控制器代码:
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
System.out.println(message);
return new ModelAndView("hello", "message", message);
}
}
Servlet xml 代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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="net.viralpatel.spring3.controller" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
</beans>
网页xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<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>
</web-app>
请帮帮我。
【问题讨论】:
-
精确地定义发生的事情。如果您收到错误消息,请将其完全粘贴。
-
尝试将
@RequestMapping("/hello")更改为@RequestMapping("/hello.html")并在此之后为您的控制器类添加请求映射,例如 @RequestMapping("/helloWorld") 确保在浏览器中您的最终 URL 应该是像这样localhost:8080/appName/helloWorld/hello.html -
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>在你的配置中你已经给出了你所有的请求应该像 .html -
user230391 问题解决了吗??
标签: java xml spring jsp spring-mvc