我是Spring的新手,看過無數的例子,無法弄清楚這個問題。我的問題是,當我點擊從我的index.jsp頁面的鏈接時,它不會轉發到hello.jsp頁面。我不知道爲什麼。我花了2天時間查找例子,並試圖通過無數例子來確定問題是無濟於事的。我希望你能告訴我問題是什麼。我使用的是NetBeans 7.2,Tomcat 7,JDK 7,Spring 3.2.1。 redirect.jsp用於訪問index.jsp頁面,但單擊index.jsp頁面上的鏈接會給我一個404錯誤。Spring新手無法讓控制器工作
我瞭解它的方式,歡迎文件是redirect.jsp(不知道這個文件是如何處理的,爲什麼它不通過調度程序 - 因爲它是一個jsp?)。這將重定向到一個htm文件。調度程序找到一個匹配index.htm的url映射,並調用indexController,它發送到「index.html」視圖,即「/ WEB-INF/jsp /」+ index +「.jsp」。
當Tomcat啓動時我看到以下消息:
Mapped URL path [/helloworld] onto handler 'helloWorldController'
Mapped URL path [/helloworld/*] onto handler 'helloWorldController'
Mapped URL path [/index.htm] onto handler 'indexController'
單擊在index.jsp中的鏈接提供了以下警告消息:
No mapping found for HTTP request with URI [/WTSpring3/hello.htm]
我的網頁文件奠定了如下:
web
WEB-INF
jsp
hello.jsp
index.jsp
applicationContext.xml
dispatcher-servlet.xml
web.xml
redirect.jsp
redirect.jsp中
<% response.sendRedirect("index.htm"); %>
的index.jsp(這是一個我永遠無法得到工作。我不確定是否有鏈接不正確的彈簧設置)。
<html><body>
<a href="hello.htm">Say Hello</a>
</body></html>
的hello.jsp
<html>
<body>${message}</body>
</html>
web.xml中 - 注意他在這裏的url-pattern是的* .htm,這是我在index.jsp中使用。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
調度-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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<context:component-scan base-package="net.mvp.spring3.controller" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
HelloWorldController.java
package net.mvp.spring3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("hello")
public ModelAndView helloWorld() {
String message = "Hallo Asgard";
return new ModelAndView("hello", "message", message);
}
}
嘗試也許Say Hello,不知道壽。 – fonZ 2013-02-18 21:51:16
你有jsp文件有任何views.xml文件嗎? – fonZ 2013-02-18 21:52:05
要開始試圖改變自己的redirect.jsp中的內容白衣可點擊的鏈接: