Categories
Java Web Development

Using Velocity date tool with Spring MVC

I was having trouble getting Velocity‘s date tool to work with Spring MVC. I followed the instructions, most copied verbatim from what was most likely one ancient source. Those instructions said that to enable Velocity’s date formatting tool, DateTool, I needed to configure the VelocityViewResolver like this (WRONG):

<bean id="viewResolver">
  class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"></bean>
<br /><property name="numberToolAttribute"><value>numbertool</value></property>
<br /><property name="dateToolAttribute"><value>datetool</value></property>
&nbsp;

And then use it as:

$date.format("yyyy-MM-dd", $currentDate)

This would have been cool had I seen it in just one source but more than one source had it. The problem is small but crucial:

The value you give the dateToolAttribute property will be the name of the object Velocity will try to access when you are using the tool. So in the above configuration, the value for dateToolAttribute is “dateTool”, and hence the usage should be:

$dateTool.format(“yyyy-MM-dd”, $currentDate)

It may sound petty but it took me time to discover… lame.

Share
Categories
Computing Java Web Development WebSphere

Setting up Spring MVC welcome page without rewrites or redirection

So you have a Spring MVC web application and want to make sure that when the user enters a URL such as
http://www.myawesomedomain.com he/she will see the home page of your application. How do you get Spring MVC’s RequestDispatcher servlet, which acts as the traffic cop and then some for your web application, to act as the default URL handler in the eyes of the web application? There are two good ways to do it, and two bad ones. Let’s start with the bad ones, of course:

  • Redirect through meta tag: you add a meta-refresh element to the head element which causes the browser showing the page to go the ‘real’ home page url, such as /index.do
  • You use JavaScript to force the page to redirect – this will also work but forces you to rely on client-side processes to get stuff done. For example: window.location="index.do" inside an index.html file.

Both are sub-prime because search engines are not too keen to follow such redirects, often time avoiding JavaScript altogether.

So what are the good ways:

  • Using Apache’s mod-rewrite to mask the URL of your application, but that assumes you have a web server in front of your application
  • Using the servlet API – which is what you read below

So how do you do it?

  1. In the application’s web.xml file map the RequestDispatcher servlet to handle requests for index.htm, aside from handling the extension of your liking, say, *.do. You do this through the servlet-mapping element. This should look like this:

    <servlet -mapping>
        </servlet><servlet -name>requestDispatcher</servlet>
        <url -pattern>*.do</url>
     </servlet -mapping>
      <servlet -mapping>
        </servlet><servlet -name>requestDispatcher</servlet>
        <url -pattern>/index.htm</url>
     </servlet -mapping>  
    
  2. Also in web.xml, set up a welcome file that may be requested by a browser when a user does not enter a specific page request, e.g. www.domain.com. Most browsers at a minimum will automatically request, on behalf of the user, index.html and index.htm.

So choose one, say index.htm and set the <welcome-file> element to look something like this:

<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>

This will make your application by default make requests for ‘/’ go to index.htm.

  • The last and final step is to map the url index.htm to the controller for the application’s home page. You do this in a Spring MVC beans xml file when specifying the URL mapper.
    For example:

    <bean id="unAuthenticatedUrlMapping"
            class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
          <props>
            <prop key="/index.do">homePageController</prop>
            <prop key="/index.htm">homePageController</prop>
          </props>
        </property>
      </bean>
    

    This way the RequestDispatcher will know which control is actually going to load the information that will be displayed by the view for the home page.

  • And that’s it. No redirection. No nonsense.

    Share
    Categories
    General Java Web Development

    Eclipse Subclipse SVN + Hostgator.com

    I use Hostgator as my hosting company. I love them – great service, tons of space and no long term obligations. And the servers are fast. Now, another reason to love them – running Subversion repositories on your server with the caveat – you can only have one account to account to access that repository. If you develop by yourself, and I do for now, it’s not a problem. So how do you do it – assuming you are a Java coder using Eclipse on Windows?

    1. Send Hostgator support a request to get SSH access to your server. They will ask you to send in a picture id and once that is cleared, you should be good to go. A matter of a day or two.
    2. Install Subclipse SVN plugin for Eclipse. The Subversive plugin DOES NOT WORK.
    3. Install the really good Tortoise SVN client
    4. Set up an environment variable called SVN_SSH. To do that, go to the Windows Control Panel -> System -> Advanced -> Environment Variable -> New (under the bottom Window). For variable name enter the value SVN_SSH and for its value enter c:\\Program Files\\TortoiseSVN\\bin\\TortoisePlink.exe (assuming you installed Tortoise SVN in the default location).
    5. Start Eclipse and go to the Subclipse options: Window -> Preferences -> Team -> SVN . There, for the SVN Interface select “SVNKit (Pure Java)”. Click OK to store the setting.
    6. Now, using Putty or some other SSH tool, SSH to your account. You will need to set up a repository for your project. To do that, create the directory that will be used as repository, say /myrepo. Now, create an Subversion repository inside that directory: svnadmin create myrepo.
    7. Create a new project by checking out the repository from SVN. To do that, in Eclipse, go to: File -> New -> Other -> SVN -> Checkout Project From SVN.
    8. The ‘Checkout from SVN’ window will appear. There select to create a new repository location
    9. For the URL, enter: svn+ssh://<your hostgator admin user name>@<your domain name>:2222/home/<your hostgator admin user name>/<Repository directory path>. In other words, if your domain is example.com, your admin user name is joey and the path to your repository is /myrepo, then your URL will be svn+ssh://joey@example.com:2222/home/joey/myrepo
    10. Another popup window will follow that will ask you for your SSH user name. Enter the details and make sure they are saved.

    You should be good to go at that point as the rest of the process is the normal project checkout scheme used by Subclipse.

    Two resources were helpful: The support message board post on the subject and this blog entry.

    Hope this helps!

    Share
    Share