我有以下内容:
查看:
<h:form class="form-signin">
<h:inputText id="name" name="name" value="#{userCabinetController.user.name}" required="true" requiredMessage="Поле имя не заполнено" class="form-control" type="name"/>
<h:inputText id="email" name="email" value="#{userCabinetController.user.email}" required="true" requiredMessage="Введите логин (email)" class="form-control" type="email"/>
<h:inputSecret id="password" name="password" value="#{userCabinetController.user.password}" required="true" requiredMessage="Введите пароль" class="form-control" type="password"/>
<h:commandButton action="#{userCabinetController.updateUser}" value="Изменить данные" class="btn btn-lg btn-primary btn-block"/>
</h:form>
控制器:
@Named
@ViewScoped
public class UserCabinetController {
@PostConstruct
public void init() {
/**
* get user instance for further updating
*/
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> sessionMap = externalContext.getSessionMap();
User userFromSession = (User) sessionMap.get("user");
int userId = userFromSession.getId();
user = userFacade.finById(userId);
}
private User user;
@Inject
UserFacade userFacade;
public void updateUser() {
System.out.println(user);
userFacade.updateUser(user);
}
// getters/setters
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
和门面:
@ApplicationScoped
public class UserFacadeImpl implements UserFacade, Serializable{
@PersistenceContext
private EntityManager em;
@Transactional
public void updateUser(User user) {
em.merge(user);
}
}
但是当我尝试更新用户实例时,它会给出相同的实例,即旧实例而不提供新值。
如何更新用户实例?有什么问题?
答案 0 :(得分:0)
UserFacadeImpl.updateUser()
应该将更新的用户对象传回控制器:
@Named
@ViewScoped
public class UserCabinetController {
public void updateUser() {
System.out.println(user);
user = userFacade.updateUser(user);
}
}
@ApplicationScoped
public class UserFacadeImpl implements UserFacade, Serializable {
public User updateUser(User user) {
user = em.merge(user);
return user;
}
}