2015-01-05 113 views
4

我有一個非常簡單的問題。當我在我的.jsp文件中有一個** /註冊的鏈接,方法viewRegistration被執行,一切工作正常。如果我有一個鏈接到**/registration/getTags?tagName = blahblah page is not found。我不知道爲什麼,因爲我認爲我的requestMapping註釋看起來是正確的......我將非常感謝您的幫助!請求映射 - Spring 4 - 不起作用

控制器:

@Controller 
@RequestMapping(value = "/registration") 
public class HelloController { 

    final static Logger logger = Logger.getLogger(HelloController.class); 

    @RequestMapping(method = RequestMethod.GET) 
    public String viewRegistration(Map<String, Object> model, HttpSession session) { 
     ... 
    } 

@RequestMapping(value = "/getTags", method = RequestMethod.GET) 
    public @ResponseBody 
    List<Tag> getTags(@RequestParam String tagName) { 

     .... 

    } 
} 

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" version="3.0"> 
    <display-name>aa</display-name> 
    <servlet> 
    <servlet-name>xxx</servlet-name> 
    <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>xxx</servlet-name> 
    <url-pattern>/registration/*</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/xxx-servlet.xml</param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app> 

XXX-servlet.xml中:

<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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:component-scan base-package="movies.controller" /> 
    <context:property-placeholder location="/WEB-INF/properties/website.properties" /> 
    <context:component-scan base-package="com" /> 
    <context:annotation-config /> 
    <mvc:annotation-driven /> 

    <bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" 
     id="tilesConfigurer"> 
     <property name="definitions"> 
      <list> 
       <value>/WEB-INF/tile/tilesJsp.xml</value> 
      </list> 
     </property> 
    </bean> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" 
     id="viewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.tiles3.TilesView" /> 
    </bean> 


    <bean id="messageSource" 
     class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename" value="website" /> 
    </bean> 


</beans> 

編輯編輯編輯: 我甚至嘗試某事更加簡單:

@RequestMapping(value = "/getTags") 
    @ResponseBody 
    public List<Tag> getTags() { 
     String tagName=""; 
     return simulateSearchResult(tagName); 

    } 

但仍然/ registration/getTags不起作用...,頁面未找到。

回答

3

如果你想處理請求**/registration/getTags,你的控制器方法映射更改爲

+0

這是正確的方式,只要我還記得。 – kazbeel

+0

不幸的是它不起作用,仍然沒有找到頁面 – user2455862

1

尼克·克倫威爾提到您的映射問題是你的要求,並在映射的參數名稱之間的不匹配。因此,無論是如他所提到的請求中的更改,在處理程序方法中重命名參數,還是使用@RequestParam註釋的屬性,例如,

List<Tag> getTags(@RequestParam(value="name") String tagName) 
+0

它不起作用,我之前嘗試過 – user2455862

5

servlet URL映射如下:

<servlet-mapping> 
    <servlet-name>xxx</servlet-name> 
    <url-pattern>/registration/*</url-pattern> 
    </servlet-mapping> 

這意味着,與/registration/*開頭的所有urls將由servlet處理,隨之而來後,它會由controller處理。 所以,如果你有一個@RequestMapping(value="/otherURL")配置控制器,將服務於以下Url

http://localhost:xxxx/<appname>/registration/otherURL

在這種情況下,以訪問正確的方法,你應該:

  • @RequestMapping("/registration)從控制器,導致它已被映射到servlet級別,並且調用:

    http://localhost:xxxx/<appname>/registration/getTags?tagName=blahblah將正常工作。 OR

  • 調用此URL:http://localhost:xxxx/<appname>/registration/registration/getTags?tagName=blahblah

1

使用requestParam如下:

List<Tag> getTags(@RequestParam("tagName") String tagName) 
1

更新您的網址

/registration/getTags/{blahblah} 

,並在控制器 -

@RequestMapping(value = "/getTags/{blahBlah}") 
public @ResponseBody List<Tag> getTags(@PathVariable String blahBlah) { 
    String tagName=""; 
    return simulateSearchResult(tagName); 

} 
1

我懷疑這是關係到視圖解析器配置,您使用的

org.springframework.web.servlet.view.UrlBasedViewResolver 

你只需要返回JSP頁面一個視圖解析器。在你的方法中,你試圖返回一個List對象,我相信你想在http正文中將其序列化爲JSON。

要返回的JSON響應,你可以嘗試在XML配置使用@JSONView

Spring @JsonView

並添加另一個ViewResolver的像org.springframework.web.servlet.view.json.MappingJacksonJsonView,如通過這個博客說明

multiple view resolvers

1

您在/註冊/ *

有servlet的URL映射

在課堂上你/註冊

具有映射在方法你有映射/ getTags

那麼,您的網址是/registration/registration/getTags?name=blahblah