Render JSF h:message with p element instead of span - jsf

I would like to create a custom message renderer to renders h:message as a 'p' html element instead of as a 'span' element. It concerns the following message tag:
<h:message id="firstNameErrorMsg" for="firstname" class="error-msg" />
I've written to code underneath, but that's only rendering an empty 'p' element. I suppose I have to copy all attributes and text from the original component and write it to the writer. However, I don't know where to find everything and it seems to be a lot of work for just a replacement of a tag.
Is there a better way to get an h:message tag rendered as a 'p' element?
Code:
#FacesRenderer(componentFamily = "javax.faces.Message", rendererType = "javax.faces.Message")
public class FoutmeldingRenderer extends Renderer {
#Override
public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("p", component);
writer.endElement("p");
}
}

It isn't exactly "a lot of work". It's basically a matter of extending from the standard JSF messages renderer, copypasting its encodeEnd() method consisting about 200 lines then editing only 2 lines to replace "span" by "p". It's doable in less than a minute.
But yes, I agree that this is a plain ugly approach.
You can consider the following alternatives which are not necessarily more easy, but at least more clean:
First of all, what's the semantic value of using a <p> instead of a <span> in this specific case? To be honest, I'm not seeing any semantic value for this. So, I suggest to just keep it a <span>. If the sole functional requirement is to let it appear like a <p>, then just throw in some CSS. E.g.
.error-msg {
display: block;
margin: 1em 0;
}
You can obtain all messages for a particular client ID directly in EL as follows, assuming that the parent form has the ID formId:
#{facesContext.getMessageList('formId:firstName')}
So, to print the summary and detail of the first message, just do:
<c:set var="message" value="#{facesContext.getMessageList('formId:firstName')[0]}" />
<p title="#{message.detail}">#{message.summary}</p>
You can always hide it away into a custom tag file like so:
<my:message id="firstNameErrorMsg" for="firstname" class="error-msg" />
Use OmniFaces <o:messages>. When the var attribute is specified, then you can use it like an <ui:repeat>.
<o:messages for="firstNameErrorMsg" var="message">
<p title="#{message.detail}">#{message.summary}</p>
</o:messages>

Related

How to use b:growl

I'm trying to show a specific b:growl element loaded from a bean, basically I'm try to replicate the example from the BootsFaces showcase (https://showcase.bootsfaces.net/forms/Growl.jsf;jsessionid=QdtXGdUxPK9sS714iLGuyksK93AMfZM-WfZm3py_.showcase01).
I used FacesMessages.info, but I get two b:growl messages. So, how can I target the specific b:growl element to show my message?
In the example from showcase: What does the messagesBean.specificInfo method do?
Edit:
Thank you Stephan, but is not working for me, I still get two b:grolw messages and the one width globalOnly="true" is ignored and a standar one is showed instead.
Here is my code:
xhtml:
<ui:define name="content">
<div class="container" style="width: auto">
<h:form id="idForm">
<b:panel title="#{msg['administrarServicio']}" look="primary"
id="panel" collapsible="false">
<b:commandButton id="idBorrar" col-lg="3" col-md="3" colSm="10"
col-xs="10" offset-lg="2" offset-md="2" offset-sm="1"
offset-xs="1" value="Borrare o o o o" look="danger"
iconAwesome="trash" iconAlign="right"
action="#{serviceManagementBean.borrar}" />
</b:panel>
<b:growl id="growlCommonMsg" placementFrom="bottom"
show-detail="true" show-summary="true" allowDismiss="true"
global-only="true" delay="10000" escape="true" />
<b:growl for="idBorrar" id="growlMsg" globalOnly="true"
placementFrom="bottom" show-detail="true" show-summary="true"
allowDismiss="true" delay="10000" escape="true" global-only="false"
animation-enter="animated bounceInDown"
animation-exit="animated flipOutX" />
</h:form>
</div>
</ui:define>
Java:
public void borrar() {
System.out.println("BORRAR " + this.idTramite);
FacesMessages.info("idForm:idBorrar", "Se boró correctamente el servicio " + this.idTramite, "Nunca va a volver. ¡Nunca!");
}
Let's begin with the source code you're missing. Here you go:
public void specificInfo() {
FacesMessages.info("growlForm:ref", "Info", "This is a specific message!");
}
public void error() {
FacesMessages.error("Error!", "Something has gone <strong>wrong</strong>.");
}
You'll notice the only difference is the number of parameters. specificInfo() includes the id growlForm:ref. You didn't include it with your code snippet, but growlForm is the id of the surrounding form (at least in our showcase). The second part of the id, ref, indicates that the FacesMessage is to be displayed by an <h:message>, <b:message>, <p:message>, <p:growl> or <b:growl> which is defined inside this form and bears the attribute for="ref".
Looking at the two <b:growl>s of the showcase, you'll see that the first growl doesn't have a for attribute. Instead, it sets globalOnly="true". This causes the growl to ignore every FacesMessage bearing an id. It'll ignore the messages generated by specificInfo().
So that's why there are two <b:growl>s in our showcase example, but every user action only triggers one of them.
There's that. Your issue is the other way round: One <b:growl> displays two growl elements on the screen. You haven't provided any Java source code (yet?), so I can only guess. The most likely explanation is that you're really generating the FacesMessage twice. I recommend setting a breakpoint in your debugger (or adding a System.out.println(), if you prefer that approach) to rule that out.
I hope I've given you enough clues to solve your issue. If not, don't hesitate to reach out to us on our bug tracker on GitHub. Please include the link to this StackOverflow question, so I can update this answer if necessary.

JSF PrimeFaces rendering components

Let's say I have a simple method that, like this:
public String test()
{
return "hello";
}
Now let's say I have the following PrimeFace component:
<p:fieldset styleClass="containers" rendered="#{controller.test()}">
<h2>Testing</h2>
<p:outputLabel for="test" value="Test" />
<p:inputText id="test" />
</p:fieldset>
The method above returns "hello". I would like to dynamically show and hide that fieldSet by comparing the returned value of that method to a field of one of my beans. For instance, on the rendered parameter, I would like to do something like: controller.test() != "some variable" which would return true or false. Am I allow to do that? If not, what is the way of doing it?
Basically the goal is to dynamically show and hide some container by comparing the returned value of a method with a bean property.
Look Like you misunderstood rendered
The rendered Attribute
A component tag uses a Boolean EL expression, along with the rendered
attribute, to determine whether or not the component will be rendered.
If you will check above definition you will know what exactly is the use of this attribute.
More you can see below
The rendered attribute which uses Boolean EL expression indicates
whether a component is currently visible or not. The property is
useful when you want to hide components for specific users or based on
condition. The default value of rendered attribute is true.
<h:outputText value=”mapping” rendered=”Boolean EL expression” />
For example, the commandLink component in the following section of a page is not rendered if the cart contains no items:
<h:commandLink id="check"
...
rendered="#{cart.numberOfItems > 0}">
<h:outputText
value="#{bundle.CartCheck}"/>
</h:commandLink>
With your concrete problem you can do like this
Make a String variable call value
Now create get/set methods for above variable
Now in your test method you can add
public void test(){
value="hello";
}
Bur remember you have call test() method of page load
Now in your Xhtml or Jsf or Jsp page
rendered="#{controller.value != 'hello'}"
Or better way create a Boolean variable and do all the magic of hide and show the component

JSF recursive ui:include jumbles output tree on multiple postback

I have a report that displays college course enrollment information in an expandable tree format. It displays the course prefix with the summary enrollment numbers and the specific course enrollment numbers when the prefix is clicked on. The report works fine when it is first run. Should you decide to run the report again for another session, however, things start to get jumbled up.
When Report One is run, it is correct. When Report Two is run (for a different session, same session runs work fine), notice the second has duplicated/missing elements. Both of these reports will work correctly if they're run "first", ie, no other report has been run in that users session.
I'm using a recursively included page to build the tree. Here's the pertinent code from enrollment.xhtml, the report page itself:
<div id="resultSet" class="treeContainer">
<c:if test="${EnrollmentBean.reportRan}">
<ui:include src="/WEB-INF/includes/reportTree.xhtml">
<ui:param name="nodes" value="#{EnrollmentBean.reportTreeData.getChildren()}" />
<ui:param name="isHidden" value="false" />
</ui:include>
</c:if>
</div>
The included reportTree.xhtml code (tree expansion code done with CSS/jQuery):
<ul class="${isHidden ? 'hidden' : ''}">
<c:forEach items="#{nodes}" var="node">
<li><a href="#" class="reportTreeLabel">#{node.getData().getKey()}
<span class="reportTreeData">#{node.getData().getValue()}</span>
</a>
<c:if test="#{node.hasChildren()}">
<ui:include src="/WEB-INF/includes/reportTree.xhtml">
<ui:param name="nodes" value="#{node.getChildren()}" />
<ui:param name="isHidden" value="true" />
</ui:include>
</c:if>
</li>
</c:forEach>
</ul>
The pertinent parts of the backing bean, EnrollmentBean.java:
#Named("EnrollmentBean")
#SessionScoped
public class EnrollmentBean implements Serializable {
/** Holds Report Data */
private List< List<String> > reportData;
/** Hold Report Data in tree format */
private TreeNode<SimpleEntry<String, Integer>> reportTreeData;
private void buildReportTree() {
this.reportTreeData = new TreeNode<SimpleEntry<String,Integer>>();
String prefix = "";
Integer prefixEnrollSum = 0;
// Stores the tree data for the prefix being processed. Once all the data has been processed, this is added as a child to the reportTreeData field.
TreeNode<SimpleEntry<String,Integer>> PrefixTree = null;
SimpleEntry<String,Integer> data = null;
for (List<String> line : this.reportData) { // for each raw data line
String course = line.get(0);
Integer enrollments = Integer.parseInt(line.get(1));
if (!prefix.equals(this.getPrefix(course)) ) { // if the prefix changed since the last line that was processed
prefix = this.getPrefix(course); // set the new prefix
if (PrefixTree != null) { // if we're not dealing with the very first line of data
// set the sum of enrollments gathered for this prefix and reset the counter for the next prefix.
PrefixTree.getData().setValue(prefixEnrollSum);
prefixEnrollSum = 0;
}
// prepare a data element for the prefix summary node, then create the node, passing in the data and the parent for this node.
data = new SimpleEntry<String,Integer>(prefix, 0);
PrefixTree = new TreeNode<SimpleEntry<String,Integer>>(data);
this.reportTreeData.addChild(PrefixTree);
} // end prefix changed
data = new SimpleEntry<String,Integer>(course, enrollments);
PrefixTree.addChild(data);
prefixEnrollSum += enrollments;
} // end for each data line
// If there was data to process, upon processing the final row, assign the summed enrollments to the final prefix tree.
if (PrefixTree != null) { PrefixTree.getData().setValue(prefixEnrollSum); }
}
}
The TreeNode class is one that I've created and it provides simple tracking of data, parent, and children. I can post that code if needed, but I believe it is superfluous at this time.
During my troubleshooting of this problem, I've verified the reportData the report tree is built on in the backing bean is always correct. Through using the Logger class, I've verified that the tree is being generated correctly (by writing to the server log each line that was being processed into the tree). I've even verified that the reportTreeData is correct after every run by writing out the tree to the server log after the tree was constructed.
I can only conclude that something is going wrong in the Render Response phase of the JSF lifecycle, as I did notice that if I change the backing bean from #SessionScoped to #RequestScoped, the report is generated correctly every time. I'd rather not us this as my fix, as I have a "Download CSV" link that uses the already generated report data in the backing bean so the report logic doesn't need to re-run to generate the CSV.
Does anyone know why this is happening and what I can do to correct this behavior? I'm using JSF 2.2 with Java EE 7 on GlassFish Open Source Edition 4.1 (build 13)
UPDATE 12/24/15
I've been stepping through the render response phase JSF code and it seems that the EL expression is just being evaluated with the wrong data on a second report run. I can see where it is making the function call to evaluate the EL expression and it is coming back with the wrong data. I will try to get the weld-osgi-bundle.jar source so I can step into that function call at a later date.
Based on a lot of debugging and research, but specifically this answer, I assume my issue has something to do with the view state trying to be restored and the recursion I'm using is making it hard on the JSF framework to properly update the component tree model.

How to get values returned by child action method in mvc 5 partial view

I am trying not very successfully to get my head around MVC. My home controller contains an Index method that runs OK, so far so good, but I don't know how to call the ChildAction method Home/TopArticle
Action Method
[ChildActionOnly]
public ActionResult TopArticle()
{
return PartialView(_service.GetTopArticle());
}
In my Index view I have the mark up:
#section featured {
#Html.Partial("_TopItem")
}
_TopItem View
#model IEnumerable<MySite.Models.NewPage>
<section class="featured">
<div id="TopItem">
<div id="TopItemImg">
<a href="http://www.mysite.co.uk/">
<img style="border: 1px solid lightgray" width="320" height="233" style="border:1px solid lightgray;" alt="Model.Title" src="/Img/Model.TopItemImage">
</a>
</div>
<div id="TopContent">
<h2></h2>
<div class="dt">
<div class="dl">
#Html.Label(Model.DatePublished.ToString())
#Html.Label(#Html.Action("TopArticle", "Home", new { // am lost at this point}))
</div>
<div class="tl">
#Html.Label(Model.InfoTags ?? "")
</div>
</div>
</div>
</div>
</section>
The Index view is also using #model IEnumerable and I don't actually know whether that's OK or not. The model itself contains everything needed for both the Index and the _TopItem views, it's just that there will be one record returned for the _TopItem view and many for the Index view. Plus the code that runs in _service.GetTopArticle does some non-query stuff that is relevant only for the top article record.
I need a lie down ... and time to learn this stuff properly.
Firstly, regarding your question about calling the child action from your Index view:
Your featured section is currently calling #Html.Partial which means that it will find the "_TopItem" partial view and render it as an html encoded string in the current view (i.e. your Index view).
You specified that you are trying to call the child action TopArticle() and render the partial view returned as a html string in the view. To do this you would need to use:
#section featured {
#Html.Action("TopArticle", "Home")
}
However, I don't believe this is what you do need as you said that your Index view model contains all of the information for both Index and for the _TopItem partial view (see later).
For more information you should do a google search about the differences of views, partial views and child actions.
To correct the code I would start off by ensuring that the _TopItem partial view is correct. I have identified the following issues with the _TopItem partial view, some of which are beyond the scope of the original question:
The model passed in as an IEnumerable of NewPage but your code does not enumerate over several new page objects, it looks like it should just create the html for a single NewPage model. Therefore, I believe the model declaration should be:
#model MySite.Models.NewPage
The tag contains 2 references to the style attribute rather than 1.
The tag contains the alt attribute of alt="Model.Title" which means that alt="Model.Title" will be written directly as html where I expect you would like alt="#Model.Title" to render the contents of the model in the alt attribute.
Similarily, the tag contains src="/Img/Model.TopItemImage" where I expect this should be src="/Img/#Model.TopItemImage"
All of the label tags appear to be incorrect. For example, #Html.Label(Model.DatePublished.ToString()) - Model.DatePublished.ToString() will return a string and this string will then be attempted to be found on the model and will error as that field name does not exist. Therefore, you probably want to write: #Html.Label("DatePublished") or #Html.Label(m => m.DatePublished). With the second label i'm not sure what your trying to achieve but you may want to look up the appropriate articles.
Once, you have the corrected _TopActicle partial view, you can then return to your Index view to render the partial directly:
#section featured {
#Html.Partial("_TopItem", Model.TopArticle)
}
Note, as you have said that your Index model contains the information to pass to the _TopItem partial view, I have assumed that the Index model contains a property called TopArticle of type NewPage. Regardless, you can pass the model into the partial however you find appropriate through the call to #Html.Partial. If you pass the model through the call to #Html.Partial then you may not need the ChildOnlyAction.

How do I implement a NamingContainer? All children get the same client ID

I try to write my own tree component. A tree node renders as a div containing child components of the tree component, for example:
<my:tree id="extendedTree"
value="#{controller.rootNode}"
var="node">
<h:outputText id="xxx" value="#{node.name}" />
<h:commandLink value="Test" actionListener="#{controller.nodeSelectionActionListener}" />
</my:tree>
So far, so good - everything works as expected, but the h:outputText gets the same id repeatedly.
So I had my component implement javax.faces.NamingController, overwriting getContainerClientId():
#Override
public String getContainerClientId(FacesContext context) {
String clientId = super.getClientId(context);
String containerClientId = clientId + ":" + index;
return containerClientId;
}
index is set and updated during iteration over the nodes. But getContainerClientId() is called only once for every children (not for every iteration and every children, as I would expect). That causes every child id to be prefixed with the same container id:
form:treeid:0:xxx
Same thing for overwriting getClientId().
What did I miss?
The answer is hidden in the bottom of chapter 3.1.6 of JSF 1.2 specification:
3.1.6 Client Identifiers
...
The value returned from this method must be the same throughout
the lifetime of the component instance unless setId() is called, in which case it will be
recalculated by the next call to getClientId().
In other words, the outcome of getClientId() is by default cached by the JSF component as implemented in UIComponentBase#getClientId() (see also the nullcheck at line 275 as it is in Mojarra 1.2_15) and this cache is resetted when the UIComponentBase#setId() is called (see also line 358 as it is in Mojarra 1.2_15). As long as you don't reset the cached client ID, it will return the same value on every getClientId() call.
So, while rendering the children in encodeChildren() implementation of your component or the renderer which shall most probably look like this,
for (UIComponent child : getChildren()) {
child.encodeAll(context);
}
you should for every child be calling UIComponent#setId() with the outcome of UIComponent#getId() to reset the internally cached client ID before encoding the child:
for (UIComponent child : getChildren()) {
child.setId(child.getId());
child.encodeAll(context);
}
The UIData class behind the <h:dataTable> implementation does that by the way also (see line 1382 as it is in Mojarra 1.2_15). Note that this is not JSF 1.x specific, the same applies on JSF 2.x as well (and also on UIRepeat class behind Facelets <ui:repeat>).
It's worth mentioning that if your component's children have children, then it may also be necessary to refresh their cached ids, too. With this mark-up, slightly adapted from the original:
<my:tree id="extendedTree"
value="#{controller.rootNode}"
var="node">
<h:panelGroup layout="block" id="nodeBlock">
<h:outputText id="xxx" value="#{node.name}" />
<h:commandLink value="Test" actionListener="#{controller.nodeSelectionActionListener}" />
</h:panelGroup>
</my:tree>
The id for the <panelGroup> comes out OK after applying BalusC's fix above but all the sub-components come out with 0 in the iterator.
To fix that, iterate through all the levels of children too and refresh their cached ids. So:
child.setId(child.getId()); becomes uncacheId(child); where the uncacheId function is defined:
private void uncacheId(UIComponent el) {
el.setId(el.getId());
el.getChildren().forEach(this::uncacheId);
}
That may be obvious but it took me a while to figure out, so ...
h:outputText id gives you same as you didn't make it dynamic. You can create it like:
<h:outputText id="xxx_#{node.id}" value="#{node.name}" />
Assume node has an 'id' attribute which is an unique.

Resources