Spring MVC 控制器中的硬编码 @RequestMapping URL [英] Hard-coded @RequestMapping URL in Spring MVC Controller

查看:48
本文介绍了Spring MVC 控制器中的硬编码 @RequestMapping URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Spring 3,我正在一个简单的网络应用程序中使用它.

I'm studying Spring 3 and I'm using it in a simple web-application.

现在我正在使用注释实现一个 Spring MVC 控制器,我想知道:是否有使用 @RequestMapping 注释的最佳实践?

Now I'm implementing a Spring MVC Controller using annotations, and I'm wondering: Is there any best practice using @RequestMapping annotation?

我的意思是:我已经看到通常在这个注解中映射的 URL 是在类中硬编码的...
有没有办法以松散耦合的方式"传递 URL(以获得更可重用的类)?

I mean: I've seen that usually the URL mapped in this annotation is hardcoded in the class...
Is there a way to pass the URL in a 'loosely coupled way' (to obtain a more reusable class)?

我知道有一些通配符可以使用,但我认为这不是解决方案......我错了吗?

I know that there are some wild cards that can be used, but I think that isn't the solution... Am I wrong?

我添加了一个例子来更好地解释我的疑问.

I add an example to better explain my doubt.

假设我希望我的控制器由对 /foo/bar/baz/mypage.htm 的请求触发,在我的控制器中,处理程序方法将使用 @RequestMapping("/foo/bar/baz/mypage").

Suppose I want my controller to be triggered by a request to /foo/bar/baz/mypage.htm, in my controller the handler method will be annotated with @RequestMapping("/foo/bar/baz/mypage").

现在我决定将触发我的控制器的 URL 更改为 /foo/bar/otherpage.htm,所以我需要编辑我的类,将 @RequestMapping("/foo/bar/otherpage") 在我的处理程序方法上,重新编译项目并再次部署它.

Now I decide to change the URL triggering my controller into /foo/bar/otherpage.htm, so i need to edit my class, put @RequestMapping("/foo/bar/otherpage") on my handler method, recompile the project and deploy it again.

在我看来不太实用...

It seems to me not so practical...

推荐答案

当前带注释的控制器不是很可配置.

Currently annotated controllers aren't very configurable.

据我所知,解决这个问题的唯一可能方法是使用替代 HandlerMappings 以配置控制器的基本 URL".例如如下:

As far as I know, the only possible approach to this problem is to use alternative HandlerMappings in order to configure "base URLs" of controllers. For example, as follows:

// Note the absense of @Controller to prevent this controller
// from being discovered by DefaultAnnotationHandlerMapping
public class FooController {
    @RequestMapping("/list") public String list(...) { ... }
    @ReqeustMapping("/save") public String save(...) { ... }
}

.

<bean 
    class = "org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" />

<bean name = "/foo" class = "FooController" />
<bean name = "/bar" class = "FooController" />

在这个例子中,FooController 的两个实例处理了 /foo/list/foo/save/bar/list/bar/save 分别.

In this example two instances of FooController handle /foo/list, /foo/save, /bar/list and /bar/save respectively.

即将发布的 Spring 3.1 将具有改进的 Spring 3.1 架构(Spring 3.1 M2: Spring MVC Enhancements) 似乎更灵活,虽然我还没有检查过.

The upcoming Spring 3.1 will have an improved Spring 3.1 architecture (Spring 3.1 M2: Spring MVC Enhancements) that seems to be more flexible, though I haven't checked it yet.

这篇关于Spring MVC 控制器中的硬编码 @RequestMapping URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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