h:commandbutton calls wrong action method - jsf-2

I faced a interesting problem here. When I click commandButton, it calls wrong action method even button has not action attribute. What is this?

That may happen when the EL expression which you intented to use as a method expression is been evaluated as a value expression.
For example, when you have outcommented it using a HTML comment while not having configured Facelets to skip comments.
<!-- <h:commandButton value="submit" action="#{bean.submit()}" /> -->
The HTML comments namely doesn't stop EL expressions like #{bean.submit()} from being evaluated. It will ultimately generate HTML output with the return value of #{bean.submit()} inlined in the action attribute.
To naildown the root cause in your case, you should be creating and providing a fullworthy SSCCE.
See also:
How can I remove HTML comments in my Facelets?
Outcommented Facelets code still invokes EL expressions like #{bean.action()} and causes javax.el.PropertyNotFoundException on #{bean.action}

Related

JSF 2.0 h:datatable not using supplied id attribute

I'm observing the following behavior within a JSF 2 page (Mojarra 2.1.18 / RedHat EAP 6.1, if that's useful). I've got a form wrapping a data table, and I'm supplying an ID attribute value for both the form and the table. When I view the resulting html source file, I see that the table ID is set to an auto-generated value and the form ID attribute is not prepended to the auto-generated table ID. That is:
This set of tags:
<h:form id="form4">
<h:datatable id="notices" ...>
...
</h:datatable>
</h:form>
Yields the following html:
<form id="form4" ...>
<table id="j_itd68"> //i.e. id != "notices"
...
</table>
</form>
There are more elements in the JSF xhtml file preceding the h:form/h:datatable, but I've intentionally excluded them here (hoping that someone might recognize this symptom without additional clutter). Things go wrong for me when I attempt to include some PrimeFaces p:commandbutton instances within the table. They don't get named properly (i.e. they don't include the enclosing form ID in the generated ID), and this causes a "component not found for ID" servlet error. The same improper naming occurs if I replace the p:commandbutton instances with h:commandbutton instances (so I don't believe this is a PrimeFaces issue). This behavior seems like the result of a malformed JSF page, but I haven't found anything yet (a NetBeans XML check on the JSF xhtml file returns a successful result). Any help is appreciated.
Best regards,
-Andy

How do you compare 2 struts param in a struts if

I'm am still pretty new to struts and am having trouble trying to compare two struts params in a struts if statement. I am trying find if the current year param is equal to the checkYear param
If they are equal I want to execute some code. If they are not equal then I would like to execute some other code..
Sample code:
<s:param name="currentYear">
<s:date name="%{new java.util.Date()}" format="yyyy" />
</s:param>
<s:param name="checkYear">
<s:date format="yyyy" name="#year"/>
</s:param>
<s:if test="${checkYear == legendYear}">
code executed
</s:if>
I don't really understand the purpose of the '$','%', or '{}' in the test part of the if statement if or how I need to apply it to have it check the params.
Thank you in advance!
Inside Struts2 tags, you use OGNL. Refer to the small language guide to see how it works.
In OGNL, %{} means you want to force the evaluation of an expression; it is needed somewhere, while it is superfluos somewhere else. In the <s:if/>, it is not needed, because test="" will evaluate its content by default.
${} does not exists in OGNL, it is JSP EL. Search on StackOverflow for some nice answer on that if you are curious.
{} alone in OGNL is List Projection... you probably don't need it now.

Can I use Get-Parameter in EL 2.2 without writing it into the Bean?

I want to parse the URL
title.xhtml?id=1
My code within "title.xhtml" should look like
...
<h:outputText value="#{titles.getTitle(${param.id}).id}"></h:outputText>
...
But unfortunatelly this does not work, since nested "#" and "$" is not accepted.
So my question is: Can I use an URL parameter and hand it over to a bean function without to store it separately within the bean?
That's invalid EL syntax. You can't and don't need to nest EL expressions in any way. Even nesting ${} is invalid. The only difference between #{} and ${} is that the #{} can perform a set operation as well (where applicable), while the ${} can do only a get operation.
This is valid EL syntax:
<h:outputText value="#{titles.getTitle(param.id).id}" />
Note that #{param.id} is fully legal JSF EL syntax. To avoid future confusions, it would be a good idea to make sure that you never use the old JSP EL syntax ${} in JSF anymore. See also Difference between JSP EL, JSF EL and Unified EL.

Richfaces4.2 conditional rendering issue

We are in process of migrating from JSP VDL to Facelets VDL. We have conditional rendering tag need to be ported to Facelets. Because of && symbol in condition rendering, .xhtml failing in compilation. Any thoughts on how to handle this?
<a4j:outputPanel styleClass="myclass" layout="block"
rendered="#{myBean.iscorrect && anotherBean.isCorrect}">
render something here ...
</a4j:outputPanel>
Thanks for your time.
Use and instead of &&. It's also immediately more self-documenting.
<a4j:outputPanel styleClass="myclass" layout="block"
rendered="#{myBean.iscorrect and anotherBean.isCorrect}">
The reason is because Facelets is a XML based view technology and that & is a special character in XML representing the start of an entity. The exact Facelets compilation error message which you have gotten should also have hinted something about that. Other special characters to be careful for are < and > which should in EL be replaced by lt and gt respectively.
This problem is unrelated to RichFaces. You would have exactly the same problem when doing so in standard JSF tags.
By the way, do you really have a isIscorrect() getter method? It would make more sense to me if you had a private boolean correct; with public boolean isCorrect() method and are evaluating as rendered="#{myBean.correct and anotherBean.correct}".

f:verbatim tag stops working when inside a dataTable

I posted this to the PrimeFaces user forum but I think they are too busy to look into it, so I thought I would try here.
I have server-side string that has markup in it, so when I want it rendered I do this:
<p:panel>
<f:verbatim>
#{daBean.markedUpString}
</f:verbatim>
</p:panel>
This works fine, but not if the same tag is used inside a p:dataTable -- either with or without the p:panel enclosure. What gets rendered is a div class="ui-dt-c" element with nothing in it. To test, if I take out the f:verbatim tag the marked-up text gets escaped and rendered.
I don't know if this should be considered a bug or not, but does anyone know of a work-around for this? This is with PrimeFaces 3.0.M3.
The <f:verbatim> tag is intented to hold plain text/HTML, not JSF components nor EL expressions. The tag is a leftover from JSF 1.0/1.1 ages when it was not possible to inline plain text/HTML between JSF components. The tag is deprecated in JSF2. You do not need it anymore.
Your concrete functional requirement is thus displaying some HTML string from a managed bean unescaped. For that you should use <h:outputText> with escape="false".
<h:outputText value="#{daBean.markedUpString}" escape="false" />
See also:
Getters inside f:verbatim called before form submission
JSF/Facelets: why is it not a good idea to mix JSF/Facelets with HTML tags?
What are the main disadvantages of Java Server Faces 2.0?

Resources