Spring 3 简单的无扩展 url 映射与基于注释的映射 - 不可能吗? [英] Spring 3 simple extentionless url mappings with annotation-based mapping - impossible?

查看:19
本文介绍了Spring 3 简单的无扩展 url 映射与基于注释的映射 - 不可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring 3,并尝试使用注释来定义控制器映射来设置一个简单的 Web 应用程序.如果不使用 *.form 或 *.do 填充所有网址,这似乎非常困难

I'm using Spring 3, and trying to set up a simple web-app using annotations to define controller mappings. This seems to be incredibly difficult without peppering all the urls with *.form or *.do

因为部分站点需要密码保护,所以这些url都在/secure下.web.xml 中有一个 保护该根目录下的所有内容.我想将所有 Spring 控制器映射到/secure/app/.

Because part of the site needs to be password protected, these urls are all under /secure. There is a <security-constraint> in the web.xml protecting everything under that root. I want to map all the Spring controllers to /secure/app/.

示例网址为:
/安全/应用程序/登陆页面
/secure/app/edit/customer/{id}
每个我都会用适当的 jsp/xml/whatever 处理.

Example URLs would be:
/secure/app/landingpage
/secure/app/edit/customer/{id}
each of which I would handle with an appropriate jsp/xml/whatever.

所以,在 web.xml 我有这个:

So, in web.xml I have this:

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

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/secure/app/*</url-pattern>
</servlet-mapping>

在 despatcher-servlet.xml 中我有这个:

And in despatcher-servlet.xml I have this:

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

在控制器包中,我有一个控制器类:

In the Controller package I have a controller class:

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/secure/app/main")
public class HomePageController {
    public HomePageController() { }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getPage(HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("main");
        return mav;
    }
}

在/WEB-INF/jsp 下,我有一个main.jsp",并设置了一个合适的视图解析器来指向它.使用 *.form 映射调度程序时,我有一些工作,但使用上述代码无法获得任何工作.

Under /WEB-INF/jsp I have a "main.jsp", and a suitable view resolver set up to point to this. I had things working when mapping the despatcher using *.form, but can't get anything working using the above code.

当 Spring 启动时,它似乎正确地映射了所有内容:

When Spring starts up it appears to map everything correctly:

13:22:36,762  INFO main annotation.DefaultAnnotationHandlerMapping:399 - Mapped URL path [/secure/app/main] onto handler [controller.HomePageController@2a8ab08f]

我也注意到这一行,看起来很可疑:

I also noticed this line, which looked suspicious:

13:25:49,578 DEBUG main servlet.DispatcherServlet:443 - No HandlerMappings found in servlet 'dispatcher': using default

在运行时,任何查看/secure/app/main 的尝试只会在 Tomcat 中返回 404 错误,并输出以下日志:

And at run time any attempt to view /secure/app/main just returns a 404 error in Tomcat, with this log output:

13:25:53,382 DEBUG http-8080-1 servlet.DispatcherServlet:842 - DispatcherServlet with name 'dispatcher' determining Last-Modified value for [/secure/app/main]    
13:25:53,383 DEBUG http-8080-1 servlet.DispatcherServlet:850 - No handler found in getLastModified    
13:25:53,390 DEBUG http-8080-1 servlet.DispatcherServlet:690 - DispatcherServlet with name 'dispatcher' processing GET request for [/secure/app/main]    
13:25:53,393  WARN http-8080-1 servlet.PageNotFound:962 - No mapping found for HTTP request with URI [/secure/app/main] in DispatcherServlet with name 'dispatcher'    
13:25:53,393 DEBUG http-8080-1 servlet.DispatcherServlet:677 - Successfully completed request

所以... Spring 映射一个 URL,然后在一秒钟之后忘记"那个映射?这是怎么回事?

So... Spring maps a URL, and then "forgets" about that mapping a second later? What is going on?

谢谢.

推荐答案

我和你有完全一样的问题.设置 'alwaysUseFullPath' 的方法非常简单.我的conf文件如下:

I have exactly the same problem as you. The way to set 'alwaysUseFullPath' is pretty straightforward. My conf file is as following:

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
    p:order="3" > <!-- a higher value meaning greater in terms of sorting.  -->
    <property name="alwaysUseFullPath" value="true" />
    <property name="interceptors">
        <list>
            <ref local="myInterceptor" />
        </list>
    </property>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="alwaysUseFullPath" value="true" />
</bean>

这篇关于Spring 3 简单的无扩展 url 映射与基于注释的映射 - 不可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆