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>
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)?
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.
Related
I want to open a jsp page without accessing my servlete code. i.e. I neither have to input my url in (action="url") my jsp code nor have to access my Servlete code.
<form id="main" method="post" name="main" action="dpRegPost" onsubmit="return validate();">
Can anyone help me in this?
You can add javascript to your jsp file
<script type="text/javascript">
window.location.href = "www.google.com";
</script>
or using jsp
<%
response.sendRedirect("www.google.com");
%>
You can also try this
<jsp:forward page = "abc.jsp" />
Use jstl taglibrary in your current jsp page.Make available the taglibrary using below code
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Use Following code in jsp to redirect
<c:redirect url="/xxx.jsp"/>
Try this:
<form id="main" method="post" name="main" action="" onsubmit="redirect(this);">
<input type="submit" name="submit"/>
</form>
function redirect(elem){
elem.setAttribute("action","somepage.jsp");
elem.submit();
}
You can also call page directly with:
<jsp:redirect page="xyz.jsp" />
use the following code to redirect on another page in js...
$('#abc_id').click(function() {
updateSubContent("abc.jsp");
});
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSP tricks to make templating easier?
I'm new to JSPs and Servlets, I'm wondering is there a neat way to create a layout jsp and reuse it on similar jsp pages, something like asp.net master pages.
I googled it, some people say use templates
http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates
that uses jstl tag library. It says to put a tag like this:
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
but I get error (because jstl.jar and standard.jar are in WEB-INF/lib/ directory).
However some say jstl template have problems according to this
Struts OR Tiles OR ???...... JSP template solution
I would be glad to help me know the best way.
EDIT: What I need is to split page's layout into parts like content, header,... and set this parts in the page that uses the layout template, exactly like asp.net master page.
Put the following in WEB-INF/tags/genericpage.tag
<%#tag description="Overall Page template" pageEncoding="UTF-8"%>
<%#attribute name="header" fragment="true" %>
<%#attribute name="footer" fragment="true" %>
<html>
<body>
<div id="pageheader">
<jsp:invoke fragment="header"/>
</div>
<div id="body">
<jsp:doBody/>
</div>
<div id="pagefooter">
<jsp:invoke fragment="footer"/>
</div>
</body>
</html>
To use this:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:genericpage>
<jsp:attribute name="header">
<h1>Welcome</h1>
</jsp:attribute>
<jsp:attribute name="footer">
<p id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p>
</jsp:attribute>
<jsp:body>
<p>Hi I'm the heart of the message</p>
</jsp:body>
</t:genericpage>
That does exactly what you think it does!
This was part of a great answer by Will Hartung on this link.
i'm new in jsf, i would like to know how i can reuse others .jsf pages without 'copy paste' them.
In .jsp i do:
// head.jsp
<head> ... </head>
// top.jsp
<body> ... </body>
Then i include them in my new .jsp
// index.jsp
<%#include file="head.jsp" %>
<%#include file="top.jsp" %>
...
How can i do this with jsf ?
i'm trying this way:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<ui:include src="components/head.xhtml" />
</h:head>
<h:body>
<ui:include src="components/top.xhtml" />
</h:body>
</html>
But is not working..
Any idea ?
Best regards,
Valter Henrique.
Facelets is the default view technology for JSF2, so I would use its <ui:include> tag here. Make sure your paths are correct - they should start with webapp root, one containing WEB-INF - and also make sure the included facelets contain <ui:composition> tag around the included content. Anything outside this tag will be ignored.
Try looking for the "import" tag in the core library.
I think ui:include might be for facelets rather than plain jsf.
I am trying (and learning) to build a java web framework, and in the process of developing its' code generator based on the content of the database. In the view making process, I stumble in a difficulty, which I don't know how to solve it.
Firstly, I want all the pages to be created using the following index.jsp :
<body>
<%# include file="header.jsp" %>
<hr/>
<%# include file="body.jsp" %>
<hr/>
<%# include file="footer.jsp" %>
</body>
And, in the body.jsp, I want it to be like this :
<jsp:include page="${application_modul}" %>
Where application_modul is an attribute defined in its' controller this way :
request.setAttribute("application_modul","user_account\\view_user_account.jsp");
It can find the file correctly, but the processed jsp is not what I expected. Here :
<c:forEach items="[application.models.UserAccountModel#18a49e0, application.models.UserAccountModel#1f82982]" var="item" varStatus="status" >
<tr>
....
You can see the items attribute of jstl forEach, got its variable name (toString())...
Any Idea what the problem is????
I hope I describe my problem correctly
Many thanks!
PS :
I already create a quick fix for this, but not what I want it though. In the generated view_user_account.jsp, I do it like this :
<body>
<%# include file="header.jsp" %>
<hr/>
<c:forEach items="${row}" var="item" varStatus="status" >
<tr>
....
<hr/>
<%# include file="footer.jsp" %>
</body>
You can see that I create the whole file here...
EDITED:
PS : ${row} is an ArrayList populated with data from certain table
So, to summarize your problem in a single sentence, JSTL tags are not been parsed and they end up plain in generated HTML output?
You need to declare JSTL taglib in top of the JSP page where you're using JSTL tags to get them to run. For the JSTL core taglib, that'll be
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
I am not sure but, Try this...
index.jsp
<jsp:param name="parameterName" value="{parameterValue | <%= expression %>}" />
I am trying to figure out how to most effectively reuse JSP code.
I love the way Rails/erb works in that way ... with yield, layout, content_for
Example:
main_layout.erb.html
<html>
<head><%= yield :head %></head>
<body><%= yield %></body>
</html>
use
<% content_for :head do %>
<title>A simple page</title>
<% end %>
<p>Hello, Rails!</p>
in controller
layout "main_layout"
What is the closest I can get to this with JSP (without using extra frameworks)? I know about JSP include but that's not really the same as yield.
Any suggestions?
Thanks
I'm not familiar with what yield and content_for provide, but JSP tag files allow you a more robust way to template pages than JSP includes.
Example:
layout.tag
<%# tag body-content="scriptless" %>
<%# attribute name="pageTitle" required="true" type="java.lang.String" %>
<html>
<head>
<title>${pageTitle}</title>
</head>
<body>
<jsp:doBody/>
</body>
</html>
An individual JSP
<%# taglib prefix="z" tagdir="/WEB-INF/tags" %>
<z:layout pageTitle="A simple page">
<p>Hello, JSP!</p>
</z:layout>
Just place your layout.tag in the /WEB-INF/tags directory. You can use any available prefix you want, I just used "z" for the example.
While you mentioned wanting no frameworks on top of stock jsp, the Layout functionality of the Stripes Framework does pretty much exactly what you're asking for.