rajesh
rajesh

Reputation: 47

Dynamic id value for Struts 2 form

I want to use a dynamic value for id attribute in Struts 2 form and other tags as well.

Below is the same code:

<s:if test="null != #request.METHOD_CALL || #request.METHOD_CALL == 'ADD' ">
    <s:set name="pre" value="%{'a_'}" /> 
</s:if>
<s:else>
    <s:set name="pre" value="%{'e_'}" /> 
</s:else>

<s:form action="saveMeetingAction" id="**<s:property value='pre'/>**_editForm">
<s:textfield  name = "recDt" id = "**<s:property value="pre"/>**rcrdDt" size='11' maxlength='11' />

I would like to see my form as below:

<s:form action="saveMeetingAction" id="e_editForm">
<s:textfield  name = "recDt" id = "e_rcrdDt" size='11' maxlength='11' />

<s:form action="saveMeetingAction" id="a_editForm">
<s:textfield  name = "recDt" id = "a_rcrdDt" size='11' maxlength='11' />

Please suggest how to generate the id attribute in <s:form>. This is working fine with simple html form.

Upvotes: 2

Views: 2503

Answers (2)

Following code worked for me:

<s:form action="saveMeetingAction" id="<s:property value='#pre'/>" >
<s:textfield  name = "recDt" id="<s:property value='#pre'/>" size='11' maxlength='11' />

Upvotes: 0

Roman C
Roman C

Reputation: 1

Try

<s:if test="null != #request.METHOD_CALL || #request.METHOD_CALL == 'ADD' ">
    <s:set var="pre" value="%{'a'}" /> 
</s:if>
<s:else>
    <s:set var="pre" value="%{'e'}" /> 
</s:else>

<s:form action="saveMeetingAction" id="%{#pre}_editForm">
<s:textfield  name = "recDt" id = "%{#pre}_rcrdDt" size='11' maxlength='11' />

If you are using Struts tag, you can make OGNL expression inside attributes. To define a variable with the set tag you should use var attribute.

Upvotes: 1

Related Questions