I am building a web app using Spring MVC and I am having a problem.
Currently, if I login and go to the homepage of the webapp then it displays the user home page correctly (normal stuff profile image, details, etc) – thats all great.
However, I am trying to build in the functionality so that if anyone goes to the site with the url /user/username then it will redirect to a controller and attempt to find a user with the username “username” – if one is found then it loads the view of the selected users profile.
This is all working fine so far – but I wanted to check that if you are logged in and navigate to /user/mycurrentusername then dont load the normal profile page, just load the page you see when you first logon.. the problem is that when I return the same view I do for the homepage, being as I am now at url /user/* none of the page resources can be found (images/css) so i get the page but with no formatting/images..
What would be the best way around this? is there a better way to handle this?
(I also have problems that as it looks for images/css at /url/images.. it fires a request to my user Controller again 🙁 )
Help much appreciated!
The urls you’re creating for your stylesheets etc. should be created ideally with some sort of url rewriting tag, to ensure they’re relative to the root app context and not to the current url. What view technology are you using? JSP? If so you’ll want something like this
<link rel="stylesheet" href="<c:url value='/css/styles.css'/>" type="text/css"/>. The<c:url/>tag will create a url which is effectively constant and won’t change with your current page request.As for your controller being hit on requests for css/images etc, check out this question. In short, ideally you want to use something like Tuckey’s Url Rewrite filter to make sure that your DispatcherServlet isn’t called for static content requests.
Edit: Actually looks like Spring 3.04 has a nicer solution to this now than Tuckey’s Filter.
<mvc:resources/>looks much simpler to setup.