Referencing ui:params with hyphens in the name - jsf

After many years of avoiding JSF, I'm finally diving in but having problems doing the simplest of things. In the JSTL world, it's easy to reference things with hyphens in the name by doing ${requestScope['big-bad-invalid-name']}. However, that doesn't seem to work with a <ui:param> (or I'm simply starting with the wrong object--the likely issue).
I have a simple file that's referencing a template:
<html xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition template="/WEB-INF/template/main.xhtml">
<ui:param name="require-data-main" value="/something" />
<ui:define name="content">...</ui:define>
</ui:composition>
</html>
Sadly I can't figure out how to reference my param since #{require-data-main} is going to freak out due to the hyphens. I've tried #{param['require-data-main']}, #{viewScope['require-data-main']}, #{pageScope['require-data-main']}, #{applicationScope['require-data-main']}. My param doesn't seem to exist anywhere.
Update #1 -- I created another param without hyphens (camel case). Nothing managed to resolve the value as I've tried above. A plain #{requireDataMain} did, however. I have a large number of JSPs already written. So I'm not too keen on changing my variable names. I'd still love to find out how to reference the param with the hyphens in the name.
Has anybody seen my param? Is there an alternate way to retrieve the param?

A JSF page should use Managed Bean to access your java code. If you have a controller class for your jsf page, you could inject it to jsf using EL: #{managedbean.field} where managedbean is a following class:
#ManagedBean(name = "managedbean")
public class YourBean {
private String field;
//getter and setter for field
}
Hope that I understood your problem correctly.

Related

Property 'addListener' not found on type com.chat.viewbeans.ClientBean [duplicate]

While deploying GAE + primefaces application, I got following error:
com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException: Error Rendering View[/CreateEmployee.xhtml]
javax.el.ELException: /CreateEmployee.xhtml: Could not find property saveEmployee in class com.fetchinglife.domain.data.dao.EmployeeRepositoryImpl
at com.sun.faces.facelets.compiler.AttributeInstruction.write(AttributeInstruction.java:94)
at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302)
at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
Even saveEmployee is already in EmployeeRepositoryImpl class. How is this caused and how can I solve it? Are there any annotations which I have to put extra?
Let's rephrase the exception in a general format in order to better understand the cause:
javax.el.ELException: Could not find property doSomething in class com.example.Bean
This exception is literally trying to tell you that the Bean class doesn't have the following method:
public SomeObject getDoSomething() {
return someObject;
}
(where SomeObject is the type of the doSomething property)
There are several causes for this.
Action method expression is incorrectly interpreted as property value expression
Given the fact that the property name is in your case actually a combination of a verb and noun, a resonable cause is you incorrectly bound an action method to an attribute of some JSF component which actually takes a value expression, not a method expression. E.g. when you accidentally do
<h:commandButton value="#{bean.doSomething}">Save</h:commandButton>
or even
<h:button value="Save" outcome="#{bean.doSomething}" />
instead of
<h:commandButton value="Save" action="#{bean.doSomething}" />
The latter would then expect the following method, which you probably actually have:
public String doSomething() {
// ...
return "nextpage";
}
(which can also be declared to return void by the way)
Component is not resolved at all
Another probable cause is that the component is not interpreted as a real component at all, but as "plain text". In other words, when you remove the action attribute and then try to open the JSF page in browser, it'll now load fine, but you will see the whole component unparsed in the generated HTML output (which you can see via rightclick, View Source in browser).
This can have several causes:
The XML namespace of the component is wrong or missing. E.g. in case of PrimeFaces you accidentally used the old PrimeFaces 2.x URI while you're actually using PrimeFaces 3+.
<html ... xmlns:p="http://primefaces.prime.com.tr/ui">
The XML namespace URI contains a typo.
<html ... xmlns:p="http://primefaecs.org/ui">
The XML namespace prefix does not match.
<html ... xmlns:pf="http://primefaces.org/ui">
The XML namespace is totally missing.
<html ...>
The component library is not installed at all. In other words, the JARs containing component library's tag definitions is missing in webapp's /WEB-INF/lib folder.
Either way, all <p:xxx> tag won't be parsed and be considered as template text. But, all EL expressions will still be evaluated (as if you're using <p>#{bean.text}</p>), and they will all behave as ValueExpressions instead of MethodExpressions.
An easy way to recognize the root cause is looking at stack trace. If you're seeing com.sun.faces.facelets.compiler.AttributeInstruction in the stack trace, then it means that the component is interpreted as "plain text". Otherwise you would have seen e.g. org.primefaces.component.commandbutton.CommandButton in the specific case of <p:commandButton>.

primefaces update attribute not working on modal dialog opened from modal dialog [duplicate]

I have a question about the idea behind the fact, that only UIForm got the attribute prependId. Why is the attribute not specified in the NamingContainer interface? You will now probably say that's because of backward compability but I would preferre breaking the compability and let users which implement that interface, also implement methods for the prependId thing.
The main problem from my perspective about the prependId in the UIForm component is, that it will break findComponent()
I would expect that if I use prependId, then the NamingContainer behaviour would change, not only related to rendering but also when wanting to search for components in the component tree.
Here a simple example:
<h:form id="test" prependId="false">
<h:panelGroup id="group"/>
</h:form>
Now when i want to get the panelGroup component I would expect to pass the string "group" to the method findComponent(), but it won't find anything, I have to use "test:group" instead.
The concrete problem with that is, when using ajax with prependId="false". The ajax tag expects in the attributes update and process, that the values care of naming containers. It's a bit strange that when I use prependId="false" that I have to specify the full id or path, but okay.
<h:form id="test" prependId="false">
<h:panelGroup id="group"/>
</h:form>
<h:form id="test1" prependId="false">
<h:commandButton value="go">
<f:ajax render="test:group"/>
</h:commandButton>
</h:form>
Well this code will render without problems but it won't update the panelGroup because it cannot find it. The PartialViewContext will contain only the id "group" as element of the renderIds. I don't know if this is expected, probably it is but I don't know the code. Now we come to the point where the method findComponent() can not find the component because the expression passed as parameter is "group" where the method would expect "test:group" to find the component.
One solution is to write your own findComponent() which is the way I chose to deal with this problem. In this method i handle a component which is a NamingContainer and has the property prependId set to false like a normal UIComponent. I will have to do that for every UIComponent which offers a prependId attribute and that is bad. Reflection will help to get around the static definition of types but it's still not a really clean solution.
The other way would be introducing the prependId attribute in the NamingContainer interface and change the behaviour of findComponent() to work like described above.
The last proposed solution would be changing the behaviour of the ajax tag to pass the whole id, but this would only solve the ajax issue and not the programmatic issues behind the findComponent() implementation.
What do you think about that and why the hell is it implemented like that? I can't be the first having this problem, but I wasn't able to find related topics?!
Indeed, UIComponent#findComponent() as done by <f:ajax render> fails when using <h:form prependId="false">. This problem is known and is a "Won't fix": JSF spec issue 573.
In my humble opinion, they should never have added the prependId attribute to the UIForm during the JSF 1.2 ages. It was merely done to keep j_security_check users happy who would like to use a JSF form with JSF input components for that (j_security_check requires exact input field names j_username and j_password which couldn't be modified by configuration). But they didn't exactly realize that during JSF 1.2 another improvement was introduced which enables you to just keep using <form> for that instead of sticking to <h:form>. And then CSS/jQuery purists start abusing prependId="false" to avoid escaping the separator character : in their poorly chosen CSS selectors.
Just don't use prependId="false", ever.
For j_security_check, just use <form> or the new Servlet 3.0 HttpServletRequest#login(). See also Performing user authentication in Java EE / JSF using j_security_check.
For CSS selectors, in case you absolutely need an ID selector (and thus not a more reusable class selector), simply wrap the component of interest in a plain HTML <div> or <span>.
See also:
How to select JSF components using jQuery?
How to use JSF generated HTML element ID with colon ":" in CSS selectors?
By default, JSF generates unusable ids, which are incompatible with css part of web standards

Why does my Object become null when going from bean to .xhtml page in jsf

I am building a JSF 2.0 Application using primfaces. I have a managed bean that has several objects for different scenarios.
In this particular portion of the application I move from a data table/form to a managed bean with an identification number.
I pull the information from the database and even have it in the log. However when I make it to the
JSF xhtml page the object values are null and I can't seem to figure out why, any help would be great as I am getting no errors,
exceptions, or warnings in the logs and all information seems to point to the loading of the data including the log.
JSF Page that I am coming from... jobs.xhtml (I am using jsf 2.2 and primefaces to build this application) From this page the
link specifies a job number to be retrieved and the next bean will retrieve that job based on the job number I have it down to just a button
that will take you to the primary method and get the information to the next page...
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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:p="http://primefaces.org/ui">
<body>
<div style="margin-bottom:350px;">
<p:column>
<h:form>
<h:commandButton id="editProject" action="#{editProjects.getProjectForm()}" value="Edit" />
</h:form>
</p:column>
</div>
</body>
</html>
EditProjects.java the backing bean for the editMailerJob.xhtml which will take in the medium id and the job number the medium id will be used
to direct the application to the next page and the job number will be used to retrieve a specific job
::::UPDATED::::
package beans;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.RossProjectManagement.rpm.objects.MailerProject;
#ManagedBean
#RequestScoped
public class EditProjects implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6680733133634363295L;
private final Log LOG = LogFactory.getLog(this.getClass().getName());
// Editable projects
private MailerProject edit_mailer = new MailerProject();
//Changed this to be a static set value
private String mailerProject = "THIS IS THE MAILER STRING";
public int getProjectForm() {
loadEditMailer();
return 1;
}
public void loadEditMailer() {
this.edit_mailer = new MailerProject("REDJ15061005",
convertDate("2015-06-29"), false, "RickD",
"RickD and the Gang", "new", true, true, "SPEC", 1, 1, 1,
"REDJ15061005", "11X17", "SPOT_COLOR", "20,000",
convertDate("2015-06-30"), convertDate("2015-07-13"),
convertDate("2015-07-06"), convertDate("2015-07-06"),
convertDate("2015-07-15"), true, "BDC",
"someone#example.com", "STANDARD", 7500.00, "CONQUEST",
"NONE", "JS DIRECT", "NONE");
//Commented this out to keep the value static
//setMailerProject(edit_mailer.toString());
LOG.debug(":::MAILER PROJECT TO STRING::: " + this.mailerProject);
}
public Date convertDate(String date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
try {
date1 = df.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return date1;
}
}
JSF page that I am redirecting to editMailerJob.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!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:p="http://primefaces.org/ui">
<body>
<div style="margin-bottom: 350px;">
<h:outputLabel>#{editProjects.edit_mailer}</h:outputLabel>
<h:outputLabel>Mailer String::: #{editProjects.mailerProject} </h:outputLabel>
</div>
</body>
</html>
The log shows that all of the information has been received and loaded but shows no where that the information has been cleaned or the object made anew. I have tried instantiating the object in different places at different times but that didn't work.
Before I leave the bean the object has value, when I get to the page the object has no value, I don't understand why
I then tried loading the information from different places. I have tried changing the scope of the bean from request to view back to request but no luck, (I thought about doing session but I didn't see the benefit as well as I just couldn't see reasoning to put all of that information into the session).
The problem seems to be revolving directly around the bean itself and the getProjectForm Method because at one point the object is loaded and then it just loses all of its information.
All of my values seem to be correct as far as I can tell but none of the information is loading.
I can't seem to wrap my head around the issue so any help would be greatly appreciated. if any more information is needed just let me know.
I hope all of this helps Let me know if I have forgotten anything.
EDITS:::
I have created just a string to see if at any point it would be delivered either, and so far I am still getting nothing, the .xhtml page is reading that it exists but not that it has value. Still working with this if anyone else has a solution it would be awesome, or if you know the solution and could at the least point me in the right direction that would be great also.
UPDATE #2
So I found that if I set the value outside a method it will properly be displayed so it seems to me like the methods are keeping the value from the xhtml page. What is the deal with that, I have getters and setters for the variables I have them declared and defined properly, but for what ever reason the values when loaded by the method are not kept outside the method, the values are not able to be accessed by the xhtml page.
10:00 pm update
Found out that if I instantiate the MailerProject object the same way I don't get the same results I still produce an object full of null attribute values.
Any ideas guys?
Start Here - It'll make the rest of this very trivial.
The Problem
You're using a #RequestScoped bean, and what you're experiencing is the expected behaviour for beans of that scope.
Between the first and the second page, you should understand by now, that you're dealing with two different instances of the editProjects bean : a field you set on the first page, backed by one instance of editProjects will not be available on the destination page, backed by a new instance of editBean.
To Solve
While it wasn't mentioned in the answer I linked to, what you need here is the FlashScope, that serves the express purpose of being transitory in nature: use it just to transport "stuff" between two pages/beans/scopes etc. Sorta. To use:
Declare your undying love for the flash scope around the point of origination of the data. In so doing, you're storing your data in the flash scope object, as implicitly provided by JSF, after which you can then navigate away:
Flash fScope = FacesContext.getCurrentInstance().getExternalContext().getFlash();
fScope.put("editMailer",edit_mailer);
You can then refer to the variable you stored in the flash scope, on the destination page:
<h:outputLabel>#{flash.edit_mailer}</h:outputLabel>
Like I mentioned earlier, the flash scope is a transitory scope: once the destination page has been rendered and the content of the scope displayed, you can consider the data as good as gone. If you would like to hang on to it for just one page redirect longer, you can specify the keep directive:
<h:outputLabel>#{flash.keep.edit_mailer}</h:outputLabel>
Unrelated to the problem
Avoid doing any heavy lifting in a getter/accessor method (like you're doing in getProjectForm). It's bad for business
Why JSF calls getters multiple times
Initialization of List in a JSF Managed bean
What's the significance of returning "1" from that navigation method? Why not "destination_page", or "edit_page.xhtml", y'know, something easier to read? That's just a style thing anyway, no evil can come of that.

h:link with includeViewParams=true does not always include viewparams

For some reason,the does not always append the view parameters to the produced link. I can't figure out why. If I change the outcome to another similar link,then it is processed correctly.
Can anyone point me to some requirement that could not be fulfilled? I didn't find anything relevant in the docs.
I am trying to set up 4 views backed by a single bean. Those views all contains the same view parameters, but only one of them process them. All view use the same template.
So i have in all views
<f:metadata>
<f:viewParam name="param1"
value="#{bean.param1}"/>
<f:viewParam name="param2"
value="#{bean.param2}"/>
</f:metadata>
<ui:composition template="/onpage/template.xhtml">
// ...
And in one of them, i included a <f:viewAction>
In the template I have some links
<h:link outcome="#{bean.outcome1}"
value="Go to view1"/>
<h:link outcome="#{bean.outcome2}"
value="Go to view2"/>
and in the bean:
private String param1; // And get/setters
private String param2; // and get/setters
public String getOutcome1() {
return "/my/path.jsf?some=param&includeViewParams=true";
}
public String getOutcome2() {
return "/my/path2.jsf?some=param2&includeViewParams=true";
}
With this setup, some of the links point to "/my/path.jsf?param1=value1&param2=value2" as expected, while others point to "/my/path2.jsf?some=param2" for no apparent reason, and without any information in the log.
All this running on glassfish 4/JSF 2.2/primefaces 4.
THanks
Answer part of the OP comments:
What happened is that I mixed XML namespace domains. The pages using xmlns:f="java.sun.com/jsf/core were working correctly, while those using xmlns:f="xmlns.jcp.org/jsf/core were not. See The metadata component needs to be nested within a f:metadata tag. Suggestion: enclose the necessary components within <f:metadata> for more details.
Note: JSF 2.2 schemas use the xmlns:f="xmlns.jcp.org/jsf/core namespace.

how to generate dynamic rich:panelMenu?

i have a problem to generate dynamic menu, i'm using JSF1.2. I tried the c:forEach with an arrayList to generate dynamic rich:panelMenu as BalusC advised in a related forum, but get Accessor never triggered in c:forEach. it ruined me a day. can anyone provide me a solution ?
<c:forEach items="#{serviceListBean.services}" var="child">
<rich:panelMenuItem mode="none">
<h:outputText value="#{child.serviceId}"></h:outputText>
</rich:panelMenuItem>
</c:forEach>
what's wrong in this code? can anyone enlighten me?. For info, serviceListBean is request scoped bean.
Two possible causes:
JSTL is not declared as taglib in JSP or Facelets. To confirm this, rightclick page in browser and choose View Source. Do you see <c:forEach> tag unparsed among the generated HTML? If you're using JSP, declare it as follows:
<%#taglib prefic="c" uri="http://java.sun.com/jsp/jstl/core" %>
Or if you're using Facelets, declare it as follows in root element:
xmlns:c="http://java.sun.com/jsp/jstl/core"
On some servers like Tomcat, which doesn't ship with JSTL out the box, you would also need to install JSTL first, see also this info page.
This piece of code is in turn been placed inside a JSF repeating component, like <h:dataTable> with a var="serviceListBean". This is also not going to work. You would need to replace the JSF repeating component by <c:forEach> as well.

Resources