简体   繁体   中英

include a jsp as a header based on a condition inJSF

I'm using JSF-2.0 and I'm trying to include a jsp as a header for my current jsp.But all i want is the included jsp should be altered based on the login credentials. More clearly...depending on the person logging in to my application, the header menu (included jsp) should be different.I've tried implementing in the below way but it did not work..any help would be appreciated

<html>
 <head></head>
 <body>
  <%
  String menuHeader = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("menuAssigned");
  if (menuHeader.equals("XX")){ %>
  <f:view> <jsp:include page="XHeader.jsp" /> </f:view>
  <% }else if(menuHeader.equals("YY")){ %>
 <f:view>  <jsp:include page="YHeader.jsp" />
  <%}%>
   ---
  </f:view>
 </body>
</html>

Short answer, use EITHER JSP or JSF flow control. Don't mix them too much.

<html>
 <head></head>
 <body>
  <f:view>
    <h:panelGroup rendered="#{menuHeader == 'XX'}">
          <%@include file=”XHeader.jsp" %>
    </h:panelGroup>
    <h:panelGroup rendered="#{menuHeader == 'YY'}">
          <%@include file=”YHeader.jsp" %> 
    </h:panelGroup>
  </f:view>
 </body>
</html>

Perhaps static includes? Again, I've been using facelets with JSF for several years now. Not used to the JSP stuff anymore. Its been a while.

Don't use Scriptlets . Ever.

Your menuAssigned variable is just available in EL by #{menuAssigned} . I suggest to align your menuAssigned variable value with the JSP include filename. Then you can just use

<jsp:include page="#{menuAssigned}Header.jsp" />

Imagine that menuAssigned is XX , then this will include XXHeader.jsp .


Unrelated to the concrete problem, why are you using legacy JSPs while you're apparently already on JSF 2.0 which comes along with JSP's awesome successor Facelets (XHTML)?

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