我正在使用JHispter 3.12.2开发一个应用程序.
由于我需要比JHipster提供的更多信息,因此我创建了一个包含两个字符串的实体UserExtra:电话号码和Skype地址.我在One to One关系中将此实体链接到JHI_User.
现在我面临的问题是,当用户注册时,我想创建一个与注册用户关联的新UserExtra.
为此,我在客户端尝试了几件事.
标准JHipster寄存器页面的工作方式是使用名为vm.registerAccount的变量,该变量包含firstName,lastName,login,password等.
我尝试使用另一个变量vm.userExtra来保存我的电话号码和Skype.然后,我尝试了几件事:
在register.controller.js中,我将userExtra传递给createAccount函数:
Auth.createAccount(vm.registerAccount, vm.userExtra).then(function () {
vm.success = 'OK';
}).catch(function (response) {
vm.success = null;
if (response.status === 400 && response.data === 'login already in use') {
vm.errorUserExists = 'ERROR';
} else if (response.status === 400 && response.data === 'e-mail address already in use') {
vm.errorEmailExists = 'ERROR';
} else {
vm.error = 'ERROR';
}
});我修改了auth.service.js的createAccount函数来跟踪所做的更改:
function createAccount (account, userExtra, callback) {
var cb = callback || angular.noop;
return Register.save(account, userExtra,
function () {
return cb(account);
},
function (err) {
this.logout();
return cb(err);
}.bind(this)).$promise;
}</pre>
Run Code Online (Sandbox Code Playgroud)最后,我在服务器端更新了AccountResource.java的registerAccount函数:
public ResponseEntity registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM, UserExtra userExtra) { ... }但是registerAccount函数甚至没有从我记忆中执行过.
我也尝试在register.controller.js的createAccount函数的回调中添加新用户,如下所示:
Auth.createAccount(vm.registerAccount).then(function (result) {
vm.userExtra.user = result;
UserExtra.save(vm.userExtra, function() { // success }, function() { // fail });
vm.success = 'OK';
})...
Run Code Online (Sandbox Code Playgroud)
但是我在尝试保存新的UserExtra时只收到错误.
我很确定我需要修改AccountResource.java中的registerAccount()函数,但我无法检索我尝试从客户端发送的其他信息.我设法在此函数中创建一个新用户并将其链接到JHI_User,但没有附加信息.
很可能有人已经遇到过这个问题,这可能是解决这个问题的最佳方案吗?
用解决方案编辑:
感谢GaëlMarziou,我解决了我的问题.
在客户端,在我已经重写为我使用的register.html页面中,我有两个绑定到vm.registerAccount属性的字段:
<input class="form-control" id="phone" ng-model="vm.registerAccount.phone" placeholder="{{'global.form.phone.placeholder' | translate}}" />
...
<input class="form-control" id="skype" ng-model="vm.registerAccount.skype" placeholder="{{'global.form.skype.placeholder' | translate}}" />
Run Code Online (Sandbox Code Playgroud)
在ManagedUserVM中,我真的只添加了我的两个字段和它们的getter:
private String phone;
private String skype;
public String getPhone() {
return phone;
}
public String getSkype() {
return skype;
}
Run Code Online (Sandbox Code Playgroud)
我更改了我的UserExtra类以映射User和UserExtra ID,以便它们被镜像.这加快了检索过程并使其更有意义,因为UserExtra实际上只是用户的扩展:
public class UserExtra implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@Column(name = "phone")
private String phone;
@Column(name = "skype")
private String skype;
@OneToOne
@MapsId
private User user;
...
}
Run Code Online (Sandbox Code Playgroud)
我在UserService中创建了一个名为createUser()的新自定义函数,除了基本函数之外,还需要我的两个字段.我没有更新现有的函数,所以我不必更改测试类:
public User createUser(String login, String password, String firstName, String lastName, String email,
String langKey, String phone, String skype) {
User newUser = new User();
Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER);
Set<Authority> authorities = new HashSet<>();
String encryptedPassword = passwordEncoder.encode(password);
newUser.setLogin(login);
// new user gets initially a generated password
newUser.setPassword(encryptedPassword);
newUser.setFirstName(firstName);
newUser.setLastName(lastName);
newUser.setEmail(email);
newUser.setLangKey(langKey);
// new user is not active
newUser.setActivated(false);
// new user gets registration key
newUser.setActivationKey(RandomUtil.generateActivationKey());
authorities.add(authority);
newUser.setAuthorities(authorities);
userRepository.save(newUser);
userSearchRepository.save(newUser);
log.debug("Created Information for User: {}", newUser);
// Create and save the UserExtra entity
UserExtra newUserExtra = new UserExtra();
newUserExtra.setUser(newUser);
newUserExtra.setPhone(phone);
newUserExtra.setSkype(skype);
userExtraRepository.save(newUserExtra);
userExtraSearchRepository.save(newUserExtra);
log.debug("Created Information for UserExtra: {}", newUserExtra);
return newUser;
}
Run Code Online (Sandbox Code Playgroud)
最后,我更新了AccountResource的registerAccount()函数,使用另外两个字段调用我的自定义函数:
public ResponseEntity<?> registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
HttpHeaders textPlainHeaders = new HttpHeaders();
textPlainHeaders.setContentType(MediaType.TEXT_PLAIN);
return userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase())
.map(user -> new ResponseEntity<>("login already in use", textPlainHeaders, HttpStatus.BAD_REQUEST))
.orElseGet(() -> userRepository.findOneByEmail(managedUserVM.getEmail())
.map(user -> new ResponseEntity<>("e-mail address already in use", textPlainHeaders, HttpStatus.BAD_REQUEST))
.orElseGet(() -> {
User user = userService
.createUser(managedUserVM.getLogin(), managedUserVM.getPassword(),
managedUserVM.getFirstName(), managedUserVM.getLastName(),
managedUserVM.getEmail().toLowerCase(), managedUserVM.getLangKey(),
managedUserVM.getPhone(), managedUserVM.getSkype());
mailService.sendActivationEmail(user);
return new ResponseEntity<>(HttpStatus.CREATED);
})
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1549 次 |
| 最近记录: |