<script>
function submitForm() {
debugger;
document.getElementById("AdvanceSettlementForm").submit();
}
</script>
<s:form method="post" name="AdvanceSettlementForm" action="orders"
cssClass="form-horizontal" theme="simple">
<div class="form-group">
<label class="col-sm-2 control-label"> Settlement Voucher
Amount: <span class="mandatory">*</span>
</label>
<div class="col-sm-4 col-md-4 col-lg-4">
<div class="input-group">
<s:textfield maxlength="225" styleClass="form-control"
name="settlementAmount" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<a
href="${pageContext.request.contextPath}/orders/saveSettlement"
onclick=submitForm(); class="btn btn-default"> Save
Settlement</a>
</div>
</div>
</div>
</s:form>
I'm facing an issue with Struts 2 6.3.0.2 while trying to implement ModelDriven with a form submission. I have an AdvanceSettlementModel and AdvanceSettlementForm where AdvanceSettlementForm implements ModelDriven<AdvanceSettlementModel>. However, when submitting the form, the model values are not getting populated in the action class.
I've verified that the form is being submitted, but the model in AdvanceSettlementForm is not getting populated with the submitted values. Any insights or suggestions on what might be causing this issue would be greatly appreciated.
Here's a simplified version of my code:
AdvanceSettlementModel:
public class AdvanceSettlementModel implements Serializable {
private String settlementAmount;
// Getters and setters for other properties
}
AdvanceSettlementForm:
public class AdvanceSettlementForm implements Serializable, ModelDriven<AdvanceSettlementModel> {
private AdvanceSettlementModel model = new AdvanceSettlementModel();
@Override
public AdvanceSettlementModel getModel() {
return model;
}
// Other properties and methods
}
struts.xml:
<package name="your-package" extends="struts-default">
<action name="your-action" class="your-package.YourActionClass" method="execute">
<interceptor-ref name="modelDriven"/>
<result>/your-result.jsp</result>
</action>
</package>
Controller:
public class YourActionClass extends ActionSupport implements ModelDriven<AdvanceSettlementForm> {
private AdvanceSettlementForm form = new AdvanceSettlementForm();
@Override
public AdvanceSettlementForm getModel() {
return form;
}
public String execute() {
// Access model properties like form.getModel().getSettlementAmount()
// However, values are not getting populated
// Your business logic here
return SUCCESS;
}
}
Additionally, in my Struts action class, I noticed a method shouldRestrictToGET in the RestActionInvocation class:
// RestActionInvocation class
private boolean shouldRestrictToGET() {
return !hasErrors
&& !"get".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())
&& restrictToGet;
}
You have a problem with implementing
ModelDriveninterface. OnlyActionclass should implement it. In this way you return a model bean withgetModel(). Other form bean is obsolete.You should also remove reference to
modelDriveninterceptor. Because you have overridden Struts default interceptors configuration for the action. In this way thedefaultStackwon't be configured. You can check a runtime configuration for you action to see which interceptors are configured.In the JSP you can use
How to use struts2 submit tag as button without submitting the form? is the reference how you can use
<s:submit>using a JavaScript function.