2014-10-01 31 views
2

我是網頁開發人員以及Spring MVC的新手。我正在測試控制器的一個愚蠢的例子,但是,似乎控制器從來沒有被調用過,或者至少它沒有按預期工作。Spring MVC:如何獲取控制器的正確URL?

目標是在「test.jsp」頁面中單擊一個超鏈接,並直接調用「customers.jsp」頁面,同時調用控制器,以便將消息傳遞給「customers.jsp」頁。

現在我的「customers.jsp」頁面正確顯示,但沒有顯示傳輸的消息。網址是http://localhost:8080/WebProject/view/customers.jsp

你能幫我找出問題嗎?我想這是因爲URL映射到控制器。但是,有沒有一種方法可以檢查控制器映射的正確URL?謝謝!

末*更新*

這個項目的結構是這樣的:

--webapp |--view |--*.jsp |--WEB-INF |--*.xml

這裏是我的代碼:

test.jsp的

<a href='<c:url value="customers.jsp"/>'>show customers</a> 

或者

<a href="customers.jsp">show customers</a> 

都能正常工作。

customers.jsp

<h2>return: ${list}</h2> 

控制器:

@Controller 
@RequestMapping("/customers") 
public class MenuController { 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView listCustomers() { 
     ModelAndView model = new ModelAndView("customers"); 
     model.addObject("list", "controller: a list of customers"); 
     return model; 
    } 
} 

和配置:

的web.xml

<servlet> 
    <servlet-name>myDispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>myDispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

myDispatcher-servlet.xml中

<context:component-scan base-package="com.example.spring.controller" /> 
<context:component-scan base-package="com.example.spring.model" /> 

<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/view/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

更新那些誰這篇文章後可能會看到:

確實存在通過@javafan和@Beri指示URL映射的問題。但更改url後<a href="customers">show customers</a>我仍然收到錯誤404在http://localhost:8080/WebProject/customers。這實際上是由包掃描錯誤引起的,所以無法找到控制器。爲了解決這個問題,只需更改行中的 「myDispatcher-servlet.xml中」 文件:

<context:component-scan base-package="com.example.spring.controller" /> 
<context:component-scan base-package="com.example.spring.model" /> 

<context:component-scan base-package="controller" /> 
<context:component-scan base-package="model" /> 

希望它能幫助:)

回答

1

Javatan是正確的: 嘗試

<protocol>/<host>:<port>/WebProject/customers 
您定義的前綴和後綴

裏面的InternalResourceViewResolver bean定義,這些值將被用於查找尤爾jsp文件應用。與您的應用程序創建的網址沒有任何共同之處。

你的url路徑是在控制器內部定義的(不在InternalResourceViewResolver中)。 因此,在應用程序名稱之後,您正在使用在控制器內定義的路徑。

您也可以在方法:)定義@RequestMapping(「/用戶」)

檢查,如果你在你的bean定義行有,看起來像這樣:

<context:component-scan base-package="com.project.web.controllers"/> 

該定義將掃描com.project.web.controllers包(和子包)內的所有類以查找標記有@Controller註釋的類。您可以通過查詢更多在這裏: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html,章節:

  • 17.3.1 - 如何註冊控制器
  • 17.2 - 如何web.xml文件看起來像

我希望這將有助於。 確定,我已經運行本地例如用JSP: web.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" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0"> 

    <servlet> 
     <servlet-name>myDispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>myDispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> <!-- Here you have your spring bean definition --> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app> 

控制:

@Controller 
public class MenuController { 

    @RequestMapping(value="/customers", method = RequestMethod.GET) 
    public ModelAndView listCustomers() { 
     ModelAndView model = new ModelAndView("customers"); 
     model.addObject("list", "controller: a list of customers"); 
     return model; 
    } 
} 

調度 - servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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-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="com.programcreek.helloworld.controller" /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
</beans> 
+0

感謝您的回答!這個解釋確實有用。但是,當我將href更改爲「customers」時,我在url'WebProject/customers'處得到了404錯誤。任何建議? – Truely 2014-10-02 07:47:31

+0

首先我將web.xml改爲/改爲/*。這樣所有的路徑都將被映射,而不僅僅是/。 – Beri 2014-10-02 07:52:51

+0

其次你有像你這樣的代碼定義:。這裏讓我們假設你的控制器位於包my.app.controllers中。該行將掃描該軟件包中的所有類以搜索控制器。 – Beri 2014-10-02 07:54:42

0

你直接調用customer.jsp 。相反,您必須調用控制器,然後將該請求傳遞給customer.jsp。在HREF屬性中,您需要提及您的控制器中@RequestMapping註釋中給出的值。

對於更詳細的例子,請訪問我的博客Here

+0

你的博客是非常有幫助的。但是當我將href更改爲「customer」(這是@RequestMapping中給出的值)時,我在頁面'/ WebProject/customers'上收到錯誤404。看起來控制器從未被調用過。你有什麼主意嗎? – Truely 2014-10-02 12:52:27

1

您可以使用該代碼來獲得服務器的網址:

"<a href="${pageContext.request.contextPath}/customers.jsp">show customers</a>" 

而你映射到控制器/客戶

這樣的網址應該是:

http://localhost:8080/WebProject/customers/customers.jsp 
相關問題