简体   繁体   中英

Spring MVC controller using @RequestParam with Apache tile 2

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

<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>

@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";
}

<%@ 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>

When i am calling test2 (without parameter), the header.css can be read. 当我调用test2(不带参数)时,可以读取header.css。 But when test1 is called, I am getting 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM