1

我有一个struts2问题,它只在出现验证错误时发生(当我第一次加载我的页面时不会发生,如果没有发生验证错误也不会发生)如果我删除选择标签,它工作正常,并且在我提交表单后显示验证错误消息,但我需要这些选择,并且它们具有来自数据库的动态信息,因此每次显示我的表单时都需要预加载它们。我试图将deptListandroleList放入 valuestack 或将它们放入会话中,同样的事情发生了。

这是动作类代码的一部分:

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;
}

这是 JSP 页面:

<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>

这是来自 struts 报告的错误:

标签“选择”,字段“列表”,名称“empModel.deptModel.uuid”:请求的列表键“deptList”无法解析为集合/数组/映射/枚举/迭代器类型。示例:人或人。{name}

这是我的验证 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>
4

1 回答 1

1

当您提交表单时,验证错误会阻止操作执行,因为workflow拦截器返回结果 INPUT。您可以通过在操作方法上添加注释来配置此拦截器

@InputConfig(methodName="input")
public String save(){
    if(empModel.getUuid() == null ){
        empEbi.save(empModel,roleUuids);
    }else{
        empEbi.update(empModel,roleUuids);
    }
    return TO_LIST;
}

它将调用input拦截器中的方法来初始化列表,然后再INPUT通过该方法返回结果。

于 2016-02-23T20:51:17.823 回答