You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by Alexander Panzhin <ja...@balticum-tv.lt> on 2006/03/01 17:26:49 UTC
Mixing JSTL and JSF(MyFaces)
Hi all,
As I was developing(actually reimplementing) an application I
stumbled on to a need to mix JSTL's c:forEach tag and some logic(c:if)
with ways to make a button to transparently delete an element(Hibernate
mapped object).
I tried adding f:param tag (with id of my object as value) to my
f:commandLink but it would only work if it was inside t.dataList or
f:dataTable, but since I needed some logic (the logic is show link only
if the logged-in user is an admin, maybe I missed some way to do it in
JSF only) I used c:forEach with c:set. I noted that the f:param tag
would reevaluate value each time, so I present you with a version of
f:param with pre evaluation:
package pparam;
import javax.faces.component.UIComponent;
import javax.faces.component.UIParameter;
import javax.faces.el.EvaluationException;
import javax.faces.el.ValueBinding;
import org.apache.myfaces.renderkit.JSFAttr;
import org.apache.myfaces.taglib.UIComponentTagBase;
This IS a CONTRIBUTION.
/**
* PreresovledParam resolves the value when a value binding is set.
* @author Alexander Panzhin
* @version
*/
public class PreresovledParam extends UIComponentTagBase {
public String getComponentType()
{
return "javax.faces.Parameter";
}
public String getRendererType()
{
return null;
}
private String _name;
protected void setProperties(UIComponent component)
{
super.setProperties(component);
String s = JSFAttr.VALUE_ATTR;
ValueBinding vb = component.getValueBinding(JSFAttr.VALUE_ATTR);
if(vb != null){
try {
if (component instanceof UIParameter){
((UIParameter)component).setValue(vb.getValue(this.getFacesContext()));
}
} catch (EvaluationException ex) {
component.setValueBinding(JSFAttr.VALUE_ATTR, null);
throw ex;
}
}
setStringProperty(component, "name", _name);
}
public void setName(String name)
{
_name = name;
}
}
And TLD entry:
<name>preParam</name>
<tag-class>pparam.PreresovledParam</tag-class>
<body-content>empty</body-content>
<!-- UIParameter attributes -->
<attribute>
<name>binding</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<type>java.lang.String</type>
<description>Component binding.</description>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<type>java.lang.String</type>
<description>Name of this parameter</description>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<type>java.lang.String</type>
<description>Initial value or value binding.</description>
</attribute>
Hope that this will be useful to someone.
--
Su pagarba,
Aleksandr Panzin
IT sistemu architektas.
With best regards,
Alexander Panzhin
IT systems architect
Re: Mixing JSTL and JSF(MyFaces)
Posted by Mike Kienenberger <mk...@gmail.com>.
<h:commandLink action="#{ticketAccess.deleteTicket}"
rendered="#{yourBean.adminUser}" .....
<h:outputText value="you are not admin"
rendered="#{not yourBean.adminUser}" .....
On 3/1/06, Alexander Panzhin <ja...@balticum-tv.lt> wrote:
> Mike Kienenberger wrote:
> > To do it in pure JSF:
> >
> > <h:commandLink action="#{ticketAccess.deleteTicket}"
> > rendered="#{yourBean.adminUser}" .....
> >
> > Note that yourBean must be preserved across requests, so if yourBean
> > is request-scoped, you'd need to use t:saveState on it or on its
> > "isAdminUser()" value.
> >
> > On 3/1/06, Alexander Panzhin <ja...@balticum-tv.lt> wrote:
> >
> >> Oh yeah.
> >> Here is the JSP code that puts it all together.
> >>
> >> <h:commandLink action="#{ticketAccess.deleteTicket}"
> >> onclick="if(!confirm('?')){ return;}">
> >> <jmrp:preParam binding="#{ticketAccess.ticketIdParameter}"
> >> name="ticketId" value="#{item.id}"/>
> >> <h:outputText value="X"/>
> >> </h:commandLink>
> >>
> >> --
> >> Su pagarba,
> >> Aleksandr Panzin
> >> IT sistemu architektas.
> >>
> >> With best regards,
> >> Alexander Panzhin
> >> IT systems architect
> >>
> >>
> >>
> >>
> >>
> >
> >
> Thank you.
> I did think that my way is a hack.
> But who knows maybe someone will need a f:param that will resolve the
> value in advance.
> But the thing is now I need to output a text that says that you are not
> admin instad of my "button".
> Any ideas how to do that? This approach will not work.
>
> --
> Su pagarba,
> Aleksandr Panzin
> IT sistemu architektas.
>
> With best regards,
> Alexander Panzhin
> IT systems architect
>
>
>
>
Re: Mixing JSTL and JSF(MyFaces)
Posted by Alexander Panzhin <ja...@balticum-tv.lt>.
Mike Kienenberger wrote:
> To do it in pure JSF:
>
> <h:commandLink action="#{ticketAccess.deleteTicket}"
> rendered="#{yourBean.adminUser}" .....
>
> Note that yourBean must be preserved across requests, so if yourBean
> is request-scoped, you'd need to use t:saveState on it or on its
> "isAdminUser()" value.
>
> On 3/1/06, Alexander Panzhin <ja...@balticum-tv.lt> wrote:
>
>> Oh yeah.
>> Here is the JSP code that puts it all together.
>>
>> <h:commandLink action="#{ticketAccess.deleteTicket}"
>> onclick="if(!confirm('?')){ return;}">
>> <jmrp:preParam binding="#{ticketAccess.ticketIdParameter}"
>> name="ticketId" value="#{item.id}"/>
>> <h:outputText value="X"/>
>> </h:commandLink>
>>
>> --
>> Su pagarba,
>> Aleksandr Panzin
>> IT sistemu architektas.
>>
>> With best regards,
>> Alexander Panzhin
>> IT systems architect
>>
>>
>>
>>
>>
>
>
Thank you.
I did think that my way is a hack.
But who knows maybe someone will need a f:param that will resolve the
value in advance.
But the thing is now I need to output a text that says that you are not
admin instad of my "button".
Any ideas how to do that? This approach will not work.
--
Su pagarba,
Aleksandr Panzin
IT sistemu architektas.
With best regards,
Alexander Panzhin
IT systems architect
Re: Mixing JSTL and JSF(MyFaces)
Posted by Mike Kienenberger <mk...@gmail.com>.
To do it in pure JSF:
<h:commandLink action="#{ticketAccess.deleteTicket}"
rendered="#{yourBean.adminUser}" .....
Note that yourBean must be preserved across requests, so if yourBean
is request-scoped, you'd need to use t:saveState on it or on its
"isAdminUser()" value.
On 3/1/06, Alexander Panzhin <ja...@balticum-tv.lt> wrote:
> Oh yeah.
> Here is the JSP code that puts it all together.
>
> <h:commandLink action="#{ticketAccess.deleteTicket}"
> onclick="if(!confirm('?')){ return;}">
> <jmrp:preParam binding="#{ticketAccess.ticketIdParameter}"
> name="ticketId" value="#{item.id}"/>
> <h:outputText value="X"/>
> </h:commandLink>
>
> --
> Su pagarba,
> Aleksandr Panzin
> IT sistemu architektas.
>
> With best regards,
> Alexander Panzhin
> IT systems architect
>
>
>
>
Re: Mixing JSTL and JSF(MyFaces)
Posted by Alexander Panzhin <ja...@balticum-tv.lt>.
Oh yeah.
Here is the JSP code that puts it all together.
<h:commandLink action="#{ticketAccess.deleteTicket}"
onclick="if(!confirm('?')){ return;}">
<jmrp:preParam binding="#{ticketAccess.ticketIdParameter}"
name="ticketId" value="#{item.id}"/>
<h:outputText value="X"/>
</h:commandLink>
--
Su pagarba,
Aleksandr Panzin
IT sistemu architektas.
With best regards,
Alexander Panzhin
IT systems architect