Need Help in struts2 Validation
-
06-07-2019 - |
Question
I Have following in my struts.xml file
<action name="ProductVerification" class="com.frontend.ProductVerification">
<result name="success">/jsp/product_verification.jsp</result>
<result name="input">/jsp/product_verification.jsp</result>
<result name="error">/jsp/product_verification.jsp</result>
</action>
I have following in my html code
<s:form name="frmVerification" id="frmVerification" onsubmit="Javascript: return checkFrmVerification(this);" >
<s:select name="countryId" id="cmbcountryid" headerKey="0" headerValue="%{getText('label.Please_Select')}" list="%{countryListCombo}" listKey="countryId" listValue="country" value="countryId" cssClass="style2" onchange="Javascript: GetCities();" required="true" />
<s:submit name="submit" src="images/submit_btn.jpg" type="image" value="submit" />
</form>
I have execute method as below.
public String execute() throws Exception {
Session session = this.getHibernateSession();
Transaction tx = session.beginTransaction();
//Following will set combo box containing country list
this.setCountryListCombo();
tx.commit();
return SUCCESS;
}
I am overriding validate method as below.
@Override
public void validate() {
HttpServletRequest request = this.getServletRequest();
if(request.getParameter(countryId) == 0){
addFieldError("countryId", "Please select country");
}
}
Now when I execute my action it will show me form with countryId combo box filled with countries.
Now when I submit form without selecting combo box it should show me error.
But instead of showing error "Please select country" It gives me following error.
Struts Problem Report
Struts has detected an unhandled exception:
Messages: tag 'select', field 'list', name 'countryId': The requested list key '%{countryListCombo}' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}
File: org/apache/jasper/servlet/JspServletWrapper.java
Line number: 522
Can any one tell me why is it so?
It seems that after validate() method giving result="input", it is not calling execute() method and instead tries to show "/jsp/product_verification.jsp" page directly.
Please help me solving the problem.
Thanks.
Solution
Your assumption is correct, when a field error is added, it will by default return the "INPUT" result, causing the input result to be rendered. I would suggest looking into implementing preparable, which would allow you to always populate the combobox prior to page rendering.
OTHER TIPS
You need to add your combo box code in method just like below:
public String execute() throws Exception {
//add this code also as per your comobobx code
countryList = new ArrayList<Country>();
countryList.add(new Country(1, "India"));
countryList.add(new Country(1, "Shri Lanka"));
countryList.add(new Country(1, "USA"));
countryList.add(new Country(1, "Pakistan"));
countryList.add(new Country(1, "NewsLnad"));
//add this code also as per your comobobx code
Session session = this.getHibernateSession();
Transaction tx = session.beginTransaction();
//Following will set combo box containing country list
this.setCountryListCombo();
tx.commit();
return SUCCESS;
}
and also add the following tag in your jsp page
<s:actionerror />