【问题标题】:Creating two or more sitemesh decorators in struts 2 app?在 struts 2 应用程序中创建两个或多个站点网格装饰器?
【发布时间】:2013-05-08 15:23:34
【问题描述】:

我正在使用 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,并且我创建了一个导航文件,如下所示

<%@ 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”,它会将请求的资源附加到先前的资源目录中。请帮助我让它工作

【问题讨论】:

    标签: struts2 decorator sitemesh


    【解决方案1】:

    我在每个链接中使用 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> 
    

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

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2017-12-07
      • 1970-01-01
      • 2011-05-18
      • 2016-09-26
      • 1970-01-01
      • 2013-05-24
      • 2015-10-12
      • 2020-12-20
      相关资源
      最近更新 更多