【问题标题】:struts logic tag: only works with form bean parameters?struts 逻辑标签:仅适用于表单 bean 参数?
【发布时间】:2011-02-16 02:20:45
【问题描述】:

我正在重构现有的 Web 应用程序以使用 struts 标签而不是 scriptlet。发生多个关系的事情之一是条件检查,如

<%if ("true".equals(requset.getparameter("someParam"))) {%>

some html, javascript

<%else{%>
some other html,javascript
<%}%>

我希望用struts 逻辑标签替换它来替换scriptlet。是否可以在不将 someParam 存储在 formbean 中的情况下做到这一点?默认情况下,逻辑标签的语法似乎只适用于 formbean 参数。

【问题讨论】:

    标签: struts-1


    【解决方案1】:

    虽然不是特定于 struts 的解决方案:您是否考虑过使用 JSTL 自定义标签?这些标签包含在最新的 Web 容器规范中,并且可以轻松添加到默认情况下不包含 JSTL 规范的旧 Web 容器。

    这里是一个基于JSTL的解决方案:

    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
    ...
    <c:choose>
      <c:when test="${param.someParam eq 'true'}">
        some html, javascript
      </c:when>
    
      <c:otherwise>
        some other html, javascript
      </c:otherwise>
    </c:choose>
    

    除非您使用 struts 自定义标签来生成您的 UI 组件,否则尽可能使用 JSTL 标准标签。但是,如果您打算使用 struts,这里有一个 Struts 1 的示例:

    <bean:parameter id="paramValue" name="someParam" />
    <logic:equal name="paramValue" value="true">
      some html, javascript
    </logic:equal>
    
    <logic:notEqual name="paramValue" value="true">
      some other html, javascript
    </logic:notEqual>
    

    您会注意到,此解决方案采用请求参数并将其放入页面范围属性(paramValue)中,然后可以由 struts 逻辑自定义标记访问。

    【讨论】:

    • 谢谢。如果“someParam”为空(即参数未随请求一起发送),则 struts1 解决方案将不起作用。有没有办法解决这个问题?
    • 谢谢。我将添加一个默认值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多