不要将html 与js模型和java模型链接起来

时间:2022-04-08 20:25:12

I have a login page with some code.

我有一个包含一些代码的登录页面。

<div ng-show="!loggedIn()">
        <form ng-submit="login()">
            Username:
            <input type="text" ng-model="userName"/>
            Password:
            <input type="password" ng-model="userPassword"/><span><input type="submit" value="Login"/>
        </form>
    </div>

Login links with userName model in js file and userPassword - not.

使用js文件和userPassword中的userName模型登录链接 - 不是。

$scope.login = function() {
                $scope.error = null;
                mainService.login($scope.userName, $scope.userPassword).then(function(token) {
                    $scope.token = token;
                    $http.defaults.headers.common.Authorization = 'Bearer ' + token;
                    $scope.checkRoles();
                },
                function(error){
                    $scope.error = error;
                    $scope.userName = '';
                    $scope.userPassword = '';
                });
            };

...

 appModule.service('mainService', function($http) {
        return {
            login : function(username, userpassword) {
                return $http.post('/user/login', {name: username}, {password: userpassword}).then(function(response) {
                    return response.data.token;
                });
            },

I have some controller with inner class

我有一些内部控制器

@SuppressWarnings("unused")
    private static class UserLogin {
        public String name;
        public String password;
    }

and method login()

和方法登录()

 @RequestMapping(value = "login", method = RequestMethod.POST)
public LoginResponse login(@RequestBody final UserLogin login)
    throws ServletException {
    System.out.println(login.name + login.password);//

Here login.name = what I wrote in web page (Login) and login.password always = null. What I'm doing wrong?

这里login.name =我在网页(Login)和login.password中写的内容总是= null。我做错了什么?

2 个解决方案

#1


-1  

The login function has to change like this:

登录功能必须改变如下:

login : function(username, userpassword) { return $http.post('/user/login', {name: username, password: userpassword}).then(function(response) { return response.data.token; });

#2


0  

The reason it is not working is because your POST call is passing the password as the configuration parameter in $http.post. Your password should be part of the data parameter object: $http.post('/user/login', {name: username, password: userpassword})

它无法工作的原因是因为您的POST调用将密码作为$ http.post中的配置参数传递。您的密码应该是数据参数对象的一部分:$ http.post('/ user / login',{name:username,password:userpassword})

#1


-1  

The login function has to change like this:

登录功能必须改变如下:

login : function(username, userpassword) { return $http.post('/user/login', {name: username, password: userpassword}).then(function(response) { return response.data.token; });

#2


0  

The reason it is not working is because your POST call is passing the password as the configuration parameter in $http.post. Your password should be part of the data parameter object: $http.post('/user/login', {name: username, password: userpassword})

它无法工作的原因是因为您的POST调用将密码作为$ http.post中的配置参数传递。您的密码应该是数据参数对象的一部分:$ http.post('/ user / login',{name:username,password:userpassword})