Struts 2 If-Else-If

时间:2014-11-14 03:00:31

标签: java jsp session if-statement struts2

我在UserAction类中的会话中设置了一个属性,该属性将在JSP中检索。会话中检索到的属性将控制页面的显示方式。目的是在登录后根据用户的type(0 = guest,1 = admin,2 = user)打开/关闭JSP的某些部分。

行动类:

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>

但是,它似乎没有输入任何if条件。至少,我认为guest视图应该显示,因为type已经最初由<s:set var="type" value="0" />设置为0。

(现在请原谅我的设计,我已经意识到使用<s:include>代替<%@ include %>有一种优雅的方法,但我只是开始使用Struts 2.我经常做出改进/在我开始工作之后进行优化。)

1 个答案:

答案 0 :(得分:1)

你在测试中忘记了#符号。
它应该是这样的。

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

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

Reference关于如何访问struts中的变量