春季3个简单的extentionless网址映射与基于注解的映射 - 不可能? [英] Spring 3 simple extentionless url mappings with annotation-based mapping - impossible?

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

问题描述

我用弹簧3,并试图使用注释来定义控制器映射来建立一个简单的Web应用。这似乎是不尖刻所有的URL与.FORM *或*。做

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都在/安全。有一个<安全约束> 在web.xml中的根目录下保护一切。我想所有的春控制器/安全/应用/图。

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/.

范例网址是:结果
/安全/应用/的LandingPage结果
/安全/应用/编辑/客户/ {ID}结果
每一个我会用适当的JSP / XML处理/不管。

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>

和调度员在-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 *的映射调度员工作时,却无法得到任何使用上述code工作。

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.

在春季启动时,它似乎映射一切正常:

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

和在运行时的任何尝试,查看/安全/应用/主刚刚返回404错误在Tomcat中,这个日志输出:

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

所以...春天映射一个URL,然后忘记关于映射第二以后呢?这是怎么回事?

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

感谢。

推荐答案

我有完全一样的问题,因为你。设置'alwaysUseFullPath'的方式是pretty简单。我的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>

这篇关于春季3个简单的extentionless网址映射与基于注解的映射 - 不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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