I have a jsp containing three text fields which are indexed property, I needs
to validate all the text box contains a value other than null or empth string.
When I click on save I got the error (see the bottom). Your helps are highly
appreciated!
jsp:
<html:errors/>
<html:form action="/testingSaveAction" method="post">
(1) indexed property - text box
<br />
<logic:iterate name="testingForm" property="listOfItems" id="labelValue">
Fields to be validated: <html:text name="labelValue" property="value"
indexed="true"/><br />
</logic:iterate>
<br /><br />
<html:submit property="submit">save</html:submit>
</html:form>
ActionForm:
public class TestingForm extends ValidatorActionForm {
private LabelValueBean[] listOfItems;
private LabelValueBean labelValue=null;
public LabelValueBean getLabelValue(int index) {
return (LabelValueBean)listOfItems[index];
}
public void setLabelValue(LabelValueBean bean, int index) {
listOfItems[index]= bean;
}
public LabelValueBean[] getListOfItems() {
return listOfItems;
}
public void setListOfItems(LabelValueBean[] beans) {
listOfItems = beans;
}
}
Actions:
public class DisplayAction extends org.apache.struts.action.Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws NestedException {
TestingForm testingForm = (TestingForm) form;
if (null == testingForm || null ==testingForm.getListOfItems()) {
LabelValueBean[] listOfItems = new LabelValueBean[3];
LabelValueBean bean1 = new LabelValueBean("1", "");
LabelValueBean bean2 = new LabelValueBean("2", "");
LabelValueBean bean3 = new LabelValueBean("3", "");
listOfItems[0]=bean1;
listOfItems[1]=bean2;
listOfItems[2]=bean3;
testingForm.setListOfItems(listOfItems);
}
return mapping.findForward("success");
}
}
public class TestingSaveAction extends org.apache.struts.action.Action{
public ActionForward executeAction(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws NestedException {
TestingForm testingForm = (TestingForm) form;
return mapping.findForward("success");
}
struts-config.xml
<global-forwards>
<forward name="displayTesting" path="/displayAction.do"/>
</global-forwards>
<form-bean name="testingForm" type="MyPath.TestingForm" />
<action path="/displayAction" type="MyPath.DisplayAction"
name="testingForm" scope="request"
validate="false" >
<forward name="success" path="/MyPath/testing.jsp" />
</action>
<action path="/testingSaveAction" type="MyPath.TestingSaveAction"
name="testingForm" scope="request"
validate="true" input="displayTesting">
<forward name="success" path="/MyPath/successful.jsp" />
</action>
validation.xml
<form name="/testingSaveAction">
<field property="value" indexedListProperty="listOfItems"
depends="required">
<msg name="required" key="Keytoerrormessage.cannotblank"/>
</field>
</form>
Errors when click on save:
E SRVE0026E: [Servlet Error]-[BeanUtils.populate]:
java.lang.reflect.InvocationTargetException: java.lang.NullPointerException
at MyPath.TestingForm.getLabelValue(TestingForm.java:75)
at java.lang.reflect.Method.invoke(Native Method)
at
org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:493)
where line 75 is the place: return (LabelValueBean)listOfItems[index]; in the
actionform
---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
starting at 1¢/min.