首页 文章

使用@RequestParam和Apache tile 2的Spring MVC控制器

提问于
浏览
0

我有我的Spring MVC Apache tile 2项目的以下配置 .

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 当我调用test2(没有参数)时,可以读取header.css . 但是当调用test1时,我得到了header.css的 404 Not Found . 我注意到该视图正试图通过路径访问css
当test1正在调用时

http://localhost:8080/myproject/test1/resources/css/header.css

而不是

http://localhost:8080/myproject/resources/css/header.css

. 那么为什么@RequestParam会有所不同呢?

谢谢 .

1 回答

  • 0

    这是因为相对于当前URL加载了css . 对于test1,你有一个param,它在test1之后添加一个斜杠 . 其中test2没有param,所以它相对于父目录 .

    您可以尝试使用css的绝对路径 .

相关问题