gpt4 book ai didi

java - 使用 angularjs 和 spring boot 将数据存储到数据库中的问题

转载 作者:行者123 更新时间:2023-12-01 11:08:13 24 4
gpt4 key购买 nike

这就是我所拥有的:

用户.java

@Entity
public class User {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

@Column
private String username;

@Column
private String password;

@Column
private String lastname;

public Integer getId() {
return id;
}

public String getLastname() {
return lastname;
}

public String getPassword() {
return password;
}

public String getUsername() {
return username;
}
}

UserController.java

@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserRepository repo;

@RequestMapping(method = RequestMethod.POST)
public User addUser(@RequestBody User user) {
return repo.saveAndFlush(user);
}

用户.service.js

注意:这里控制台输出了用户名、密码和姓氏这3个值。

    function Create(user) {

console.log(user.username)
console.log(user.password)
console.log(user.lastName)


return $http.post('/user', user).then(handleSuccess, handleError('Error creating user'));
}

注册.controller

(function () {
'use strict';

angular
.module('app')
.controller('RegisterController', RegisterController);

RegisterController.$inject = ['UserService', '$location', '$rootScope', 'FlashService'];
function RegisterController(UserService, $location, $rootScope, FlashService) {
var vm = this;

vm.register = register;

function register() {
vm.dataLoading = true;
UserService.Create(vm.user)
.then(function (response) {
if (response.success) {
FlashService.Success('Registration successful', true);
$location.path('/login');

} else {
FlashService.Error(response.message);
vm.dataLoading = false;
}
});
}
}

})();

注册.html

<div class="container" ng-controller="RegisterController as vm">
<div class="col-md-6 col-md-offset-3">
<h2>Register</h2>
<div ng-show="vm.error" class="alert alert-danger">{{vm.error}}</div>
<form name="form" ng-submit="vm.register()" role="form">


<div class="form-group" ng-class="{ 'has-error': form.lastName.$dirty && form.lastName.$error.required }">
<label for="lastName">Last name</label>
<input type="text" name="lastName" id="lastName" class="form-control" ng-model="vm.user.lastName" required />
<span ng-show="form.lastName.$dirty && form.lastName.$error.required" class="help-block">Last name is required</span>
</div>

<div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" ng-model="vm.user.username" required />
<span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
</div>


<div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" ng-model="vm.user.password" required />
<span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
</div>
<div class="form-actions">
<button type="submit" ng-disabled="form.$invalid || vm.dataLoading" class="btn btn-primary">Register</button>
<img ng-if="vm.dataLoading" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" />
<a href="#/login" class="btn btn-link">Cancel</a>
</div>
</form>
</div>
</div>

但由于某种原因,这是用户的结果。

{id: 1, username: "33", password: "33", lastname: null}
id: 1
lastname: null
password: "33"
username: "33"

无论我做什么,它总是显示为空。我真的不知道我错过了什么

有人知道这件事吗?我想我一直在忽略一些事情。

最佳答案

您的 View 和服务将姓氏的数据指定为lastName,您的模型为小写。

关于java - 使用 angularjs 和 spring boot 将数据存储到数据库中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32693165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com