简体   繁体   English

如何使用struts标签在jsps中创建对象?

[英]How to create objects in jsps using struts tags?

I am developing a struts based application on google app engine. 我在Google App Engine上开发基于Struts的应用程序。 I am using google's user service to allow users to log into my application. 我正在使用Google的用户服务,以允许用户登录我的应用程序。

When the user is signed into his google account and opens my application, my application must not ask to sign in again. 当用户登录到他的Google帐户并打开我的应用程序时,我的应用程序不得要求再次登录。 Suppose the homepage of my application is index.jsp, it should say 假设我的应用程序的主页是index.jsp,应该说

Welcome user xyz@gmail.com 欢迎用户xyz@gmail.com

To do so, I have to check whether there is a current user or not like this 为此,我必须检查是否有当前用户

<%
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if(user == null)
    //login link
else
    //Welcome message
%>

But I am not supposed to use scriptlets in my application. 但是我不应该在应用程序中使用scriptlet。 I have to achieve this using struts tags. 我必须使用struts标签来实现。

Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thank you all in advance and good day. 谢谢大家提前和美好的一天。

First of all, you need to register the user logged in the session. 首先,您需要注册登录会话的用户。 You must create an action where you will put the code that does the checking account with Google: 您必须创建一个操作,然后在该操作中放置执行Google支票帐户的代码:

public LoginAction {

    @Action(...)
    public String login () {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        ActionContext.getContext().getSession().put("user", user);
        return "success";
    }
}

Then you will have the user available on any jsp. 然后,您将可以在任何jsp上使用该用户。 The jsp code would be something like: 该jsp代码将类似于:

<s:if test="#session.user == null">
    //login link
</s:if>
<s:else>
    //Welcome message
</s:else>

When the user logs off, you just have to remove the session object. 当用户注销时,您只需要删除会话对象。

And if you need to log off the user if he does in his Google account, you will need an interceptor that will check if it is still logged all the time. 如果您需要注销该用户的Google帐户,则需要一个拦截器来检查该用户是否一直都在登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM