views:

1259

answers:

2

I want to create a Tag (Source) File to get a custom tag in facelets (as described here). I want it to get used like this:

<my:inputText value="#{myBean.someString}"/>
<my:inputText inputText="#{myBean.inputText}"/>

In the first case, one could bind it to a simple String property within my bean. In the second case myBean should provide a backing bean for the input field. That backing bean contains not only the value, but also properties like maxlength, disabled, mandatory and so on.

The Tag File looks like this (simplified):

<?xml version="1.0" encoding="UTF-8"?>
<ui:fragment
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"&gt;

<c:set var="someValue" value="#{value}"/>

<c:if test="#{empty value and not empty inputText}">
 <c:set var="someValue" value="#{inputText.value}"/>
</c:if>

    <h:inputText value="#{someValue}"
 disabled="#{disabled or inputText.disabled}"/>
</ui:fragment>

This problem is, this is not allowed. When I enter some text in the input field, I get the following error: Illegal Syntax for Set Operation. How can I use "value" or "inputText.value" depending on whether the one or the other is specified?

A: 

Try this:

<my:inputText inputText="#{bean.value}" disabled="#{bean.disabled}"/>

<?xml version="1.0" encoding="UTF-8"?>
<ui:fragment
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"&gt;

<c:set var="someValue" value="#{value}"/>

<c:if test="#{empty value and not empty inputText}">
        <c:set var="someValue" value="#{inputText}"/>
</c:if>

    <h:inputText value="#{someValue}" disabled="#{disabled}"/>
</ui:fragment>
Martlark
I don't see any difference there, only for the "disabled" attribute. Could you explain to me, why this should help?
Tim Büthe
I've not used inputText.value and inputText.disabled both of which won't be available for facelets. (I think!)
Martlark
A: 

Assuming your if tests do what you want them to (I do usually use them. Probably should, seem cleaner than just using the rendered property) this probably would work...

<?xml version="1.0" encoding="UTF-8"?>
<ui:fragment
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"&gt;

<c:if test="#{empty value and not empty inputText}">
   <h:inputText value="#{inputText.someValue}"
                disabled="#{disabled or inputText.disabled}"/>
</c:if>
<c:if test="#{not empty value and empty inputText}">
   <h:inputText value="#{value}" disabled="#{disabled}"/>
</c:if>
</ui:fragment>

Using SomeValue probably makes a new local string rather than maintaining a reference to the string you've given it.

Drew