k_rollo
k_rollo

Reputation: 5472

Struts 2 If-Else-If

I have set an attribute in session in the UserAction class that will be retrieved in a JSP. The retrieved attribute from the session will control how the page will be displayed. The intention is to make certain portions of the JSP turn on/off depending on the type of user (0 = guest, 1 = admin, 2 = user) after logging in.

Action Class:

public class UserAction extends ActionSupport implements SessionAware {
    private static final long serialVersionUID = 1L;

    private Map<String, Object> session;

    private String userId;
    private String userPassword;
    private String userEmail;
    private int userType;
    private Date registeredDate;

    @Override
    public String execute() {
        UserManager um = new UserManager();
        String registeredPassword = um.getCurrentUserDetail("user_password",
                getUserId());

        if (getUserPassword().equals(registeredPassword)) {
            String currentUserId = um.getCurrentUserDetail("user_id", userId);
            int currentUserType = um.getCurrentUserType(userId);

            session.put("currentUserId", (String) currentUserId);
            session.put("currentUserType", (Integer) currentUserType);

            System.out.println("You have successfully logged in!");
            return SUCCESS;
        }

        System.out.println("Your login has failed!");
        return ERROR;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    // getters and setters
}

index.jsp:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="com.mypackage.model.UserAction"%>

<html>
<head>
<title>My Site - Home</title>

<%@ include file="css/style.css"%>
<%@ include file="css/style2.css"%>

<s:set var="type" value="0" />
<s:if test='#session.containsKey("currentUserType")'>
    <s:set var="type" value='#session["currentUserType"]' />
</s:if>
</head>

<body>
    <div id="wrapper">
        <div id="inner">
            <div id="header">
                <s:if test="type==0">
                    <%@ include file="templates/guest-header.jsp"%>
                </s:if>

                <s:else>
                    <%@ include file="templates/logged-header.jsp"%>
                </s:else>
            </div>

            <dl id="browse">
                <s:if test="type==1 || type==2">
                    <%@ include file="templates/logged-acct.jsp"%>
                </s:if>

                <dt>NAVIGATE WEBSITE</dt>
                <s:if test="type==0">
                    <%@ include file="templates/guest-nav.jsp"%>
                </s:if>

                <s:elseif test="type==1">
                    <%@ include file="templates/admin-nav.jsp"%>
                </s:elseif>

                <s:elseif test="type==2">
                    <%@ include file="templates/user-nav.jsp"%>
                </s:elseif>

                <dt>SEARCH MOVIE</dt>
                <dd class="searchform">
                    <%@ include file="templates/search-box.jsp"%>
                </dd>
            </dl>

            <div id="body">
                <div class="inner">
                    <%@ include file="templates/content.jsp"%>
                </div>
                <!-- end .inner -->
            </div>
            <!-- end body -->
            <div class="clear"></div>
            <%@ include file="templates/copyright.jsp"%>
        </div>
        <!-- end inner -->
    </div>
    <!-- end wrapper -->
    <%@ include file="templates/footer.jsp"%>
</body>
</html>

However, it does not seem to enter any of the if-conditions. At the very least, I believe the guest view should be displaying, since type has been initially set to 0 by <s:set var="type" value="0" />.

(Kindly forgive the design for now, I have been made aware there is an elegant approach with this using <s:include> instead of <%@ include %>, but I am only beginning Struts 2. I often make improvements/optimizations after I make it work.)

Upvotes: 2

Views: 8744

Answers (1)

Ken de Guzman
Ken de Guzman

Reputation: 2810

You've forgot the pound sign # in your testing.
It should be like this.

  <s:if test="#type==0">
        <%@ include file="templates/guest-header.jsp"%>
    </s:if>

    <s:else>
        <%@ include file="templates/logged-header.jsp"%>
    </s:else>

Reference on how to access a variable in struts

Upvotes: 2

Related Questions