2

所以,这是我的看法

<div>
    <div class="loginDiv">
        <form name="loginForm" ng-submit="validateDetails()" novalidate>
            <div class="userNameDiv">
                <label class="label" for="username">User Name:</label>
                <input type="text" id="username" name="userName" class="input-medium" ng-model="employee.userName" required ng-minlength=5 ng-maximumlength=20/>
            </div>
            <div class="passwordDiv">
                <label class="label" for="password">Password:</label>
                <input type="password" id="password" name="password" class="input-medium" ng-model="employee.password" required />
            </div>

            <button type="submit" class="btn-large">SUBMIT</button>
        </form>
    </div>
</div>

并且视图的控制器是

angular.module('employeeApp.controllers')
    .controller('LoginPageController',['$scope','authenticationDataModelService',function($scope,authenticationDataModelService)
    {

        $scope.validateDetails=function(){
            employee={};
            employee.userName=$scope.employee.userName;
            employee.password=$scope.employee.password;
            console.log(employee);
            authenticationDataModelService.validateData(employee);
        }
    }
    ]);

我将此员工对象发送到服务文件中,我将在发布请求中将其发送到该文件中(不包括整个服务代码):

$http.post(this.employeeDataUrl,employee);

我将它发送到服务器,servlet代码是

public class LoginService extends HttpServlet{

    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        PrintWriter printWriter=response.getWriter();
        response.setContentType("text/html");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        EmployeeDAO employee = new EmployeeDAO();
        System.out.println(userName+""+password);
        boolean validateLogin=employee.validateLogin(userName, password);
        if(validateLogin){
            printWriter.println("Login Success");
        }
        else{
            printWriter.println("Login Failed");
        }

    }
}

但我在这里得到空值。我无法将员工对象中的值从 angularjs 中的服务文件发送到 servlet。我怎么能做到这一点?如果问题很愚蠢,请原谅我......

4

1 回答 1

4

AngularJS 以 JSON 格式发送您的“员工”对象,因此您无法在服务器端使用您的代码读取此对象。您应该首先解析 JSON 对象。

于 2013-11-07T07:53:12.230 回答