表单提交时datatable中的数据更新不到bean中

dawnstars 2007-08-17
在调用saveOrUpdate方法时,availRoles中的数据始终为空,请帮忙看看
bean 代码

public class UserForm extends BasePage implements Serializable {
private static final long serialVersionUID = -1141119853856863204L;

private SysRoleManager sysRoleManager;

private SysDepartmentManager sysDepartmentManager;

private SysUser user = new SysUser();

/**
* 用户所属部门
*/
private Long departmentId;

/**
* 用户角色
*/
private List<SysRole> availableRoles=new ArrayList<SysRole>();

private List<SelectItem> availableDepartments;

public List<SelectItem> getAvailableDepartments() {
availableDepartments = new ArrayList<SelectItem>();
Iterator it = sysDepartmentManager.findAll().iterator();
SelectItem si = null;
SysDepartment dep = null;
si = new SelectItem(null, "select...");
availableDepartments.add(si);
while (it.hasNext()) {
dep = (SysDepartment) it.next();
si = new SelectItem(dep.getDepartmentId(), dep.getDepartmentName());
availableDepartments.add(si);
}
return availableDepartments;
}

public List<SysRole> getAvailableRoles() {
if (availableRoles == null || availableRoles.size() == 0) {
if (departmentId != null) {
availableRoles = sysRoleManager
.findByDepartmentId(departmentId);
} else if (user.getSysDepartment() != null) {
availableRoles = sysRoleManager.findByDepartmentId(user
.getSysDepartment().getDepartmentId());
}
}
return availableRoles;
}

public void setAvailableRoles(List<SysRole> availableRoles) {
this.availableRoles = availableRoles;
}

public SysUser getUser() {
return user;
}

public void setUser(SysUser user) {
this.user = user;
}

public void setSysRoleManager(SysRoleManager roleManager) {
this.sysRoleManager = roleManager;
}

/**
* 添加或修改用户信息时取消操作
*
* @return 导航页面
*/
public String cancel() {
if (log.isDebugEnabled()) {
log.debug("Entering 'cancel' method");
}
return "cancel";
}

/**
* 进入修改页面
*
* @return
*/
public String edit() {
if (getParameter("userId") != null) {
Long userId = new Long(getParameter("userId"));
getUserById(userId);
}
return "editUser";
}

/**
* 根据用户ID查找用户
*
* @return
*/
private void getUserById(Long userId) {
user = sysUserManager.findById(new Long(getParameter("userId")));
}

/**
* 进入查看用户页面
*/
public String viewUser() {
if (getParameter("userId") != null) {
Long userId = new Long(getParameter("userId"));
getUserById(userId);
}
return "viewUser";
}

/**
* 保存或修改用户信息
*
* @return
*/
public String saveOrUpdate() {
try {
if (departmentId != null) {
user.setSysDepartment(sysDepartmentManager
.findById(departmentId));
if (availableRoles != null && availableRoles.size() > 0) {
SysRole role = null;
for (Iterator it = availableRoles.iterator(); it.hasNext();) {
role = (SysRole) it.next();
if (role.isSelected())
user.addSysRole(role);
}
}
}
if (user.getUserId() != null) {
sysUserManager.update(user);
} else {
sysUserManager.save(user);
}
return "userAdded";
} catch (UserExistsException e) {
e.printStackTrace();
return "userAddedFail";
}
}

/**
* 删除用户信息
*
* @return
*/
public String delete() {
if (getParameter("userId") != null) {
Long userId = new Long(getParameter("userId"));
getUserById(userId);
}
sysUserManager.delete(user);
return "deleteUser";
}

// Form Controls ==========================================================

public void setSysDepartmentManager(
SysDepartmentManager sysDepartmentManager) {
this.sysDepartmentManager = sysDepartmentManager;
}

public Long getDepartmentId() {
if (user.getSysDepartment() != null) {
departmentId = user.getSysDepartment().getDepartmentId();
}
return departmentId;
}

public void setDepartmentId(Long departmentId) {
this.departmentId = departmentId;
}
}


页面代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
xmlns:rich="http://richfaces.ajax4jsf.org/rich"
xmlns:c="http://java.sun.com/jstl/core" xml:lang="en" lang="en">

<f:view>
<ui:composition template="/layout/MainFrame.xhtml">
<ui:define name="title">Add or update User</ui:define>
<ui:define name="content">
<h:form id="addUserForm">


<h:messages globalOnly="true"></h:messages>
<h:inputHidden id="userId" value="#{userForm.user.userId}"></h:inputHidden>
<h:inputHidden value="#{userForm.user.version}" id="version">
</h:inputHidden>
<h:panelGrid border="1" columns="3">
<h:outputText value="Username"></h:outputText>
<h:inputText value="#{userForm.user.username}" id="username"
required="true">
<f:validateLength minimum="3" maximum="60"></f:validateLength>
</h:inputText>
<h:message for="username"></h:message>

<h:outputText value="Password"></h:outputText>
<h:inputSecret value="#{userForm.user.password}" id="password"
required="true">
<f:validateLength minimum="6" maximum="24"></f:validateLength>
</h:inputSecret>
<h:message for="password"></h:message>

<h:outputText value="ConfirmPassword"></h:outputText>
<h:inputSecret value="#{userForm.user.confirmPassword}"
id="confirmPassword" required="true">
<f:validateLength minimum="6" maximum="24"></f:validateLength>
</h:inputSecret>
<h:message for="confirmPassword"></h:message>

<h:outputText value="FirstName"></h:outputText>
<h:inputText value="#{userForm.user.firstName}" id="firstName"
required="true">
<f:validateLength maximum="50"></f:validateLength>
</h:inputText>
<h:message for="firstName"></h:message>

<h:outputText value="LastName"></h:outputText>
<h:inputText value="#{userForm.user.lastName}" id="lastName"
required="true">
<f:validateLength maximum="50"></f:validateLength>
</h:inputText>
<h:message for="lastName"></h:message>

<h:outputText value="Sex"></h:outputText>
<h:selectOneRadio value="#{userForm.user.sex}" id="sex"
required="true">
<f:selectItem itemLabel="Male" itemValue="M" />
<f:selectItem itemLabel="Female" itemValue="F" />
</h:selectOneRadio>
<h:message for="sex"></h:message>

<h:outputText value="Birthday"></h:outputText>
<h:panelGroup>
<h:inputText value="#{userForm.user.birthday}" id="birthday"
required="true">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>(yyyy-mm-dd)
</h:panelGroup>
<h:message for="birthday"></h:message>

<h:outputText value="Country"></h:outputText>
<h:inputText value="#{userForm.user.address.country}" id="country">
<f:validateLength maximum="100"></f:validateLength>
</h:inputText>
<h:message for="country"></h:message>

<h:outputText value="Province"></h:outputText>
<h:inputText value="#{userForm.user.address.province}"
id="province" required="true">
<f:validateLength maximum="100"></f:validateLength>
</h:inputText>
<h:message for="province"></h:message>

<h:outputText value="City"></h:outputText>
<h:inputText value="#{userForm.user.address.city}" id="city"
required="true">
<f:validateLength maximum="100"></f:validateLength>
</h:inputText>
<h:message for="city"></h:message>

<h:outputText value="Address"></h:outputText>
<h:inputText value="#{userForm.user.address.address}" id="address"
required="true">
<f:validateLength maximum="150"></f:validateLength>
</h:inputText>
<h:message for="address"></h:message>

<h:outputText value="PostalCode"></h:outputText>
<h:inputText value="#{userForm.user.address.postalCode}"
id="postalCode" required="true">
<f:validateLength minimum="6" maximum="15"></f:validateLength>
</h:inputText>
<h:message for="postalCode"></h:message>

<h:outputText value="PhoneNumber"></h:outputText>
<h:inputText value="#{userForm.user.phoneNumber}" id="phoneNumber">
<f:validateLength maximum="255"></f:validateLength>
</h:inputText>
<h:message for="phoneNumber"></h:message>

<h:outputText value="Email"></h:outputText>
<h:inputText value="#{userForm.user.email}" id="email"
required="true">
<f:validateLength maximum="255"></f:validateLength>
</h:inputText>
<h:message for="email"></h:message>

<h:outputText value="Website"></h:outputText>
<h:inputText value="#{userForm.user.website}" id="website"
required="true">
<f:validateLength maximum="255"></f:validateLength>
</h:inputText>
<h:message for="website"></h:message>

<h:outputText value="Enabled"></h:outputText>

<h:selectBooleanCheckbox value="#{userForm.user.enabled}"
id="enabled"></h:selectBooleanCheckbox>
<h:message for="enabled"></h:message>

<h:outputText value="AccountExpired"></h:outputText>
<h:selectBooleanCheckbox value="#{userForm.user.accountExpired}"
id="accountExpired"></h:selectBooleanCheckbox>
<h:message for="accountExpired"></h:message>

<h:outputText value="AccountLocked"></h:outputText>
<h:selectBooleanCheckbox value="#{userForm.user.accountLocked}"
id="accountLocked"></h:selectBooleanCheckbox>
<h:message for="accountLocked"></h:message>

<h:outputText value="CredentialsExpired"></h:outputText>
<h:selectBooleanCheckbox
value="#{userForm.user.credentialsExpired}"
id="credentialsExpired"></h:selectBooleanCheckbox>
<h:message for="credentialsExpired"></h:message>

<h:outputText id="department" value="Department:"></h:outputText>
<h:selectOneMenu id="departmentList"
value="#{userForm.departmentId}"
style="height: 22px; width: 74px">
<a4j:support event="onchange" reRender="roleListRichTable"
ajaxSingle="true">

</a4j:support>
<f:selectItems id="siDepartments"
value="#{userForm.availableDepartments}" />
</h:selectOneMenu>
<h:dataTable id="roleListRichTable" border="1"
value="#{userForm.availableRoles}" var="row">
<h:column>
<f:facet name="header">
<h:outputText value="Select" />
</f:facet>
<h:selectBooleanCheckbox id="selRole" value="#{row.selected}"
rendered="true"></h:selectBooleanCheckbox>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="RoleName" />
</f:facet>
<h:outputText id="roleName" value="#{row.roleName}"
rendered="true"></h:outputText>
</h:column>
</h:dataTable>

<h:commandButton value="Submit" action="#{userForm.saveOrUpdate}">
<f:param name="userId" value="#{userForm.user.userId}"></f:param>
</h:commandButton>
<h:commandButton value="Cancel" action="#{userForm.cancel}"
immediate="true"></h:commandButton>
</h:panelGrid>

</h:form>
</ui:define>
</ui:composition>
</f:view>
</html>
lyxh_2003 2007-09-05
我也遇到同样的原因。。。
Global site tag (gtag.js) - Google Analytics