public class SystemPrivilege {

private SystemPrivilegePK id;
/* 权限名称 */
@Column(length=20,nullable=false)
private String name;
...简略一部分get,set
}
public class SystemPrivilegePK implements Serializable{
private static final long serialVersionUID = -7393112044057119072L;
/* 模块名 */
@Column(length=25, name="model")
private String model;
/* 权限值 */
@Column(length=25, name="privilegeValue")
private String privilegeValue;
...简略一部分get,set
}public class PrivilegeGroup {
@Id @GeneratedValue
private Integer id;

@Column(length=20,nullable=false)
private String name;

@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER)
@JoinTable(name="group_privilege",joinColumns=@JoinColumn(name="groupid"),
inverseJoinColumns={@JoinColumn(name="model", referencedColumnName="model"),
@JoinColumn(name="privilegeValue", referencedColumnName="privilegeValue")})...简略一部分get,set
}现在要进行权限组增加功能。
jsp叶面如下<input type="text" name="name"  size="32" maxlength="32" />
<input type="checkbox" name="privileges" value="department,delete">
部门删除 
  
<input type="checkbox" name="privileges" value="department,insert">
部门添加 
  
<input type="checkbox" name="privileges" value="department,update">
部门修改 
  
<input type="checkbox" name="privileges" value="department,view">
部门查看 <br>
  
<input type="checkbox" name="privileges" value="privilegeGroup,delete">
权限组删除 
  
<input type="checkbox" name="privileges" value="privilegeGroup,insert">
权限组添加 
  
<input type="checkbox" name="privileges" value="privilegeGroup,update">
权限组修改 
  
<input type="checkbox" name="privileges" value="privilegeGroup,view">
权限组查看
提交action中如下。是struts2框架下。接受privileges参数出问题。咨询一下。public class PrivilegeGroupManageAction {
private String name;
private SystemPrivilegePK[] privileges;
private Integer groupid;

public String addUI() throws Exception{
ActionContext.getContext().put("privileges", systemPrivilegeService.getScrollData().getResultlist());
return "add";
}

public String add() throws Exception{System.out.println(getName());
System.out.println(getPrivileges()); System.out.println("添加成功");
return "message";

}文件代码完毕!!问题如下
若action中。
 private SystemPrivilegePK[] privileges;定义,则System.out.println(getPrivileges());不能输出任何内容 private String[] privileges;定义,则System.out.println(getPrivileges());能输出Ljava.lang.String;@aee320原先是struts1下,在form中定义。public class PrivilegeGroupForm extends BaseForm {
private Integer groupid;
private String name;
private SystemPrivilegePK[] privileges;则参数接受正常!请问怎么搞!?谢谢

解决方案 »

  1.   

    你那几个action的属性属性有get/set吗.?
      

  2.   

    有的!贴上来,避免代码过多,所以省略。要不然使用string怎么可能传递来!
      

  3.   

    I See.! 我知道是怎么回事了.! - -!
      

  4.   

    LZ加点分嘛.!  这个问题涉及到的知识会触到bug的.! 不过你用html标签不会出问题 !
      

  5.   

    算了.! 助人为乐.!
    是这样的,Struts2会自动帮你转换一些类型, 比如说基本类型,还有struts2支持的,比如说Date,Enum等等.!  如果不在这些类型范围之内, 你就要自定义转换器.!
    这里你就要定义一个类型转换器.!
    第一步:继承DefaultTypeConverter和StrutsTypeConverter两个类中的一个,以DefaultTypeConverter为例好了,重写convertValue(Object value, Class toType)这个方法.!
    if(toType == SystemPrivilegePK[].class){
        String[] str = (String[])value;
        SystemPrivilegePK[] ret = new SystemPrivilegePK[str.length];
        for(int i = 0; i < str.length; i++){
            ret[i] = new SystemPrivilegePK(str[i].split(",")[0],str[i].split(",")[1]);
        }
        return ret;
    }else{
        //把上面的反过来搞, 把SystemPrivilegePK[]转成String,然后return
    }
    第二步:注册转换器
    ActionClassName-conversion.properties;//action类名自己去替换,这个文件放在与action同包下.!
    文件里面写上
    SystemPrivilegePK的完整类名 = 转换器的完整类名 第三步  按照上面两步搞完,如果出错, 在你的页面上的checkBox的name privileges[0] 逐一加上索引访问(太久没做数组的转换器了,细节不太记得页面要不要索引访问)友情提示: 千万不要用struts2标签, 否则撞到TemplateEngineException不要怪我没提醒过你, 这是个bug
      

  6.   


    谢谢。搞定了。struts2类型转换器注册了即可。原来是struts1下,我有个类型转换器,没有起作用,还不知道。呵呵。