请求映射 - Spring 4 - 不起作用

use*_*862 4 java spring jsp spring-mvc request

我有一个非常简单的问题.在我的.jsp文件中,我有**/registration的链接,方法viewRegistration被执行,一切正常.如果我有**/registration/getTags的链接?找不到tagName = blahblah页面.我不知道为什么,因为我认为我的requestMapping注释看起来是正确的......我将非常感谢你的帮助!

控制器:

@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) {

        ....

    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

编辑编辑:我甚至尝试过更简单:

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

    }
Run Code Online (Sandbox Code Playgroud)

但仍然/ registration/getTags不起作用...,找不到页面.

Raf*_*LDI 5

servletURL映射如下:

 <servlet-mapping>
    <servlet-name>xxx</servlet-name>
    <url-pattern>/registration/*</url-pattern>
  </servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

这意味着所有urls开头/registration/*servlet东西都会被它所处理,它将由它来处理controller.因此,如果您使用控制器配置控制器,@RequestMapping(value="/otherURL") 它将提供以下服务Url:

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

在这种情况下,为了访问正确的方法,您应该:

  • @RequestMapping("/registration)来自控制器,因为它已经映射到servlet级别,并调用:

    http://localhost:xxxx/<appname>/registration/getTags?tagName=blahblah会正常工作. 或者:

  • 请拨打以下网址: http://localhost:xxxx/<appname>/registration/registration/getTags?tagName=blahblah