user3682520
user3682520

Reputation: 71

Action's Boolean value is not binding from JSP after submitting the form in Struts 2

From JSP, only the Boolean value is not binding into action's Boolean variable. Other all variables are getting binding fine.

xBudgetFlag is Boolean variable which is not binding. Other all variables are binding.

Earlier from action to JSP also not worked, after added multiple getters for xBudgetFlag its working fine.

@Component
@Scope(value = "request")
public class PaActions extends ActionSupport {

private static final long serialVersionUID = 1L;

.
.
.
private Long keyId;
private String taxUnit;
private Long surveyTypeId;
private String surveyType;
private Long accountId;
private String status;
private Boolean parentFlag;
private Boolean xBudgetFlag;

// all setter getter are here
// Tried with different ways of getter method thinking isxBudgetFlag() is working, but still all three getters are threre in action class
public Boolean getxBudgetFlag() {
    return xBudgetFlag;
}
public Boolean isXBudgetFlag() {
    return xBudgetFlag;
}
public Boolean isxBudgetFlag() {
    return xBudgetFlag;
}

public void setxBudgetFlag(Boolean xBudgetFlag) {
    this.xBudgetFlag = xBudgetFlag;
}

@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public String save() {
    .
    .
    .
    
    System.out.println("----------isxBudget   --"+isxBudgetFlag()); // printing null
    System.out.println("---------- isXbudget  --"+isXBudgetFlag()); // printing null
    System.out.println("---------- GET XBUDGET --"+getxBudgetFlag());       // printing null
    System.out.println("---------- xBudgetFlag --"+xBudgetFlag);                    // printing null
    .
    .
    .
    return SUCCESS;
}
}

in JSP pa-edit.jsp:

<s:form id="PaActions" action="pa-submit" validate="true">
<s:token />
<s:hidden name="keyId" id="keyId"/>
.
.
.
<tr>
    <td class="formLabel"><label for="xBudgetFlag">X-Budget Flag</label></td>
    <td><s:checkbox property="xBudgetFlag" name="xBudgetFlag" /></td>
</tr>
.
.
.
<s:submit property="save" value="Save" id="save" />
</s:form>

struts.xml:

<action name="pa-submit" class="/PaActions" method="save">
<result name="success" type="redirectAction">
    <param name="actionName">pa-display</param>
    <param name="key">${keyId}</param>
</result>
<result name="input" type="tiles">.pa.edit</result>
<result name="invalid.token" type="tiles">.pa.edit</result>
</action>

tiles.xml:

<definition name=".pa.edit" >
<put-attribute name="title" value="Project KKK" />
<put-attribute name="body" value="/pages/pa-edit.jsp" />
<put-attribute name="parentMenu" value="pa" cascade="true" />
</definition>

Upvotes: 0

Views: 3951

Answers (1)

Roman C
Roman C

Reputation: 1

Try to use another setter name

public void setXBudgetFlag(Boolean xBudgetFlag) {
    this.xBudgetFlag = xBudgetFlag;
}

when the form is submitted the setter method is called to populate action class variables from the http parameters map. The correct setter method is determined to set the value and it depends on version of Struts/OGNL to find and invoke corresponding to a name the property accessor.

Since you have a property name with one lowercase letter first it won't be capitalized to calculate the name of the setter method, but in older versions the algorithm is different, so you have to either to add the setter method corresponding to the version of Struts/OGNL or rename the property to have at least two lowercase letters at first.

Also the property attribute doesn't exist on <s:checkbox> tag, use the name attribute.

So change

<td><s:checkbox property="xBudgetFlag" name="xBudgetFlag" /></td>

to

<td><s:checkbox name="xBudgetFlag" /></td>

Upvotes: 2

Related Questions