Hard-coded @RequestMapping URL in Spring MVC Contr

2019-06-19 10:58发布

问题:

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

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

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?

EDIT:

I add an example to better explain my doubt.

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").

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

回答1:

Currently annotated controllers aren't very configurable.

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

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

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.



回答2:

I think you are trying to solve the wrong problem. If you wanted to change the pages that matched a controller you'd need to change a file somewhere. Would you rather change the file with the related code underneath it, or would you rather work with some XML files that specifies the URL and the class, and then you have to worry about the file being in the right place during runtime?

As there should be almost no code in your controller anyway, you should think of your controllers as compilable configuration files. Also, if you are using a build system like Maven or Ant and not compiling individual files by hand using Javac then compilation time shouldn't be an issue. If it becomes one, it's probably time to split your project into sub-projects.

I think you should just embrace this and see that it is probably not the issue you think it is. Also, did you know that controllers can match to expressions and not just literal strings? That gives you some flexibility in your naming.

If you really want to, you could just fall back to the Spring 2.0 style XML configuration, but I don't think anyone would recommend that.



回答3:

I think that is not a best practice, but have you tryed with @PathVariable annotations?

 @RequestMapping(value="/path/{word}", method=RequestMethod.GET)
public ModelAndView myRestMethod(@PathVariable String word) {
    ...
}