【问题标题】:RequestMapping not working with multi-level URLsRequestMapping 不适用于多级 URL
【发布时间】:2011-09-14 18:04:02
【问题描述】:

我有一个场景,我通过一个链接发出一个简单的 get 请求,而我的 @RequestMapping 配置没有像我预期的那样运行。

在锚标记中,我使用以下模式引用 url '/action-plan/export/pdf?token=xxx&taskId=1111&taskId=2222...'

在我的控制器类中,我在类级别有这个映射:

@RequestMapping("/action-plan/export")

并且这个映射在方法级别

@RequestMapping(value="/pdf", method=RequestMethod.GET)
public String exportToPdf(@RequestParam("taskId") String[] taskIds,
            @RequestParam("token") String[] encryptedEmplId, ModelMap model)

但每次我尝试这个时,我都会收到 404 page not found 错误和以下 Spring 异常:

org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException:没有为 servlet 请求找到匹配的处理程序方法:路径 '/pdf',方法 'GET',参数映射 ['taskId' -> 数组 ['1962326' , '1962264', '1962317', '1962328', '1962324', '1962427', '1962325', '1962323', '1963147', '1962327', '1962318', '1962329', '1 'token' -> 数组['xxxx']]

我注意到当我删除“/pdf?”链接的一部分并从 @RequestMapping 方法中删除 'value="/pdf"' 它工作正常。对于我的一生,我不明白为什么将 /pdf 添加到 url 并且 RequestMapping 不起作用。

【问题讨论】:

  • 请为 DispatcherServlet 发布您的 web.xml servlet 映射
  • 我相信您只需将其更改为 @RequestMapping("/action-plan/export/*") 和不带斜线的 @RequestMapping(value="pdf" ettc)

标签: spring spring-mvc


【解决方案1】:

我认为 danny.lesnik 的答案非常接近,但我正在写我自己的答案,所以我可以更详细一些。

我正在做一个不同的项目,并弄清楚为什么上述方法不起作用。关于我原来的问题,这里是相关的 web.xml servlet 映射:

<servlet-mapping>
    <servlet-name>spring-dispatcherServlet</servlet-name>
    <url-pattern>/action-plan/export/*</url-pattern>
</servlet-mapping>

我注意到我在 web.xml 中包含的路径的任何部分都没有包含在 RequestMapping 值的评估中。我原以为这种 bean 配置会阻止这种情况(注意“alwaysUseFullPath”属性):

<bean id="annotationHandlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="order" value="2"/>
        <property name="alwaysUseFullPath" value="true"/>
</bean>

也许有人可以帮我解释一下这个细节。

无论如何,谢谢 danny.lesnik

【讨论】:

    【解决方案2】:

    我重新创建了您的问题并通过使用 .action 扩展映射 servlet 解决了它。

    例如:

    @Controller
    @RequestMapping(value="/test")
    public class DefaultController {
    
        @RequestMapping(value="/pdf.action", method=RequestMethod.GET)
        public ModelAndView indexView(@RequestParam("taskId") String[] taskIds,
                @RequestParam("token") String[] encryptedEmplId){
            ModelAndView mv = new ModelAndView("index");
        return mv;
        }
    

    Spring XML 映射:

    <context:annotation-config />
    <context:component-scan base-package="com.vanilla.controllers" />
    
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
    <value>/</value>
    </property>
    <property name="suffix">
    <value>.jsp</value>
    </property>
    </bean>
    

    还有这个 web.xml servlet 映射

     <display-name>SpringMvcServlet</display-name>
       <servlet>
        <servlet-name>SpringMvcServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>SpringMvcServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
      </servlet-mapping>
    

    这段代码解析了这个url

    /test/pdf.action?token=3&token=4&taskId=4

    完美无瑕。

    【讨论】:

    • 如果您在类级别的 url 中添加另一个层,例如 @RequestMapping(value="/test/level2"),您的解决方案是否有效?
    • 我正在从事另一个项目,并在某种程度上证实了您的回答。关于我原来的问题,这里是相关的 web.xml servlet 映射:&lt;servlet-mapping&gt; &lt;servlet-name&gt;spring-dispatcherServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/action-plan/export/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt;
    猜你喜欢
    • 2020-03-19
    • 2015-08-15
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 2020-02-23
    • 2023-03-12
    相关资源
    最近更新 更多