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.
Related
i am using ready-made template(with css and j-queries) in my java ee app. all the primefaces components are rendered properly except the panelgrid control of primefaces 3.2.
it is displayed with border. i want it without border.
i have removed all the table styling from the css of custom ready-made template.
still the border is there.
when i remove the readymade template, the panelgrid is rendered perfectly without any border. how do i remove the border and what is the cause of this problem?
edited:
xhtml file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AP administration panel - A massive administration panel</title>
</h:head>
<h:body>
<div>
<h:form>
<p:panelGrid columns="2" style="border: none">
<h:outputText value="scrip symbol"/>
<p:inputText value=""/>
<p:commandButton value="submit"/>
</p:panelGrid>
</h:form>
</div>
</h:body>
</html>
When overriding PrimeFaces default styles, you have to specify a CSS selector of at least the same strength or to specify a stronger selector. The strength of a CSS selector (the cascading rules) is specified in the W3 CSS specification and clearly explained in this article: Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade.
Based on PrimeFaces own CSS, the following selectors should do:
.ui-panelgrid tr, .ui-panelgrid td {
border: none;
}
Just put them in a .css file which you include by <h:outputStylesheet> inside the beginning of the <h:body> so that it will be included after PrimeFaces own style.
<h:body>
<h:outputStylesheet name="layout.css" />
...
</h:body>
See also:
How to remove border from specific PrimeFaces p:panelGrid?
How do I override default PrimeFaces CSS with custom styles?
Update: As per your update, your CSS doesn't seem to be loaded at all. You should have noticed this by verifying the HTTP traffic in browser builtin webdeveloper toolset (press F12 in Chrome/IE9/Firebug) and seeing that it returned a HTTP 404 error. When using <h:outputStylesheet> you need to put the CSS file in the /resources folder of the webcontent. So you must have a /resources/css/mycss.css in order to be able to use <h:outputStylesheet name="css/mycss.css" />.
See also:
How to reference CSS / JS / image resource in Facelets template?
I'm quite new to using JSF and I'm not sure if that's the right way to go, but in Rails you usually have a main application file into which the current page is loaded. That way I don't have to worry about copy-pasting the menu, etc. every time.
How can I achieve that with JSF 2? Can I navigate to the same main page every time and tell it to load a current content? Or do I tell the current page that I navigate to to load the "main frame around the content"?
Thanks!
Yes of course, JSF 2.0 has page-templating feature. You define a template that defines a generic layout to all the view pages.
Facelets tags to create basic page:
ui:insert – defines content that is going to replace by the file that load the template;
ui:define – defines content that is inserted into tag ui:insert;
ui:include – includes content from another page;
ui:composition – the specified template is loaded, if used with template attribute, and the children of this tag defines the template layout. In other case, it’s a group of elements, that can be inserted somewhere.
For example:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/templates/myLayout.xhtml">
<ui:define name="menu">
<ui:include src="/mypath/menu.xhtml"/>
</ui:define>
<ui:define name="content">
<ui:include src="/mypath/content.xhtml"/>
</ui:define>
</ui:composition>
or
<ui:insert name="content">
<ui:include src="/mypath/mycontent.xhtml"/>
</ui:insert>
JSF doesn't support what you want to archive. Instead, it support the views and basic layout(template). What you need it this:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
template="path/to/template.xhtml>
<your custom content here/>
<ui:composition/>
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.
I have recently started working with JSF2.0 and Facelets, but have run into what I hope is an easy answer for most of you out there. When I am trying to add any HTML tag within a <ui:define> tag I receive the following error:
javax.faces.view.facelets.TagException: /content/home/test.xhtml #11,10 Tag Library supports namespace: http://java.sun.com/jsf/facelets, but no tag was defined for name: div
If I remove all of the HTML tags from the section the page displays correctly. Here is my simple page that I have been trying to get working:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:jrc="http://com.comanche.web.components">
<ui:composition template="/templates/masterLayout.xhtml" xmlns="http://java.sun.com/jsf/facelets">
<ui:define name="windowTitle">Home</ui:define>
<ui:define name="content">
<div>I want to add HTML and am having lots of trouble.</div>
</ui:define>
</ui:composition>
</html>
I know I should be able to add HTML within the define tag. What do I need to do to get HTML in without any errors.
Your <ui:composition> declaration is using the wrong global XML namespace. You definied http://java.sun.com/jsf/facelets as global XML namespace while it should have been assigned to ui: XML namespace. The <div> tag doesn't exist in the Facelets taglib (which is what the exception is trying to tell you). You should have assigned http://www.w3.org/1999/xhtml as global XML namespace. Further, the <!DOCTYPE> and <html> will be ignored anyway. The sole content of the file should be the following:
<ui:composition template="/templates/masterLayout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:define name="windowTitle">Home</ui:define>
<ui:define name="content">
<div>I want to add HTML and am having lots of trouble.</div>
</ui:define>
</ui:composition>
Nothing before or after <ui:composition> in the very same file is necessary.
See also:
How to include another XHTML in XHTML using JSF 2.0 Facelets?
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.