0

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>
Mango
  • 650
  • 2
  • 16
  • 38

2 Answers2

0

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.

Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62
0

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)?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I think this would be problematic. Dynamic include with JSF. – Kevin Galligan Nov 06 '11 at 18:56
  • @Kevin: It works, in contrary to your answer. You seem to have almost no practical experience with JSF+JSP. – BalusC Nov 06 '11 at 18:59
  • Thanks! Much appreciated. Like I said, I have experience using facelets. A whole lot of it. My concern here is that JSP and JSF don't mix well in certain cases. What probably would not work well here is if you change the value of 'menuAssigned' while you're on that page, because the view model expects a certain hierarchy. Its the same reason you don't mix 'c:if' tags, but I'd guess you don't have a problem with that either? – Kevin Galligan Nov 06 '11 at 19:04
  • @Kevin: You said it nowhere. As to the possible mix issues, it will only not work when the EL variable is *only* present in the scope of a JSF component. But this isn't the case in this particular case. See also among others this for a layman's explanation of how JSF components and JSP based tags mix: http://stackoverflow.com/questions/3442380/jstl-cif-inside-a-jsf-hdatatable – BalusC Nov 06 '11 at 19:07
  • I said it in my answer. Sorry to be unclear. I'm not worried about 'menuAssigned' being available as much as the view backing the JSF changing due to including different files dynamically. You don't seem to be concerned with that, and as mentioned, I'm a facelets guy, so I'm sure you're right. – Kevin Galligan Nov 06 '11 at 19:34
  • How abt this ? – r0ast3d Nov 14 '11 at 20:21
  • @r0ast3d: I don't see how that's different from my answer. – BalusC Nov 14 '11 at 20:23
  • just that it helps to clarify the OP for the possibilities. Not much different. – r0ast3d Nov 14 '11 at 20:28