Search code examples
springspring-mvcapache-tiles

Spring MVC controller using @RequestParam with Apache tile 2


I am have the following configuration for my Spring MVC + Apache tile 2 project.

The Tile configuration

<definition name="test1" extends="mymain">
    <put-attribute name="main">
        <definition template="/WEB-INF/views/tiles/template/generictemplate.jsp">
            <put-attribute name="headerstyle" value="./resources/css/header.css" type="string" /> 
            <put-attribute name="genericcontent" value="/WEB-INF/views/tiles/test.jsp" />                 
        </definition>
    </put-attribute>
</definition>
<definition name="test2" extends="mymain">
    <put-attribute name="main">
        <definition template="/WEB-INF/views/tiles/template/generictemplate.jsp">
            <put-attribute name="headerstyle" value="./resources/css/header.css" type="string" /> 
            <put-attribute name="genericcontent" value="/WEB-INF/views/tiles/test.jsp" />                 
        </definition>
    </put-attribute>
</definition>

The controller

@RequestMapping(value="/test1", method=RequestMethod.GET)
    public String test1(@RequestParam(value="id", required=true) String id){
        return "test1";
    }
@RequestMapping(value="/test2", method=RequestMethod.GET)
    public String test2(){
        return "test2";
}

The view generictemplate.jsp

<%@ include file="include.jsp" %>
<tiles:importAttribute name="headerstyle" />
<link href="${headerstyle}" rel="stylesheet" type="text/css" />

<div role="main" class="main clearfix">
        <section class="generic">
            <div class="post">
                <tiles:insertAttribute name="genericcontent"/>
            </div>
        </section>
    <!-- end main -->
</div>

My problem is When i am calling test2 (without parameter), the header.css can be read. But when test1 is called, I am getting 404 Not Found for header.css. I noticed that view is trying to access the css with path of

http://localhost:8080/myproject/test1/resources/css/header.css
instead of
http://localhost:8080/myproject/resources/css/header.css
when test1 is calling. So why the @RequestParam makes this difference?

Thank.


Solution

  • This is because the css is being loaded relative to the current URL. For test1 you have a param which adds a slash after test1. Where as test2 doesn't have the param, so its relative to the parent directory.

    You could try using absolute paths for the css.