ognl获取user.roles[0].roleId报错!!!,user里面有集合roles,想通过user访问roles[0]的roleId,却报错!!!
package com.zjlolife.test1.domain;
import java.util.List;
public class User {
private String id;
private String name;
private List<Role> roles;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
}
package com.zjlolife.test1.domain;
public class Role {
private String roleId;
private String name;
public String getRoleId() {
return roleId;
}
public void setRoleId(String roleId) {
this.roleId = roleId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.zjlolife.test1.action;
import com.zjlolife.test1.domain.User;
public class FormAction {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() {
System.out.println(user.getRoles().get(0).getRoleId());
return "success";
}
}
错误信息:
ognl.OgnlException: source is null for getProperty(null, "roles")
如果是roles[0],roleId那就好访问了,但是下面的这种user.roles[0].roleId还是会报错啊
不知道如何访问user.roles[0].roleId,大家帮忙看下,格式应该如何写?
------解决方案--------------------
举个例子 比如name="user.name" struts传参的时候是在先在action里实例化user 然后调用user的setName方法赋值,而name="user.roles[0].roleId" 必须先在action中实例化user 再在user中实例化roles(而这个应该是struts没法做到的)
------解决方案--------------------
你还是debug一下,看你的action中传过来!