2013-02-18 73 views
2

我是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); 
     } 
    } 
+0

嘗試也許Say Hello,不知道壽。 – fonZ 2013-02-18 21:51:16

+0

你有jsp文件有任何views.xml文件嗎? – fonZ 2013-02-18 21:52:05

+0

要開始試圖改變自己的redirect.jsp中的內容白衣可點擊的鏈接: 春3.0 MVC系列:首頁 - ViralPatel.net Say Hello 嘗試,並說,如果它的工作......如果這個版本的工作就意味着問題在於自動重定向,而不是在你的應用程序邏輯 – AndreaNobili 2013-02-18 21:54:57

回答

0

可能有其他方法可以做到這一點,但我就是這樣學會了,用查看豆類。 會發生什麼情況如下:

  1. 用戶點擊含有某個名字即www.yoursite.com/ 你好
  2. 春天的鏈接查找在requestmappings「你好」。
  3. 如果找到了requestmapping,則執行相應的方法。
  4. 該方法返回一個ModelAndView,該ModelAndView包含必須在與您的jsp文件位於相同位置的views.xml中定義的url(WEB-INF/jsp/hello.jsp)的別名(hello)。
  5. 最後,來自別名hello的url(WEB-INF/jsp/hello.jsp)的頁面被顯示給用戶。

views.xml定義了不同jsp文件的url的別名。所以在那個文件中你說你好對應於WEB-INF/jsp/hello.jsp

它看起來沿着這些路線的東西:

<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 id="excelViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> 
    <property name="order" value="1"/> 
    <property name="location" value="/WEB-INF/views.xml"/> 
</bean> 

<!-- in views.xml --> 

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

<bean id="hello" class="org.springframework.web.servlet.ModelAndView"> 
    <property name="hello" value="/WEB-INF/jsp/hello.jsp" /> 
</bean> 

</beans> 

的requestmapping在你的控制器看起來是一樣的:

@Controller 
public class HelloWorldController { 

    @RequestMapping("hello") // what the user calls for, the request 
    public ModelAndView helloWorld() { 
     String message = "Hallo Asgard"; 
     return new ModelAndView("hello", "message", message); // the response 
     //"hello" in ModelAndView is the same "hello" as the one in views.xml 
    } 
} 

我希望可以幫助你。

+0

我以前沒有聽說過views.xml文件。從你寫的內容來看,你所描述的內容聽起來像是註釋和viewResolver應該做的事情?無論如何,我嘗試在jsp filder中添加views.xml文件,但得到了相同的結果。順便說一句,我在網上或教程中看到的例子都沒有使用views.xml文件。但值得一試。 :-) – user1777296 2013-02-19 01:08:30

+0

@ user1777296對不起,我忘了告訴你,你需要XmlViewResolver。 – fonZ 2013-02-19 11:14:40

+0

您還可以在http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html中詳細解釋。 – fonZ 2013-02-19 11:18:24

1

一個新的bean添加到調度員servlet.xml中

<bean id="helloWorldController" 
    class="net.mvp.spring3.controller.HelloWorldController" /> 

與更換ID urlMapping中你的bean下面的代碼給出

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
<property name="mappings"> 
    <props> 
     <prop key="index.htm">indexController</prop> 
     <prop key="hello.htm">helloWorldController</prop> 
    </props> 
</property> 

注:沒有必要更換@RequestMapping("hello")

+0

這變得越來越令人沮喪。我嘗試修改RequestMapping,但它沒有任何不同。錯誤消息是相同的。它說WARNING:在名爲'dispatcher'的DispatcherServlet中找不到具有URI [/WTSpring3/hello.htm]的HTTP請求的映射。這是我堅持的部分。我的理解是Controller/RequestMapping部分爲任何以「hello」開頭的url映射到該函數創建映射。我看到的每一個例子都會有所不同,所以如果我試圖從不同的例子中拉出一些片段,那麼它就不起作用。 – user1777296 2013-02-19 04:46:42

+0

查看最新的答案 – Kris 2013-02-19 09:33:24

0

你可以改變hello.htm文件名hello2.jsp或類似的東西?我的意思是,嘗試從請求映射中使用不同的名稱。

@Controller 
public class HelloWorldController { 

    @RequestMapping("hello") // what the user calls for, the request 
    public ModelAndView helloWorld() { 
     String message = "Hallo Asgard"; 
     return new ModelAndView("hello2", "message", message); // the response 
     //"hello" in ModelAndView is the same "hello" as the one in views.xml 
    } 
} 

hello2.jsp

<html> 
<body>${message}</body> 
</html>