Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I find the URL for a spring mvc controller in the view layer?

I think what I need is called reverse url resolution in Django. Lets say I have an AddUserController that goes something like this:

@Controller
@RequestMapping("/create-user")
public class AddUserController{ ... }

What I want is some way to dynamically find the url to this controller or form a url with parameters to it from the view (JSP), so I don't have to hardcode urls to controllers all over the place. Is this possible in Spring MVC?

like image 315
Vasil Avatar asked Jul 04 '09 18:07

Vasil


People also ask

Which annotation is used for mapping URL with controller in Spring framework?

One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers.

What is Model-View-Controller in Spring MVC?

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files.


2 Answers

Have you considered having a bean that aggregates all of the controller URLs you need into a HashMap and then adding this controller/URL Map to any model that requires it? Each Spring controller has the ability to call an init() method, you could have each controller add it's name and URL to the controller/URL map in the init() methods so it would be ready to use when the controllers go live.

like image 167
jottos Avatar answered Sep 18 '22 17:09

jottos


Can solve with Java Reflection API. By Creating Custom Tag library. methods looks like this

Class c = Class.forName("Your Controller");

            for(Method m :c.getMethods()){
                if(m.getName()=="Your Method"){
                    Annotation cc = m.getAnnotation(RequestMapping.class);
                    RequestMapping rm = (RequestMapping)cc;
                    for(String s:rm.value()){
                        System.out.println(s);
                    }
                }
            }

Possible Problem You Can Face is

1.Path Variable > Like this /pet/show/{id} so set of path name & value should be support then replace this String.replace() before return url

2.Method Overriding > only one method is no problem. if Method override Need to give support sequence of Parameter Type That you really want like Method.getParametersType()

3.Multiple Url to Single Method> like @RequestMapping(value={"/", "welcome"}). so easy rule is pick first one.

4.Ant Like Style Url > Like this *.do to solve this is use multiple url by placing ant like style in last eg. @RequestMapping(value={"/pet","/pet/*.do"})


So Possible link tag style is

<my:link controller="com.sample.web.PetController" method="show" params="java.lang.Integer">
<my:path name="id" value="1" />
</my:link>

Where parmas attribute is optional if there is no method override.


May be I left to think about some problem. :)

like image 38
Mr.Desert Fox Avatar answered Sep 18 '22 17:09

Mr.Desert Fox