【问题标题】:Apache Tiles 2.2.2 Pass value from tiles-config.xml to JSPApache Tiles 2.2.2 将值从 tiles-config.xml 传递到 JSP
【发布时间】:2015-07-28 10:04:55
【问题描述】:

我在所有页面中都有一个共同的页眉,除了一些页眉右侧有一些选项的页面。例如,我的标题右侧将有如下选项:块、撰写、编辑,具体取决于页面。是否可以从tiles-config.xml 设置这个headerShowIcons,然后在rhsHeader.jsp 中使用它。

有什么方法可以将一些值从 tiles-config.xml 传递到 rhsHeader.jsp 并基于该值我想显示选项(即阻止、编辑或删除)

示例:如果我想向用户显示 Compose 选项,我只需从 tiles-config.xml 传递值(即 headerShowIcons='compose')并在 JSP 中使用它。如果我想显示块,我将传递 headerShowIcons='block' 将由 rhsHeader.jsp 读取以提供显示块选项。我希望这会更清楚

tiles-config.xml

<definition name="*/*/*/*/*/index" template="/{1}/{2}/xyz/{4}/layouts/layout.jsp">
    <put-attribute name="lhsHeader" value="/{1}/{2}/xyz/s/headerWithBack.jsp" />
    <put-attribute name="rhsHeader" value="/{1}/{2}/xyz/s/rhsHeader.jsp"/>
    <put-attribute name="body" value="/{1}/{2}/xyz/s/myProfile.jsp" />
</definition>

layout.jsp

<!-- What should I add here to meet the requirement -->  
 <body>
   <header> 
      <div>
        <tiles:insertAttribute name="header"/>
        <tiles:insertAttribute name="rhsHeader"/>   
       </div>
   </header>
    <div>
       <tiles:insertAttribute name="body"/>
   </div>
  </body>

rhsHeader.jsp 是这样的:

 <tiles:useAttribute name="headerShowIcons" />
 <div class="RHS">
<ul>
<!-- I want to show the options (like- Block,Delete,etc) which will be passed from tiles-config.xml to rhsHeader.jsp (which here I have written it as headerShowIcons )-->
    <c:choose>
        <c:when test="${headerShowIcons  eq 'refine'}">
            <li><a href="#" class="refine">Refine</a></li>
        </c:when>
        <c:when test="${headerShowIcons  eq 'block'}">
            <li><a href="#" class="block">Block</a></li>
        </c:when>
        <c:when test="${headerShowIcons  eq 'edit'}">
            <li><a href="javaScript:void(0);" class="edit">Edit</a></li>
        </c:when>
        <c:when test="${headerShowIcons  eq 'delete'}">
            <li><a href="javaScript:void(0);" class="delete">Delete</a></li>
        </c:when>
        <c:otherwise>
                 <p>test</p>
        </c:otherwise>
    </c:choose>
</ul>

【问题讨论】:

    标签: xml jsp spring-mvc jstl apache-tiles


    【解决方案1】:

    我终于找到了解决方案,我相信它可能对有这种要求的人有所帮助:

    我们将不得不使用

    headerLayout.jsp

    <tiles:useAttribute name="whatToShow"/>
      <header> 
        <div>
          <tiles:insertAttribute name="lhsHeader" />
          <tiles:insertAttribute name="rhsHeader">
              <tiles:putAttribute name="whatToShow">${whatToShow}</tiles:putAttribute> 
            </tiles:insertAttribute>
        </div>
       </header>
     <div>
         <tiles:insertAttribute name="body"/>
     </div>
    

    tiles-config.jsp

     <definition name="Header.*" template="/{1}/layouts/headerLayout.jsp">
        <put-attribute name="lhsHeader" value="/s/headerWithBack.jsp"/>
        <put-attribute name="rhsHeader" value="/s/rhsHeader.jsp"/>  
        <put-attribute name="body" value=""/>
        <put-attribute name="whatToShow" value="block" type="string"/>
    </definition>
    <!-- inside the definition under whatToShow, I just write whatever value(block,edit,delete,etc) I want to pass to the rhsHeader.jsp -->
    

    rhsHeader.jsp

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <tiles:useAttribute name="whatToShow"/>
    <div class="RHS">
    <ul>
        <c:choose>
            <c:when test="${whatToShow  eq 'refine'}">
                <li><a href="#" class="refine">Refine</a></li>
            </c:when>
            <c:when test="${whatToShow  == 'block'}">
                <li><a href="#" class="block">Block</a></li>
            </c:when>
            <c:when test="${whatToShow  eq 'edit'}">
                <li><a href="javaScript:void(0);" class="edit">Edit</a></li>
            </c:when>
            <c:when test="${whatToShow  eq 'delete'}">
                <li><a href="javaScript:void(0);" class="delete">Delete</a></li>
            </c:when>
            <c:otherwise>
                     <p></p>
            </c:otherwise>
        </c:choose>
      </ul>
    </div>
    

    PS : 我使用了 whatToShow 而不是 headerShowIcons

    【讨论】:

      【解决方案2】:

      我会选择一种不同的方式,但如果这是你想要的,请看下面

      tiles-config.xml

      <tiles-definitions>
          <definition name="app.base" template="/WEB-INF/views/default/default.jsp">
              <put-attribute name="title" value="Not Found" />
              <put-attribute name="headerShowIcons" value="" />
              <put-attribute ..........>  
          </definition>
          <definition name="home" extends="app.base">
              <put-attribute name="title" value="Home Page" />
              <put-attribute name="headerShowIcons" value="edit" />   
              <put-attribute ..........>  
          </definition>
      </tiles-definitions>
      

      home.jsp

      <c:set var="var"><tiles:insertAttribute name="headerShowIcons"/></c:set>
      
      <c:choose>
          <c:when test="${'refine' == var}">
                <li><a href="#" class="refine">Refine</a></li>
          </c:when>
          <c:when test="${'block' == var}">
              <li><a href="#" class="block">Block</a></li>
          </c:when>
          <c:when test="${'edit' == var}">
              <li><a href="javaScript:void(0);" class="edit">Edit</a></li>
          </c:when>
          <c:when test="${'delete' == var}">
              <li><a href="javaScript:void(0);" class="delete">Delete</a></li>
          </c:when>
          <c:otherwise>
               <p>test</p>
          </c:otherwise>
      </c:choose>
      

      如果您希望可以将其移至 layout.jsp 并相应地显示您的标题

      【讨论】:

      • 我已经编辑了问题,我想现在会更清楚了。
      • @balboa_21 抱歉,我省略了部分解决方案。查看编辑
      • 感谢您的帮助...这是非常需要的,但它并没有解决我的要求。顺便说一句,我找到了解决方案并在上面写了。
      • 很高兴它有帮助。顺便说一句,我的解决方案和你的解决方案完全相同。
      • 是的,它做同样的事情..但我不知道为什么,我分配给 headerShowIcon 的任何值都显示在浏览器中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2015-06-30
      • 2011-06-25
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多