1.要获取集合list的Action类
public class RoleAction extends ActionSupport {
private Role role;
public RoleService roleService;
public String message;
public List list;
public static ActionContext ac = null;
public String getMessage() {
return message;
}
@Resource(name = "roleService")
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
public List getList() {
return list;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String List() throws Exception {
/*
* 浏览权限
*/
if (this.isRole()) {
this.list = this.roleService.get();
System.out.println(this.getList());
return "list";
} else
return ERROR;
}
}
2.struts.xml配置action
<action name="nuserAction_*" class="nuserAction" method="{1}">
<interceptor-ref name="nuserInterceptorStack"></interceptor-ref>
<result name="list">/user/ListUser.jsp</result>
<result name="loginSuccess">main.jsp</result>
</action>
<action name="roleAction_*" class="roleAction" method="{1}">
<result name="list">/role/ListRole.jsp</result>
</action>
3.adduser.jsp中代码如下:
<s:form action="nuserAction_Add" method="post">
<s:action name="roleAction_List" id="role"></s:action>
<s:select list="#role.list" label="权限" name="nuser.role.id" headerKey="-1" headerValue="--请选择--" listKey="id" listValue="position"></s:select>
<s:textfield name="nuser.name" label="用户名" value=""></s:textfield>
<s:password name="nuser.password" label="密码" value=""></s:password>
<s:submit value="提交"></s:submit>
<s:reset value="重置"></s:reset>
</s:form>
此处通过action标签 先执行roleAction_List 来获取 属性list值 然后赋给select标签的list属性 来生成下拉菜单。
有时候能够正常显示 但是有时又会报错 异常如下:
严重: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list', name 'nuser.role.id': The requested list key '#role.list' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:237)
不知道什么情况 请各位帮忙看看!