java - Struts 2 If-Else-If -
i have set attribute in session in useraction
class retrieved in jsp. retrieved attribute session command how page displayed. intention create portions of jsp turn on/off depending on 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 logged in!"); homecoming success; } system.out.println("your login has failed!"); homecoming error; } @override public void setsession(map<string, object> session) { this.session = session; } // getters , 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, not seem come in of if-conditions. @ least, believe guest
view should displaying, since type
has been set 0 <s:set var="type" value="0" />
.
(kindly forgive design now, have been made aware there elegant approach using <s:include>
instead of <%@ include %>
, origin struts 2. create improvements/optimizations after create work.)
you've forgot pound sign # in testing. should 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 access variable in struts
java jsp session if-statement struts2
No comments:
Post a Comment