开发者

struts logic tag: only works with form bean parameters?

开发者 https://www.devze.com 2023-02-10 22:48 出处:网络
I am refactoring an existing web app to use struts tags instead of scriptlets. One of the things occurring multiple tiems is the conditional check like

I am refactoring an existing web app to use struts tags instead of scriptlets. One of the things occurring multiple tiems is the conditional check like

<%if ("true".equals(requset.getpar开发者_JS百科ameter("someParam"))) {%>

some html, javascript

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

I wish to replace this with struts logic tag to replace scriptlets. Is it possible to do it without storing someParam inside a formbean? By default, it seems the syntax of logic tag works only with formbean parameters.


While not a struts-specific solution: have you thought about using JSTL custom tags? The tags are included in the latest web container specifications and can be easily added older web containers which do not include the JSTL specification by default.

Here is a solution based on 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>

Unless you are using the struts custom tags to generate your UI components, it is always preferred to use the JSTL standard tags whenever possible. However, if you are set on using struts, here is an example with 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>

You will note that this solution takes the request parameter and puts into into a page scoped attribute (paramValue) that can then be accessed by the struts logic custom tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消