1

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!

rhinds
  • 9,976
  • 13
  • 68
  • 111

3 Answers3

1

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.

Community
  • 1
  • 1
Melv
  • 2,201
  • 16
  • 14
1

For the first problem you likely are specifying your image and css sources as relative paths. Since the view is rendered from a different path, that relative path is also different. See Melv's answer for how to get them specified correctly.

For the second problem, have a look at section 15.12.4 in the spring mvc docs for how to let static requests pass through the Dispatcher Servlet.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
1

You basically have three options:

  1. Don't change your web context. Make all your URLs like http://myapp/myrequest?par1=val1. I did this kind of applications time ago, but when there are a lot of possible operations URLs won't be organized. "REST style" URLs are prettier and is where the world is going to from some time ago.

  2. Use <c:url> in ALL your internal URLs. I think this is not a bad option, but is a lot of boilerplate code, more than it seems at first glance. If you call URLs with javascript must be aware of which is the correct URL, and can be clumsy.

  3. In my current developement, I make use of the <BASE> tag. You must include <base href="http://myhost:8080/myapp/" /> in the head of all your pages. Then you can make all your URLs relative to your application's base path. There are some downsides, but is my preferred option.

EDIT: Answering your question. Instead of parameterise your application you can obtain the application's root URL this way:

<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
sinuhepop
  • 20,010
  • 17
  • 72
  • 107
  • Thanks Sinuhe - with the 3rd option, how would you cater for deploying the app to different servers? would oyu just have to parameterise it and have a build/deploy that handles it? – rhinds Mar 19 '11 at 23:01