I have a struts2 problem, it only happens when there is a validation error (it doesn't happen when I load my page for the first time and doesn't happen if the validation error doesn't occur) If I remove select tags, it works fine, and it shows the validation error messages after I submit the form, but I need those selects, and they have dynamic information from the DB, so they need to be preloaded each time my form is displayed.
I tried to put the deptList
and roleList
in valuestack or put them to session, same thing happened.
This is the part of the Action Class code:
private List<DeptModel> deptList;
private List<RoleModel> roleList;
public List<RoleModel> getRoleList() {
return roleList;
}
public void setRoleList(List<RoleModel> roleList) {
this.roleList = roleList;
}
public List<DeptModel> getDeptList() {
return deptList;
}
public void setDeptList(List<DeptModel> deptList) {
this.deptList = deptList;
}
public String input() {
roleList = roleEbi.getAll();
this.setRoleList(roleList);
deptList = deptEbi.getAll();
this.setDeptList(deptList);
if (empModel.getUuid() != null) {// for data display when doing modify
empModel = empEbi.get(empModel.getUuid());
roleUuids = new Long[empModel.getRoles().size()];
int i = 0;
for (RoleModel temp : empModel.getRoles()) {
roleUuids[i++] = temp.getUuid();
}
}
return INPUT;
}
public String save(){
if(empModel.getUuid() == null ){
empEbi.save(empModel,roleUuids);
}else{
empEbi.update(empModel,roleUuids);
}
return TO_LIST;
}
This is the JSP page:
<s:form action="emp_save" method="post" onsubmit="return checkForm();">
<s:textfield id="username" name="empModel.userName" />
<s:textfield id="password" size="25" name="empModel.pwd"/>
<s:select name="empModel.deptModel.uuid" list="deptList" style="width:190px" listKey="uuid" listValue="name" />
<s:checkboxlist name="roleUuids" list="roleList" listKey="uuid" listValue="name"/>
<a href="javascript:document.forms[0].submit()"><img src="images/save.jpg" border="0" width="81px"/></a>
</s:form>
This is the error from the struts report:
tag 'select', field 'list', name 'empModel.deptModel.uuid': The requested list key 'deptList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}
This is my validation xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="empModel.userName">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>User Name can not be null ~~~</message>
</field-validator>
</field>
<field name="empModel.pwd">
<field-validator type="requiredstring">
<message>Password can not be null ~~~</message>
</field-validator>
</field>
</validators>
When you submit form the validation errors prevent the action execution, because the
workflow
interceptor returns result INPUT. You can configure this interceptor via adding annotation on the action methodIt will call method
input
from the interceptor to initialize lists before returningINPUT
result by this method.