Add custom html attribute to be rendered for jsf2 component - jsf-2

<h:selectBooleanCheckbox />
will render a html checkbox.
How do I add a custom attribute 'myAttribute' with value 6 to it so that the result will be:
<input type="checkbox" data-myAttribute="6" ... />

There is no trivial way to achieve this. Unregistered attribtues are completely ignored. Assuming that you're using Mojarra, your best bet is to extend Mojarra's CheckboxRenderer with a custom one wherein you override the getEndTextToRender() method which writes the extra attribute. To get it to run, just register it in faces-config.xml as a renderer for component family javax.faces.SelectBoolean and renderer type javax.faces.Checkbox.
An alternative is to delegate the job to some onload JavaScript.

Related

Thymeleaf th:field model evaluation

I'm including dynamic content to a view using a custom Thymeleaf attribute processor that simply adds additional nodes while processing the attribute itself.
The code I use is very similar to the one below:
final Template template = arguments.getTemplateRepository().getTemplate(
new TemplateProcessingParameters(arguments.getConfiguration(), "name-of-a-view", arguments.getContext()));
final List<Node> children = template.getDocument().getChildren();
// Add to the tree.
for (final Node node : children) {
element.addChild(node);
}
This works fine, but breaks when the included nodes contains forms that use th:object and th:field.
I put the model I need inside the node variable map and in fact th:object does find and retrieves the object, but th:field does not seems to care and breaks with a
Neither BindingResult nor plain target object for bean name 'model' available as request attribute
From my understanding (step-by-step debugging), it seems to me that th:field only search for the model in the request context.
Am I missing something here?
Thank you in advance.
No, you're spot on. I'm still not sure why the binding is different for th:field than other th: attributes, but it definitely works differently. Essentially, you can't use th:field unless your th:object is on the model. The workaround is to stop using th:field and just specify your input attributes manually, like:
<form action="#" th:action="#{/process}" th:object="${objectFromList}" method="post">
<input type="text" id="fieldName" name="fieldName" th:value="*{fieldName}" />
</form>
I realize this post is old. Hopefully, this will help someone who is running into this quirk.

Any way to bypass form validation on p:commandButton without using o:ignoreValidationFailed or immediate=true

I create page forms with a parameter that tells them where to return when done. The backing bean is #ReqeuestScoped and includes this:
<f:metadata>
<f:viewParam name="return" value="#{createGroup.paramReturn}" />
</f:metadata>
The form has fields with validators, so the cancel button has to skip validation. So, the cancel button looks like this:
<p:commandButton value="Cancel"
action="#{createGroup.doCancel}"
immediate="true"
/>
The purpose of the doCancel() method is to go into Flash scope and return the value set with setParamReturn method.
The problem is with the immediate="true" attribute is that the Flash storage that has the paramReturn variable in it is no longer available. If someone could tell me how to fix that this would be my preferred solution.
Of course without the immediate="true" attribute the action is never invoked because the form cannot pass validation. Another solution would be to cause validation to be ignored some other way.
I have looked at the OmniFaces <o:ignoreValidationFailed> tag and it seems to do what I want, but I don't want to import a whole library for just this tag. However, that is what I will do if there is no other simple way to fix this.

How to create a tooltip over cell in Struts2 display:column?

I have a long text to display in a struts2 display:column. Thus, I truncate since it's not that important. How do I decorate each of those cells with a regular html tooltip (title/alt) when the cursor is positioned over that cell? This way I could still display the complete text to the user on demand.
Could I extend the display:column tag?
<display:column sortable="true" property="codeIdentification" />
Thanks,
Ok, I used a table decorator, which encapsulates the text (codeIdentification) with a span and an title attribute. i.e.
<span title="xxx">codeIdentification</span>
Sufficient.
You may define the decorator in the display:column tag.
<display:table name="dataPage" id="process" export="false"
decorator="com.mysite.decorator.CodeStringFormatter"
requestURI="dataList.action"
sort="list"
pagesize="30"
>
Of course, in this decorator class you need to create a getter method. In my case that is getCodeIdentificatin()
That does the job. Sufficient.

jstl <select> to persist selection

I have a JSP page where am printing the arraylist content via an iterator
<s:iterator var="BeanList" value="BeanList">
<option value='<s:property value="#BeanList.simpleID"/>'>
<s:property value="#BeanList.simpleText" />
</option>
</s:iterator>
Every time the user selects an option, the form submits to the action handling. I want to be able to take the value of the clicked option, and when the page is reloaded after the submit, the same value persists in the select drop down.
Any help will be greatly apprecaited,
You are using HTML Select Tag, with values populated from Struts2 Property Tag.
No JSTL is involved.
But believe me, you can avoid this using the Struts2 Select Tag directly.
Official documentation: http://struts.apache.org/release/2.3.x/docs/select.html
In Action
#Getter #Setter List<String> allCities;
#Getter #Setter String selectedCity;
In JSP:
<s:select list="allCities"
name="selectedCity" />
Faster and cleaner than iterating manually :)
Eventually you can add an optional header value:
<s:select list="allCities"
name="selectedCity"
headerKey="-1"
headerValue="Select a City" />
For that you need to declare a variable with select box name in Action class, and put setter and getter for that. And then, when you are submitting the form, the name matches and it automatically populate in Action class.
When retrieving the data set value to same variable.Then it will populate automatically by using name.
This will happen by using params interceptor internally.

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

Resources