-1

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

装饰器.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,我创建了一个导航文件,如下所示

<%@ 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 装饰器一起显示,当我单击“第 1 页”链接时,它也与相应的(布局)装饰器一起显示。但问题是,在访问“第 1 页”后单击主链接时,它会给出“HTTP 状态 404 - /StrutsSitemesh/second/first/index.jsp”,它会将请求的资源附加到先前的资源目录中。请帮我让它工作

4

1 回答 1

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> 

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

于 2013-05-14T11:45:43.357 回答