简体   繁体   中英

How to create objects in jsps using struts tags?

I am developing a struts based application on google app engine. I am using google's user service to allow users to log into my application.

When the user is signed into his google account and opens my application, my application must not ask to sign in again. Suppose the homepage of my application is index.jsp, it should say

Welcome user 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. I have to achieve this using struts tags.

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:

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. The jsp code would be something like:

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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