How to set the styleclass attribute of a JSF component from bean? - jsf-2

I am using Icefaces 3.2. I would like to know how to reset the styleClass attribute of a component e.g textarea from the backing bean method.
USECASE: I have an ace:textAreaEntry which has a validator method in the backing bean. In this backing bean I am doing some validation. If the validation fails I want a particular CSS class applied to the component. So I want to reset the value of the styleclass attribute.

You can set the styleClass conditionally ,like this
<ace:textAreaEntry
styleClass="#{facesContext.validationFailed?'failedClass':'validClass'}"/>
or assign fail class only and otherwise no class at all
<ace:textAreaEntry
styleClass="#{facesContext.validationFailed?'failedClass':''}"/>
If you manually set message to be displayed in your page you can check if facesContext.messageList is empty or not, like this
<ace:textAreaEntry
styleClass="#{(not empty facesContext.messageList)?'filedClass':'validClass'}"/>
If you want to test for some specific internal logic validation you can check for some boolean for example
<ace:textAreaEntry
styleClass="#{(myBean.someComponentFailed)?'filedClass':'validClass'}"/>
where someComponentFailed is some property that you set to true/false upon validation failure

Related

Difference between 'name' and 'property' argument in ResponseWriter.writeAttribute()

What is the difference between name and property in ResponseWriter.writeAttribute(String name, Object value, String property)? Also, what if property is null?
The name is the HTML attribute name you'd like to write out. The property is the JSF tag attribute name (as indicated by a property on UIComponent class) associated with that HTML attribute.
Usually (and that's for a lot of attributes), those are the same. E.g. <h:inputText value> which ends up as <input value>.
writer.writeAttribute("value", value, "value");
However, for e.g. JSF attribute styleClass this is different, because the associated HTML attribute name class is a reserved keyword in Java and you can't have a property like private String class without facing a compilation error. JSF components therefore use styleClass as tag attribute name and component property. So e.g. <h:inputText styleClass> ends up as <input class>.
writer.writeAttribute("class", styleClass, "styleClass");
The property can be specified with null if there is actually no such attribute in the JSF tag and/or UIComponent class. E.g. custom HTML5 attributes via a custom component or renderer.
writer.writeAttribute("autofocus", "true", null);
So, usually only when both UIComponent#getAttributes() don't contain the key and UIComponent#getValueExpression() returns null and the property is unknown in JSF state.

inputHidden binding to a long id field in backing bean

Just wondering how to set a hidden field value so that when I submit my form, JSF sets it as the id in an object in my CDI-managed bean.
My bean is called "discussionManager" and it has an object in it called 'discussion', which is an entity and therefore has an ID of type Long.
I need the ID so I can look it up and do stuff with it. But, JSF doesn't seem to like numeric hidden fields. It is fine with string fields though. Sure it has something to do with converters or the binding attribute, but can't get the syntax. This is what I'm trying in it's simplest form.
<h:inputHidden id="discussionId" value="#{viewDiscussionBean.discussion.id}"/>
I've tried lots of variations. Can anyone point me in the right direction please?
Thanks
You indeed need to explicitly specify a converter. The JSF builtin LongConverter is suitable.
<h:inputHidden id="discussionId" value="#{viewDiscussionBean.discussion.id}" converter="javax.faces.Long" />

How to persist JSF view parameters through validation

I am using JSF 2.0/CDI and PrimeFaces 2.2.1 for a number of CRUD forms that let the user view or update the attributes of an existing entity by clicking on a link in a datatable, where the identifier of the entity is passed to the CRUD form as a View Parameter. I display the entity's ID (often just an integer) on the CRUD form in a PrimeFaces InputText field with the readonly attribute set to true (since I can't let them change it), so the user knows which entity they're editing. The backing bean of the CRUD form is RequestScoped, which works fine except when validation fails. In that case, the value of the View Parameter is lost, so a 0 is displayed in the entity ID field on validation failure.
I am able to maintain the actual entity ID in a hidden field so it's available to update the database once validation succeeds, but it's rather maddening that I've not been able to find a way to maintain the value in a visible field of some sort after a validation failure. Ideally the InputText field would retain its functionality as an inputted and validated field even with its readonly (or disabled) attribute set to true, which would let me forgo the hidden field entirely. But it doesn't appear that I can make it work that way. Any suggestions besides making the backing bean ConversationScoped, which I'd prefer to avoid?
Actually, after stating what I'm looking for a little differently in a Google search I found a novel suggestion at the link below that seems to work cleanly. Instead of making the entity ID field readonly or disabled, I leave it enabled but blur it as soon as it receives focus. I'm able to get rid of the hidden field, the user can't change the value and it survives a validation failure.
<p:inputText id="entid" value="#{RequestBean.entityID}" onfocus="blur();" />
http://www.codingforums.com/archive/index.php/t-1738.html

grails set bean value from radio button

I'm somewhat new to grails (not groovy though) and I'm working on a sample CRUD application. The issue I'm trying to solve is how to set a property on a bean based on a radio button before I update it in the database. Is the Form Helper http://www.grails.org/plugin/form-helper plugin the way to go? Will the bean have its value set regardless of if the button is actually clicked by the user or if it is left at its default value?
thanks,
Jeff
Have you tried setting the name of the radio buttons to the bean property name and include the radio buttons in your form or remoteForm submission?

struts2: accessing getters of action class in jsp without creating object

I am working on struts2. In my action class I have written some accessors (setter-getter). Now, suppose this action class is returning SUCCESS and in struts.xml I am open a jsp page (say abc.jsp) against the result "SUCCESS". I need the values of all getter methods written in action class without creating object of action class in my jsp (i.e abc.jsp).
If your controller has a getPostalCode property you can do:
<s:textfield name="postalCode"/>
Which will bind the value to the controller field. Outside s: tags you can also use jsp-el; the expression ${postalCode} will do the same. Read this documentation

Resources