我想更新用户表中的记录。我在 maven 中配置的 seam+jsf 项目。我试图成功,但失败更多。
我来这里是为了得到你们朋友的帮助。希望有人能帮助我。
提前致谢..
我的更新页面:
<!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:s="http://jboss.com/products/seam/taglib"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:clrp="http://panorosys.com/products/clrp/taglib"
xmlns:p="http://primefaces.prime.com.tr/ui">
<ui:composition>
<div class="panel-content">
<h:form class="input-list" id="createuser">
<h:panelGrid border="1" class="creatUserDetailsTable">
<f:facet name="header">
<h:outputText value="Personal Details" />
</f:facet>
<s:decorate id="firstNameDec" template="../../../secure/edit.xhtml">
<ui:define name="label">First Name:</ui:define>
<h:inputText tabindex="1" id="firstName" type="text"
value="#{currentUser.firstName}" required="true">
<f:validateLength minimum="3" maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="lastNameDec" template="../../../secure/edit.xhtml">
<ui:define name="label">Last Name:</ui:define>
<h:inputText tabindex="2" id="lastName" type="text"
value="#{currentUser.lastName}" required="true">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="emailIdDec" template="../../../secure/edit.xhtml">
<ui:define name="label">Email Id:</ui:define>
<h:inputText tabindex="3" id="emailId" type="text"
value="#{currentUser.emailAddress}">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="homeNumberDec" template="../../../secure/edit.xhtml">
<ui:define name="label">Home Number:</ui:define>
<h:inputText tabindex="4" id="homeNumber" type="text"
value="#{currentUser.homeNumber}">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="cellNumberDec" template="../../../secure/edit.xhtml">
<ui:define name="label">Cell Number:</ui:define>
<h:inputText tabindex="5" id="cellNumber" type="text"
value="#{currentUser.cellNumber}" required="true">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="officeNumberDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Office Number:</ui:define>
<h:inputText tabindex="6" id="officeNumber" type="text"
value="#{currentUser.officeNumber}">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
</h:panelGrid>
<h:panelGrid border="1" columns="2" class="createUserAddressTable1">
<f:facet name="header">
<h:outputText value="Address" />
</f:facet>
<h:panelGrid border="1" class="createcurrentUserAddressTable">
<f:facet name="header">
<h:outputText value="Residential" />
</f:facet>
<s:decorate id="residentialAddress1Dec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Address 1:</ui:define>
<h:inputText tabindex="7" id="residentialAddress1" type="text"
value="#{currentUser.residentialAddress.address1}"
required="true">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="residentialAddress2Dec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Address 2:</ui:define>
<h:inputText tabindex="7" id="residentialAddress2" type="text"
value="#{currentUser.residentialAddress.address2}"
required="true">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="residentialCityDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">City:</ui:define>
<h:inputText tabindex="9" id="residentialCity" type="text"
value="#{currentUser.residentialAddress.city}" required="true">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="residentialCountryDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Country :</ui:define>
<h:inputText tabindex="10"
value="#{currentUser.residentialAddress.countryId}"
id="residentialCountry" type="text"
class="resi-country-class cntry" required="true">
</h:inputText>
</s:decorate>
<s:decorate id="residentialStateDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">State :</ui:define>
<h:inputText tabindex="11"
value="#{currentUser.residentialAddress.stateId}"
id="residentialState" type="text" required="true">
</h:inputText>
</s:decorate>
<s:decorate id="residentialZipcodeDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Zip code :</ui:define>
<h:inputText tabindex="12" id="residentialZipcode" type="text"
value="#{currentUser.residentialAddress.zipCode}" required="true">
<f:validateLength minimum="0" maximum="6" />
</h:inputText>
</s:decorate>
</h:panelGrid>
<h:panelGrid border="1" class="createUserAddressTable">
<f:facet name="header">
<h:outputText value="Shipping" />
</f:facet>
<s:decorate id="shippingAddress1Dec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Address 1:</ui:define>
<h:inputText tabindex="14" id="shippingAddress1" type="text"
value="#{currentUser.shippingAddress.address1}" required="true">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="shippingAddress2Dec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Address 2:</ui:define>
<h:inputText tabindex="15" id="shippingAddress2" type="text"
value="#{currentUser.shippingAddress.address2}" required="true">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="shippingCityDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">City:</ui:define>
<h:inputText tabindex="16" id="shippingCity" type="text"
value="#{currentUser.shippingAddress.city}" required="true">
<f:validateLength maximum="20" />
</h:inputText>
</s:decorate>
<s:decorate id="shippingCountryDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Country :</ui:define>
<h:inputText tabindex="17"
value="#{currentUser.shippingAddress.countryId}"
id="shippingCountry" type="text"
class="shipp-country-class cntry" required="true">
</h:inputText>
</s:decorate>
<s:decorate id="shippingStateDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">State :</ui:define>
<h:inputText tabindex="18"
value="#{currentUser.shippingAddress.stateId}" id="shippingState"
type="text" class="shipp-state-class state" required="true">
</h:inputText>
</s:decorate>
<s:decorate id="shippingZipcodeDec"
template="../../../secure/edit.xhtml">
<ui:define name="label">Zip code :</ui:define>
<h:inputText tabindex="19" id="shippingZipcode" type="text"
value="#{currentUser.shippingAddress.zipCode}" required="true">
<f:validateLength minimum="0" maximum="6" />
</h:inputText>
</s:decorate>
</h:panelGrid>
</h:panelGrid>
<h:commandButton value="Update" tabindex="20"
action="#{userAction.update}" class="usersubmit">
<f:param name="userId" value="#{currentUser.userId}" />
</h:commandButton>
</h:form>
<h:messages globalOnly="true" />
<s:remote include="currentUserListAction" />
<script type="text/javascript">
$().ready(function() {
loadCountries("#{restEasyPath}/lookup/Country/");
$(".shipp-country-class").val(#{currentUserAction.currentUser.shippingAddress.countryId});
$(".resi-country-class").val(#{currentUserAction.currentUser.residentialAddress.countryId});
$('#firstName').focus();
$("#mydropdownlist").val("thevalue");
});
$(".shipp-country-class").change(function(e){
var countryId=$(".shipp-country-class").val();
loadState("#{restEasyPath}/lookup/State/"+countryId+'/',"shipp" );
});
$(".resi-country-class").change(function(e){
var countryId=$(".resi-country-class").val();
loadState("#{restEasyPath}/lookup/State/"+countryId+'/',"resi" );
});
</script>
</div>
</ui:composition>
</html>
动作类:
@Name("userAction")
@Stateless
@AutoCreate
public class UserActionImpl implements UserAction {
@Out(value = "currentUser", scope = ScopeType.CONVERSATION)
@In(value = "currentUser", scope = ScopeType.CONVERSATION, required = false)
private CLRPUser currentUser;
@In
private UserService userService;
@RequestParameter
private Integer userId;
private static final long serialVersionUID = 8282995226262125676L;
public String update() {
userService.saveOrUpdate(currentUser);
return "success";
}
public String getCLRP() {
return Constants.SUCCESS_STR;
}
public CLRPUser getCurrentUser() {
return currentUser;
}
public void setCurrentUser(CLRPUser currentUser) {
this.currentUser = currentUser;
}
}
服务等级:
@Name("userService")
@Stateless
@AutoCreate
public class UserServiceImpl implements UserService {
private static final long serialVersionUID = 1L;
@In
private UserDAO userDAO;
@In
private LoggedInUser loggedInUser = null;
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public UserServiceImpl() {
super();
}
public Integer saveOrUpdate(CLRPUser user) {
user.setLastUpdatedBy(loggedInUser.getId());
user.getResidentialAddress().setLastUpdatedBy(loggedInUser.getId());
user.getShippingAddress().setLastUpdatedBy(loggedInUser.getId());
user.getUserLogin().setLastUpdatedBy(loggedInUser.getId());
user.getUserRoles().setLastUpdatedBy(loggedInUser.getId());
// user.getTransaction().setLastUpdatedBy(loggedInUser.getId());
UserEntity userEntity = UserMapper.mapUserEntity(user);
Integer userId = userDAO.saveOrUpdate(userEntity);
System.out.println("User Id saved " + userId);
return userId;
}
}
}
映射器类:
public class UserMapper {
public static UserEntity mapUserEntity(CLRPUser user) {
UserEntity userEntity = new UserEntity();
userEntity.setUserId(user.getUserId());
userEntity.setEmailAddress(user.getEmailAddress());
userEntity.setFirstName(user.getFirstName());
userEntity.setLastName(user.getLastName());
userEntity.setHomeNumber(user.getHomeNumber());
userEntity.setOfficeNumber(user.getOfficeNumber());
userEntity.setCellNumber(user.getCellNumber());
userEntity.setCardId(user.getCardId());
AuditInfo auditInfo = new AuditInfo();
auditInfo.setLastUpdatedBy(user.getLastUpdatedBy());
auditInfo.setLastUpdatedTime(user.getLastUpdatedTime());
userEntity.setAuditInfo(auditInfo);
userEntity.setType(user.getType());
userEntity.setPreferenceId(user.getPreferenceId());
userEntity.setASPCustomerId(user.getAspCustomerId());
user.getResidentialAddress().setAddressType("R");
user.getShippingAddress().setAddressType("S");
AddressEntity residential = AddressMapper.mapAddressEntity(user
.getResidentialAddress());
AddressEntity shipping = AddressMapper.mapAddressEntity(user
.getShippingAddress());
List<AddressEntity> addressess = new ArrayList<AddressEntity>();
addressess.add(residential);
addressess.add(shipping);
userEntity.setAddresses(new HashSet<AddressEntity>(addressess));
user.getUserLogin().setUserID(user.getUserId());
user.getUserLogin().setUserName(user.getCellNumber());
user.getUserLogin().setPassword(user.getCellNumber());
UserLoginEntity userlogin = UserLoginMapper.mapUserLoginEntity(user
.getUserLogin());
List<UserLoginEntity> userlogins = new ArrayList<UserLoginEntity>();
userlogins.add(userlogin);
userEntity.setUserlogins(new HashSet<UserLoginEntity>(userlogins));
user.getUserRoles().setUserId(user.getUserId());
user.getUserRoles().setRoleID(1);
UserRoleEntity userRoleEntity = UserRoleMapper.mapUserRoleEntity(user.getUserRoles());
List<UserRoleEntity> roles = new ArrayList<UserRoleEntity>();
roles.add(userRoleEntity);
userEntity.setRoles(new HashSet<UserRoleEntity>(roles));
return userEntity;
}
}
JPA_DAO 类:
@Name("userDAO")
@Stateless
@AutoCreate
public class JPAUserDaoImpl implements UserDAO {
private static final long serialVersionUID = -6173881454668735683L;
@PersistenceContext(unitName = "clrp")
private EntityManager entityManager;
Logger logger = Logger.getLogger(this.getClass());
public void setEntityManager(EntityManager em) {
this.entityManager = em;
}
public Integer saveOrUpdate(UserEntity newUser) {
entityManager.merge(newUser);
return newUser.getUserId();
}
}
}