Search code examples
springjspspring-mvcspring-roojspx

How to avoid hard linking Frontend assets In Spring MVC


I am new to spring MVC coming with experience using PHP MVC frameworks and ROR. I am having a hard time finding the appropriate way to organize and include front end assets into the view templates.

Here is the default code Roo produces for the default style sheet:

<spring:theme code="styleSheet" var="roo_css"/> 
<spring:url value="/${roo_css}" var="roo_css_url"/>  
<spring:url value="/static/images/favicon.ico" var="favicon" />
<link rel="stylesheet" type="text/css" media="screen" href="${roo_css_url}"></link>

This seems totally unnecessary to me. We are calling a variable from the spring:theme code list. Assigning it to a variable in the view scope/ and then calling that view variable for the

Ideally I would like to have some path tokens like: ${imagePage}, ${stylePath}, etc. That we could drop in and use for soft linking.

Hopefully somebody can point me to some quality SpringMVC documentation or give some examples. Thanks

Update:

I have seen a few examples and engfer has posted one below that suggest using the spring tags within the html to ouput the href like so

<a href="<spring:url url='/about'/ />">About</a>

This would be acceptable however I am getting the following error from jetty

Caused by: org.apache.jasper.JasperException: /WEB-INF/views/footer.jspx(6,22) The value of attribute "href" associated with an element type "null" must not contain the '<' character.

Am I missing something with the encoding? or DTD?

Update:

So apparently the in-line href style only works with .jsp files as .jspx (What im using) is strict xml. What are the benefits of .jspx over .jsp and


Solution

  • The code you have provided from Roo is a little unnecessary. If you look at the Spring MVC documentation as tkeE2036 pointed out... you will see the themes section http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-themeresolver

    <link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>
    

    The Spring theme tag gives you the ability to internationalize your CSS/images/js (imagine an image that has the word 'Hello' which needs to be changed for English/Spanish/etc) into separate internationalized theme files that follow the http://java.sun.com/javase/6/docs/api/java/util/ResourceBundle.html convention and Spring will automatically resolve the correct resource bundled theme.


    If you still want your ${imagePath} or whatever, you can use the <spring:url/> tag to get your job done.

    <spring:url value="/images" var="imagePath"/>
    <link rel="stylesheet" href="${imagePath}/foo.png" type="text/css"/>