7

Is there a way of specifying a dynamic action attribute in the s:form tag in Struts2? I want something like the below.

<c:set var="formAction" value="baseAction" />
<c:if test="${someCondition}">
     <c:set var="formAction" value="childAction" />Ac

<s:form method="post" action="${formAction}">
    <s:input....../>
    <s:select...../>
</s:form>

I know this can be achieved using javascript but I want to avoid refactoring. I have tried achieving this using scriplets but the issue is that Struts2 tags doesn't take runtime attributes. I tried even with OGNL but it didn't help either.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46

2 Answers2

7

Use Struts2 tags to set values and check condition and then use OGNL to put action attribute.

<s:set var="formAction" value="'baseAction'" />
<s:if test="some_condition">
  <s:set var="formAction" value="'childAction'" />
</s:if>

<s:form method="post" action="%{#formAction}">
  <s:input....../>
  <s:select...../>
</s:form>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
2

Use the s:url tag to build the action url dynamically.

<s:url action="%{somePoperty}" var="myUrl"/>
<s:form action="%{#myUrl}"> 

In fact Struts tag's attributes don't accept not only scriptlets but also JSTL EL expressions.

Roman C
  • 49,761
  • 33
  • 66
  • 176