Conditional output of text box or label via custom JSF component?

Hi

I'm looking to output a text box or a label on a web page depending on a user's ability to modify data.

For example, my initial thought is that if a user is considered read-only within a system, I want to evaluate this within a custom JSF component and output a label containing data. If the user has "write" access, I want to output an input text box to enable the user to modify the same data.

Is this possible? So far, I've tried dynamically modifying the component type but with no great success. I was just looking at doing the same thing with the renderer type as well.

P.S. I can do this using conditional statements within JSP but I'd like to encapsulate these conditions within a custom tag to reduce the need to write lots of if...else statements in the page.

Any help much appreciated...thanks

Message was edited by:

dfz

[888 byte] By [dfza] at [2007-11-27 11:11:00]
# 1

<h:outputText value="#{myBean.value}" rendered="#{!myBean.edit}" />

<h:inputText value="#{myBean.value}" rendered="#{myBean.edit}" />

BalusCa at 2007-7-29 13:45:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for replying BalusC...I was hoping to get this into a single tag though.

I've just started playing around with the 'readonly' attribute as well. I'm assuming I could use this with a standard h:outputText or h:selectManyMenu and set the readonly attribute if the user is read-only?

dfza at 2007-7-29 13:45:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You can. It's your design choice. There is also another useful attribute: 'disabled'.

You can use those attributes in UIInput elements only, not in h:outputText.

BalusCa at 2007-7-29 13:45:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thanks for your help - I'll check these out.

dfza at 2007-7-29 13:45:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I've just realised that I can't use the 'disabled' attribute because the application must be DDA compliant.

This means that I need to render the h:selectOneMenu as an HTML label if the user is read-only (partly because the 'readonly' attr doesn't work for HTML 'select' elements). Back to the drawing board :(

dfza at 2007-7-29 13:45:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

If you use the Tomahawk versions of inputTest, etc. they have a displayValueOnly attribute which does exactly what you need.

RaymondDeCampoa at 2007-7-29 13:45:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Thanks for the info...

I've just ended up creating my own tag library containing tag classes which dynamically modify their component type according to the validity contained within a managed bean e.g.

<lib:inputText value="#{backingBean.theValue}" valid="#{permissionBean.isAllowed(code)}" />

...and then modify the component type in the tag class mapped to lib:inputText according to validity :

public String getComponentType() {

if ( valid.equals( "true" ) )

return "inputText";

else

return "label";

}

'inputText' maps to a UI component which writes out HTML for an input text box whereas label writes out text (using the lib:inputText 'value' attribute). Seems to work OK so far.

dfza at 2007-7-29 13:45:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Hi,

I'm looking for something similar for my project.

Can you please guide me , where I can get information regarding custom tag that can be used both for Input (Input text) and Output (label)..

Most of the examples online are for custom tag which extends either UIInput or UIOutput

Regards

Amit

Amit.Kotharia at 2007-7-29 13:45:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

I would suggest using the Tomahawk components instead of implementing your own.

RaymondDeCampoa at 2007-7-29 13:45:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...