Spring MVC equivalent of .Net MVC LinkExtensions.ActionLink Method - java

I am coming from a ASP.Net MVC world where Microsoft provides the LinkExtensions.ActionLink method in Razor (their version of JSP) to generate anchor element (a element) to a specific controller/action.
The concept is pretty simple: the Razor (JSP) code references the controller and action, the ActionLink function does the hard work of generating the correct URI to get to that controller/action.
Is there anything like this in Spring MVC?

Unfortunately, Spring doesn't support this at the moment, although there is a feature request for it. The best you can do right know is use <spring:url> and hardcode the path to the controller and action. <spring:url> will give you a context-root-relative URL.
Part of the difficulty in implementing this is that Spring gives you the power to arbitrarily define your mappings (i.e. with #RequestMapping). So if you're not using ControllerClassNameHandlerMapping it can be an issue.

Related

Spring MVC bean data flow

I got task unfortunately from work to create page using spring mvc, but i never work with that. I have to do that web app with restricted specifications which are following:
JSP cannot contain javascript, java, jstl only spring mvc tag's, but it's not everything. Controller must containt vaiable's only inside methods, all methods can return only String or void, so I'm unable to use #ModelAttribute, ModelMap, Model to move data from one view to another.
Is it possible to create page with following rules? I dont realy know how beans work in Spring MVC, but it seems that it will doesnt work without it.

What will tomcat do after I type the root link of a spring application?

Let's say I have an application written with spring framework, and I want to know, when I typed in :
http://localhost:8080/test
link, what tomcat will do to generate response for this request ?
Should it have to pass all filters first, then ????
And after I typed in the url, it always be directed to another link like http://localhost:8080/test/login, where was this redirection implemented ?
If it is hard to explain to me, then please recommend me a book for that, thanks very much !
what tomcat will do to generate response for this request ?
Tomcat will typically send the the request to the relevant DispatcherServlet instance, as configured in your "web.xml" file. This is described in the Spring documentation.
Should it have to pass all filters first, then ????
Yes. Filters are applied before (and after) requests are passed to the Servlet.
And after I typed in the url, it always be directed to another link like http://localhost:8080/test/login, where was this redirection implemented ?
That depends on how you have implemented security. It could be done at the Tomcat level (I think), using SpringSecurity, or hard-wired logic in your Spring MVC controller, or in a plain (non-Spring) Servlet, Filter or (Tomcat specific) Valve.
If you are looking for advice on the best way to implement login / security, I'd recommend using SpringSecurity. SpringSecurity works using Filters.
If it is hard to explain to me, then please recommend me a book for that, thanks very much !
The online Spring and SpringSecurity documentation is as good a place as any. This documentation tends not to spell out exactly how requests get processed in the context of a particular web container, but you should be able to figure the details from the Tomcat docs and the Servlet specification.
If there's a controller that's mapped to that URL, the Spring DispatcherServlet will send the request to it for handling.
If the controller determines that the next view ought to be the one that corresponds to /test/login, then it'll specify so when it sets the ModelAndView.

Are there any Java MVC frameworks that utilize request path patterns instead of defined servlet url patterns?

From what I know of MVC outside of the Java world (PHP and Ruby on Rails), all requests are first sent to the front controller (or dispatcher... or boostrap, etc.), and the front controller looks to the request path pattern in the URL in order to determine what class/method should handle the request. In Java MVC, it appears that servlets are mapped with the url pattern in the deployment descriptor (web.xml), but the file extension and url pattern doesn't appear to be very flexible. Are there any Java MVC frameworks that use a front controller to read the request path exclusively to determine what classes should execute? Would it be fairly easy to hack Spring MVC to do this? Any examples? Thanks!
An example of one tool that works as you desire is web4j.
By default, it maps incoming URLS to the Action class whose package-qualified name maps in a fairly natural way the the incoming URL.
Example from its docs:
Request URL: 'http://www.blah.com/fish/main/member/MemberEdit.list'
Extracted part of URL: '/main/member/MemberEdit'
Maps (by default) to the Action: 'hirondelle.fish.main.member.MemberAction.java'
This is an example of how that particular tool performs the task. Since this is such a basic feature of web apps, I would imagine that nearly all such tools have similar mechanisms.
I am not a big user of Spring, but I can see from its docs that it has a number of ways of mapping requests to Actions :
SimpleUrlHandlerMapping
ControllerClassNameHandlerMapping
Java servlet mappings can also be by file extension.
Much like many non-Java frameworks, you could map all requests to a single servlet that then processes them but that tends to be discouraged in Java. It's certainly possible though.
If you want more REST-style URLs where you declare the mapping of path elements, you might want to look at the Spring MVC setup in Spring 3.0.
I agree the URL mapping is not very flexible but you can handle mapping with URLRewriteFilter
http://tuckey.org/urlrewrite/
For this purpose, the filter works almost like a controller.
Check out stripes:
http://www.stripesframework.org/display/stripes/Quick+Start+Guide
I've been looking at it as a possible upgrade from struts. There is an example on that page that is very similar to the web4j example given by John O.

url-action mapping & routing in Spring MVC 3.0

I'm developing a web application using Spring MVC 3.0 and looking for a ready-made solution, if any, or a "best practices" reference for a url/action mapping and routing system that can achieve the following:
REST-friendly controller / method name to view mapping. The current mapping implementation translates the request to a view name, which may be problematic when using several parameters and is incompatible with REST urls
A service that accepts the name of a controller, a method and arguments values and renders the URL that's represented by them
Integration with Spring Security that can allow me to check for a given URL whether the current user is allowed to access it, so that I can decide whether or not to render a URL
A menuing system based on the above that can define menues composed of these actions and render them to page
Basically what I need is the ability to define URLs in one centralized place, so that changing a URL (during development; I'm aware of the don't-change-live-urls idea :) ) does not mean looking up and changing that URL in a zillion pages.
Any directions to such an existing solution / tutorial / guide would be great.
Thanjs
This is a feature I really miss in Spring MVC.
That's why I created the springmcv-router project, basically a port of PlayFramework's Router implementation in Spring MVC (HandlerMapping + HandlerAdapter).
I'm heavily using it in several real-world projects and the Router implementation itself is reliable.
Try using Spring Roo. It utilizes many best practices for spring MVC, and it has a scaffolding feature that automatically maintains a menu, jsp's and all the CRUD methods of a controller.
Setting up Spring Security with Roo is as simple as typing "security setup".
Hope this is helpful.

Spring MVC REST : static files unaccessible because of url-pattern

My Spring Dispatcher servlet url-pattern is /* (as spring MVC REST suggests)
Now all the request are resolved by this Servlet. even CSS/JS/Images also get resolved and handled by servlet..
So, Spring MVC tries to find controller.. :(
How to bypass this? Is there any standard way out of this problem??
& Don't want to change url-pattern to /rest/* (so, other static resources get accessed by /css/ or /js etc.)
You can map your controllers to a smaller set of URLS (i.e. /app/*), and then rewrite the URLs that your users actually see so that they don't even know about. Have a look at the mvc-basic webapp sample, particularly web.xml and urlrewrite.xml to see how this is done.
Map the Spring dispatcher to some subsection of the URL space, and use Tuckey to rewrite URLs the user deals with.
http://www.example.org/app/controller/action -> http://www.example.org/controller/action
Just a heads-up update on this: the default rewrite configuration as defined in the Spring sample did not work out of the box for me. The rewrite rules for stylesheets, scripts, etc. were still processed to the /app/* rule, and subsequently handled by the DispatchServlet, which is not desirable.
I had to add the last="true" attribute to the styles/scripts/images rules to indicate that other rules should not apply, and I had to use the FreeMarker Spring URL macro in any CSS/JS include paths.
Just in case someone encounters the same problem.

Categories