【问题标题】:URL Mapping issue - Spring web MVCURL 映射问题 - Spring Web MVC
【发布时间】:2012-07-27 17:37:20
【问题描述】:

我是 Spring 和 web MVC 模块的新手。基本上,我有以下几点:

web.xml

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

<servlet-mapping>
    <servlet-name>abc-dispatcher</servlet-name>
    <url-pattern>/user/*</url-pattern>
</servlet-mapping>

abc-dispatcher-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:context="http://www.springframework.org/schema/context"
   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="myPkg" />


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>        
</bean>

我有一个控制器,相关部分是:

@Controller
public class ABCController {

@RequestMapping("/user/welcome")
public String printWelcome(ModelMap model) {

    //code

}

现在每当我尝试访问http://localhost:8080/myapp/user/welcome

它给了我 404。

日志显示“将 url '/user/welcome' 映射到处理程序 'ABCController' 但未能在 DispatcherServlet 中映射 URI [/MYAPP/user/welcome] 名称为 'abc-dispatcher' .

完全糊涂了。我已经检查了我们两次指定映射的所有线程,但这里不是这种情况。我一定是错过了什么!

感谢您的帮助!

【问题讨论】:

    标签: spring


    【解决方案1】:

    网址应为http://localhost:8080/myapp/user/user/welcome。实际上,除非处理程序的 alwaysUseFullPath 属性设置为 true,否则 servlet-mapping 会附加到请求映射 URL 以形成完整路径。

    详情请见http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-handlermapping

    alwaysUseFullPath

    如果为 true ,Spring 使用当前 Servlet 上下文中的完整路径来查找合适的处理程序。如果为 false(默认值),则 使用当前 Servlet 映射中的路径。例如,如果一个 Servlet 使用 /testing/* 和 alwaysUseFullPath 属性进行映射 设置为 true,则使用 /testing/viewPage.html,而如果 属性设置为false,使用/viewPage.html。

    【讨论】:

    • 谢谢..这行得通:) 一个后续问题如果没问题..我在jsp中包含了css和js等静态资源,但url给出404。链接结果在/user/welcome/资源/css/abc.css 当我希望 url 看起来像 /resources/css/abc.css .. 任何指针?
    • 再问一个问题,告诉上下文路径是什么,资源存储在哪里,以及用于生成 URL 的代码。
    【解决方案2】:

    已将 context:component-scan 元素添加到示例上下文文件 sn-p 中,但没有 &lt;annotation-driven/&gt; 元素表明 spring 框架要查找带有 @Controller 注释的控制器

    【讨论】:

      【解决方案3】:

      对我来说,问题是我使用了已弃用的 DefaultAnnotationHandlerMapping,即使将 alwaysUseFullPath 设置为 true,它也没有生效,但是替换了 DefaultAnnotationHandlerMapping受益于 RequestMappingHandlerMapping 并将 alwaysUseFullPath 设置为 true 解决了问题。

      <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
          <property name="alwaysUseFullPath" value="true"></property>
      </bean>
      

      【讨论】:

        猜你喜欢
        • 2012-09-14
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        相关资源
        最近更新 更多