Announcement Announcement Module
No announcement yet.
Newbie MVC Question Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie MVC Question

    Really, really frustrated right now ---

    I want to use Spring to deliver content using Velocity. I believe it is still considered good web design to use CSS and to reference things such as CSS and JavaScript in external files. I'm sure this is very easy to do, but nothing I try seems to work.

    Here is my Velocity file, index.vm:

    		<title>Hello World</title>
    		<link rel="stylesheet" type="text/css" href="login.css" />
    		The current time is ${now}
    In my web.xml, I configure the Dispatcher servlet with my context name, 'hello'. Then in hello-servlet.xml, I configure Velocity as follows:

    	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    				<prop key="/index.html">helloViewController</prop>
    				<prop key="/*.css">urlFilenameViewController</prop>
    	<bean id="urlFilenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
    	<bean id="helloViewController" class=""/>
    	<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    		<property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
    	<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    		<property name="cache" value="true"/>
    		<property name="suffix" value=".vm"/>
    My HelloController class implements the Controller interface, and I can see via logging that the handleRequest method is called. When I deploy this to Tomcat and look at it in Firefox, I see all of the content just fine.

    My problem is that my CSS file isn't getting loaded. I'm pretty sure this is because the DispatcherServlet is handling my browser's requests for the CSS as well. I tried defining a second view resolver for CSS files, and adding lines like:
    	<property name="order" value="2"></property>
    to make the DispatcherServlet go through them in order, but when I tried that, it ended up processing my Velocity template through the 'CSS' view resolver, and returned a message to my browser saying that the file doesn't exist.

    If Spring makes simple things easy, then what's the easy answer to this? I'm sure that whether one is using Velocity, JSPs or something else, one is still going to be referencing CSS, JavaScript, images, and lots of other things in their main presentation files. Can someone please provide a simple example of how this is handled in 'real life', because I've been unable to find one on the web.

  • #2
    I put css files to one folder under and it works forme

    I put css files in




    folder and refer as "/styles/login.css" in html/jsp files.

    <link rel="stylesheet" type="text/css" href="/styles/login.css" />

    Is your css file static, or style changes based on each login? Theme may be a better solution for later. Above works for static style only.
    Last edited by yuhaian; Mar 28th, 2011, 01:12 PM.


    • #3
      Looks like WEB-INF was the problem

      I wasn't thinking about the fact that the 'WEB-INF' subfolder is special and that clients can't directly request content from the folder. When I moved my CSS up out of that folder in the manner yuhaian describes, then everything works. Yay!

      Maybe I'm being paranoid, but I would really like to put ALL of my content under WEB-INF. Can anyone suggest how to make this work? The way I have my layout planned is:

      <context root>/public, for content anyone can see
      <context root>/WEB-INF/classes
      <context root>/WEB-INF/css
      <context root>/WEB-INF/images
      <context root>/WEB-INF/js (javascript)
      <context root>/WEB-INF/lib
      <context root>/WEB-INF/velocity