在struts 2 app中创建两个或多个sitemesh装饰器?

时间:2013-05-14 11:07:56

标签: struts2 decorator sitemesh

我正在使用sitemesh 2.4插件创建struts 2应用程序,其中我想根据请求的资源应用多个装饰器。

decorators.xml

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<decorator name="layout1" page="layout1.jsp">
    <pattern>/first/*</pattern>
</decorator>
<decorator name="layout" page="layout.jsp">
    <pattern>/second/*</pattern>
</decorator>
 </decorators>

我已经创建了两个不同的布局文件,名为layout.jsp和layout1.jsp在decorators目录中,我已经创建了一个这样的导航文件

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> 
<decorator:usePage id="thePage" /> 
<html>
<head>
</head>
<body>
<% String selection = thePage.getProperty("meta.selection"); %> 
<p> 
<table border="0" cellpadding="0" cellspacing="0"> 
<tr> 
    <td> 
        <% if(selection != null && "index".equals(selection)){ %> 
            <a href="first/index.jsp" class="selected">Main</a>
            <%System.out.println(""+selection); %> 
        <% } else { %> 
            <a href="first/index.jsp">Main</a> 
            <%System.out.println("index else"+selection); %>
        <% } %> 
    </td> 

</tr><tr> 
    <td> 
        <% if(selection != null && "page1".equals(selection)){ %> 
            <a href="second/page1.jsp" class="selected">Page 1</a> 
        <% } else { %> 
            <a href="second/page1.jsp">Page 1</a> 
        <% } %> 
    </td> 
</tr>
</table> 


   

欢迎页面(/first/index.jsp)与layout1装饰器一起显示,当我点击&#34;第1页&#34;链接它还显示相应的(布局)装饰。但问题是,在访问&#34;第1页&#34;之后点击主链接时它给出了#34; HTTP状态404 - /StrutsSitemesh/second/first/index.jsp"它将所请求的资源附加到先前的资源目录。 PLZ帮我搞定了

1 个答案:

答案 0 :(得分:0)

我在每个返回应用程序上下文路径的链接中使用reqest.getContextPath()方法,并使用这样的绝对路径请求资源----

<table border="0" cellpadding="0" cellspacing="0"> 
<tr> 
<td> 
    <% if(selection != null && "index".equals(selection)){ %> 
        <a href="<%=request.getContextPath() %>/first/index.jsp 
  class="selected">Main</a>
        <%System.out.println(""+selection); %> 
    <% } else { %> 
        <a href="<%=request.getContextPath() %>/first/index.jsp">Main</a> 
        <%System.out.println("index else"+selection); %>
    <% } %> 
</td> 
</tr>
<tr> 
<td> 
    <% if(selection != null && "page1".equals(selection)){ %> 
        <a href="<%=request.getContextPath() %>/second/page1.jsp"
 class="selected">Page 1</a> 
    <% } else { %> 
        <a href="<%=request.getContextPath() %>/second/page1.jsp">Page 1</a> 
    <% } %> 
</td> 
</tr>
</table> 

如果您的人们有更高效和更有效的解决方案,请详细说明。

相关问题