通过改换维护服务角 [英] Maintain Angular Service through page change

查看:155
本文介绍了通过改换维护服务角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用在后端的前端和Java Servlets角度建立一个门户网站。的页面开始以登录页面,这需要输入,并将其发送给servlet。服务器验证,并与具有用户名和他的权限的JSON对象响应。我设置的值向其中注入控制器服务。然后,我用 $ windows.location 到网页更改到主页这是仪表板。现在,我想在主页的控制器使用这项服务。但我不能够保持在 $范围由于页面的变化。

I am creating a web portal using Angular on the front end and Java Servlets in the back end. The page starts with a login page, It takes the input and sends it to the servlet. The server validates and responds with a JSON object which has the username and his permissions. I set the values to a Service which is injected into the Controller. Then I use $windows.location to change the web page to the home page which is the dashboard. Now I want to use this Service in the controller of the homepage. But I am not able to maintain the $scope due to the page change.

所以我想用的Response.Redirect重定向到主页在后端的()。但我不知道如何重定向后得到网页的用户详细信息。就像我怎么通过用户对象的 Home.java 的servlet

So I thought of redirecting to the home page in the backend using response.redirect() . But I don't know how to get the user details in the homepage after redirection. Like how do I pass the User object to the Home.java servlet

这是我的LoginCtrl.js

This is my LoginCtrl.js

if (isValid) {
            $http({
                method : 'POST',
                url : 'login',
                data : JSON.stringify($scope.user),
                headers : {
                    'Content-Type' : 'application/json'
                }
            }).success(function(data) {
                if (!("failure" == data)) {
                    console.log(data);
                    var user = {};
                    user.name = data.name;
                    user.permissions = data.permissions;
                    MyService.setUser(user); // set the values for user

                    $window.location.href = 'main.jsp';
                } else {
                    $scope.information = "Invalid username/password!"
                }
            }).error(function(data) {
                console.log(data);
            });

这是我的Login.java的servlet

This is my Login.java servlet

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonObject obj = (JsonObject) parser.parse(request.getReader());

        String username = "";
        String password = "";

        if (obj.get("name") != null & obj.get("password") != null) {
            username = obj.get("name").getAsString();
            password = obj.get("password").getAsString();
        }

        System.out.println("Username :" + username);
        System.out.println("Password :" + password);

        // Passing username and password to Context and validate.
        // If authentication successful, set user object with details and return
        // it
        // User user = (User) Context.Authorized(username,password)

        // for testing
        User user = new User();
        user.setName(username);
        user.setPermission("crwd");
        user.setAuthorized(true);

        response.setContentType("text/html");
        if (user.getAthorized()) {
            String responseJSON = gson.toJson(user);
            response.getWriter().write(responseJSON);
        } else {
            response.getWriter().write("failure");
        }
    }

请告诉我,如果我的要求可在角来实现,或者也可以使用Java来完成,那么如何?

Please tell me if my requirement can be achieved in Angular or if it can be done using Java, then how ?

推荐答案

如果您重定向到另一个页面,就会有完全不同的角度的应用程序,你将失去服务状态。

If you redirect to another page, there will be completely different Angular application, and you will lose service state.


  • 您可以让单页应用程序,并使用$ routeProvider和NG-视图为登录页面和主页导航栏。

  • You can make single page application and use $routeProvider and ng-view for both login page and homepage navbar.

或者你的应用程序可以由不同的页面,但在每一个页面中,您必须调用服务器获取用户信息

Or your app can consist of different pages, but then in every page you must call server to get user info

这篇关于通过改换维护服务角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆