<c:if> test="" in jsf - jsf-2

I'm using primefaces and jstl to loop a datatable.I have a List in backing bean for the columns.
private List<String> visableCols;
public initCols(){
visableCols.add("andOr");
visableCols.add("operator");
......
}
// getter & setter
In the xhtml page.
<p:comlumns var="col" value="#{theBean.visableCols}" >
<c:if test="#{col == 'andOr'}">
<!-- do sth here -->
</c:if>
</p:comumns>
but I found the c:if always false.I tried to print out the #{col} and compare w/ 'andOr',they are the same value.

If you are using PrimeFaces and you want to dynamically add and remove columns, PrimeFaces provides a specific way of doing that. You don't need any JSTL tags. For a good example of how to do it just look at their showcase example. It is pretty involved but fairly clean.
Note: you use the p:columns tag instead of p:column.

Related

c:forEach not working on ace:dataTable var [duplicate]

In my application I want to display a <h:dataTable> with managed bean properties. Currently this table is created from a List<Folder>. Now I want to change the Folder to something more dynamic. That's because I don't want to change the Folder class if I decide to add another field later. I would just have to add another entry in the Map<String, Object> instead of introducing a new field in Folder.
So, is it possible to bind a List<Map<String, Object>> to the <h:dataTable>?
Is it possible to bind a List of HashMaps to the jsf component h:dataTable?
That's only possible if you generate the necessary <h:column> tags with a view build time tag such as JSTL <c:forEach>.
Here's a concrete kickoff example, assuming that your environment supports EL 2.2:
<h:dataTable value="#{bean.listOfMaps}" var="map">
<c:forEach items="#{bean.listOfMaps[0].keySet().toArray()}" var="key">
<h:column>
#{map[key]}
</h:column>
</c:forEach>
</h:dataTable>
(if your environment doesn't support EL 2.2, you'd need to provide another getter which returns the map key set as a String[] or List<String>; also keep in mind that a HashMap is by nature unordered, you might want to use LinkedHashMap instead to maintain insertion order)
When you're using Mojarra version older than 2.1.18, the disadvantage is that the #{bean} has to be request scoped (not view scoped). Or at least, the <c:forEach items> should refer a request scoped bean. A view scoped bean would otherwise be recreated on every single HTTP request as the <c:forEach> runs during view build time, when the view scope isn't availabe yet. If you absolutely need a view scoped bean for the <h:dataTable>, then you can always create a separate request scoped bean exclusively for <c:forEach items>. The solution would be to upgrade to Mojarra 2.1.18 or newer. For some background information, see also JSTL in JSF2 Facelets... makes sense?
JSF component libraries such as PrimeFaces may offer a <x:columns> tag which makes this more easy, such as <p:dataTable> with <p:columns>.
<p:dataTable value="#{bean.listOfMaps}" var="map">
<p:columns value="#{bean.listOfMaps[0].keySet().toArray()}" var="key">
#{map[key]}
</p:columns>
</p:dataTable>

jsf 2.2 enable disable button with EL and not javascript [duplicate]

I have an inputField, or some other tag , that I want to be disabled unitl user clicks on it.
Something like this, but I cant get it to work in JSF.
$("div").click(function (evt) {
$(this).hide().prev("input[disabled]").prop("disabled", false).focus();
});
I add disabled=true" to my input field, and div value set on < h:form > (all parent tags in this case only one), something like j_idt13 and div of input field, so "div" value looks like j_idt13:inputID
Can someone help me with jQuery solutin?
I wold like to know can it be done in JSF, and how.
You need to toggle it via server side, not via client side. JSF as being a stateful component based MVC framework safeguards this way against tampered/hacked requests wherein the enduser uses client side languages/tools like HTML or JS to manipulate the HTML DOM tree and/or HTTP request parameters in such way that the outcome of JSF's disabled, readonly or even rendered attribute is altered.
Imagine what would happen if the JSF developer checked an user's role in such a boolean attribute against the admin role like so disabled="#{not user.hasRole('ADMIN')}" and a hacker manipulated it in such way that it isn't disabled anymore for non-admin users. That's exactly why you can only change the mentioned attributes (and the required attribute and all converters, validators, event listeners, etc) via the server side.
You can use <f:ajax> in any ClientBehaviorHolder component to achieve the requirement. You can let JSF generate a HTML <div> via <h:panelGroup layout="block">, which is also a ClientBehaviorHolder:
<h:form>
<h:panelGroup layout="block">
Click this div to toggle the input.
<f:ajax event="click" listener="#{bean.toggle}" render="input" />
</h:panelGroup>
<h:inputText id="input" ... disabled="#{not bean.enabled}" />
</h:form>
With this #ViewScoped managed bean (#RequestScoped wouldn't work for reasons mentioned in #5 of commandButton/commandLink/ajax action/listener method not invoked or input value not updated):
#Named
#ViewScoped
public class Bean implements Serializable {
private boolean enabled;
public void toggle() {
enabled = !enabled;
}
public boolean isEnabled() {
return enabled;
}
}
See also:
What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?
Why JSF saves the state of UI components on server?
Unrelated to the concrete problem, head to the following answers in case you're actually interested in how to obtain the HTML representation of JSF components via JS/jQuery:
How to select JSF components using jQuery?
How can I know the id of a JSF component so I can use in Javascript

How do I set Managed Bean field to the value of a ui:param?

I have a JSF page that is included in other JSF pages (basically a page header, with common information). This common page has its own controller and is reliant that the page which includes this common page pass it some common data.
Specifically, I am currently trying to include this common page on other pages using:
<ui:include src="commonPage.xhtml">
<ui:param name="commonData" value="#{thisPagesController.commonData}"/>
</ui:include>
Which should pass "commonData" to the commonPage.xhtml page and ideally set the "commonData" property on the CommonPageController class:
#ManagedProperty("#{commonData}")
CommonData commonData;
However, this is not working... the managed property is not getting set.
What is the proper way to do this?
See comment from BalusC. There is no standard for this in the JSF API spec. Use a composite or custom component instead.

calling methods on collection objects using OGNL or struts2 tags

i have a collection (an arraylist) in action class in which i m storing java mail api's Message class objects.
In jsp i want to access each message from this collection and wants to call msg.getFrom(),msg.getSubject() etc. to dipslay them in tabular form.
how to call methods on collection objects from jsp using struts2 tags or OGNL.
thanks...
Same way as in Java--just call the method. With getters, though, you access them as properties using normal JSP EL or OGNL.
<s:iterator value="msgs" var="msg">
<!-- "#" may not be required depending on Struts 2 version. -->
<s:property value="#msg.from"/>
</s:iterator>
Or:
<c:forEach items="${msgs}" var="msg">
${msg.from}
</c:forEach>
(There are a few other variations as well.)

setPropertyActionListener on commandLink inside ui:repeat with a composite component

I've created a composite component that has a commandLink embedded inside of a ui:repeat. I need to be able to dynamically change the method that is called via the commandLink's action property but it doesn't seem to be possible due to the fact that you need to specify the ID of the commandLink in the
Since the commandLinks are in a UI:repeat, they all have a dynamic ID.
As a workaround, I'm trying to use setPropertyActionListener on the command link. However, it doesn't look like the method is ever being called. Am I missing something? Is this the wrong way to go about what I want?
Here is some sample code.
Composite Component:
<ui:repeat value="#{cc.attributes.value}" var="aUser">
<li class="ui-widget-content ui-state-default q-userListResult">
<p:commandLink
styleClass="q-userList-resultLink"
update=":userList:q-userList-formUsers:userToolTip">
<f:setPropertyActionListener value="{aUser}" target="#{cc.attributes.resultLinkActionListener}"/>
Using Page:
<q:userList id="userList"
value="#{caseWizardBackingBean.companyContacts}"
renderHeader="false"
resultLinkActionListener="#{caseWizardBackingBean.selectedCompanyContact}"/>
Bean:
private CTProfile selectedCompanyContact;
public CTProfile getSelectedCompanyContact() { return this.selectedCompanyContact; }
public void setSelectedCompanyContact(CallTrackProfile ctp) { this.selectedCompanyContact = ctp; }
I tried adding some debug statements and breakpoints into the property's getter and setter but they are never hit. I'm guessing something odd is happening because all of the examples I can find show that this should work (but they don't use a composite component).
I should point out, I'm using the Primefaces commandLink but this seems to happen with the regular commandLink too.
OK, from what I can tell this can't be done in the current version of JSF (2.1.3).
In order to work around the issue I made the component a single commandLink and then the using page has a ui:repeat with the commandLink inside of it.

Resources