How to set Submitted Value

Hi,

I am writing a custom JSF Component. I am extending the UISelectOne Component to have a pre-populated list of values based on the attributes given to the tag. I have 3 files - Tag, Component and Renderer.

It works fine until I submit a value. So, I have 2 pages. On the first page, I put a drop down (my Select One Custom Componet) in a form. Selecting a value and clicking on a button would navigate to the second page where I would display the selected value (or do some processing with it).

The first page displays fine. I use the following tag.

<mycomponent:getValues

id="menu1"

value="#{pc_SampleFile.testbean.selectedItemValue}"

styleClass="selectOneMenu"

param1="val1"

param2="val2"

/>

When I click on the button on my 1st page, it invokes all the seeters and then invokes the setProperties() in my Tag class.

protectedvoid setProperties(UIComponent component)

{

super.setProperties(component);

SelectOneComponent custSelectComponent = (SelectOneComponent)component;

custSelectComponent.getAttributes().put("selectItems", selectItems);

custSelectComponent.getAttributes().put("styleClass", styleClass);

if(value !=null)

{

if(isValueReference(value))

{

custSelectComponent.setValueBinding("value", getFacesContext().getApplication().createValueBinding(value));

}

else

custSelectComponent.setValue(value);

}

}

Then it calles other methods and then the decode() method in the Renderer where I do the following.

publicvoid decode(FacesContext context, UIComponent component)

{

ValueBinding vbVal = selectOneComponent.getValueBinding("value");

String value ="";

if(vbVal !=null)

value = (String)vbVal.getValue(context);

else

value = (String)selectOneComponent.getValue();

((UISelectOne)selectOneComponent).setSubmittedValue(value);

}

I get a JSF message saying

com.sun.faces.context.FacesContextImpl addMessage Adding Message[sourceId=form1:menu1,summary=Validation Error: Value is not valid)

In my 2nd page, I dont see the value.

<h:outputText styleClass="outputText" id="text1" value="#{pc_ShowSelectionSample.testbean1.selectedItemValue}"></h:outputText>

However I do see it if I use the following piece of code.

<%= request.getParameter("form1:menu1")%>

Can someone help me here. How do I make sure that the submitted value gets set correctly.

Thanks for the help.

[3519 byte] By [joemongoa] at [2007-10-2 6:43:15]
# 1

public void decode(FacesContext context,

UIComponent component)

{

ValueBinding vbVal =

vbVal = selectOneComponent.getValueBinding("value");

String value = "";

if(vbVal != null)

value = (String)vbVal.getValue(context);

else

value = (String)selectOneComponent.getValue();

((UISelectOne)selectOneComponent).setSubmittedValue(value);

}

Read the javadoc of decode.

It should decode a value from the request.

However the way to decode depends on how your renderer encodes it in HTML,

a typical code is as follows:FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap.get(clientId);

> I get a JSF message saying com.sun.faces.context.FacesContextImpl addMessage Adding Message[sourceId=form1:menu1,summary=Validation Error: Value is not valid)

This message means the submitted value is not one of options.

See the javadoc of UISelectOne.validateValue.

yuki.yoshidaa at 2007-7-16 13:51:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Yes, I did change the decode method to the following...

public void decode(FacesContext context, UIComponent component)

{

Map requestMap = context.getExternalContext().getRequestParameterMap();

String clientId = component.getClientId(context);

String value = (String) requestMap.get(clientId);

((UISelectOne)component).setSubmittedValue(value);

}

I am still not able to see the submitted value on the second page. Also, I still continue to get the Validation Error.

joemongoa at 2007-7-16 13:51:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
A typical reason of validation errors of UISelectOne is using a request scopedmanaged bean bound to the select items.Use a session scoped bean.
yuki.yoshidaa at 2007-7-16 13:51:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thanks for your reply. I tried that as well, but didn't work. Now I am getting an error which says

com.sun.faces.context.FacesContextImpl addMessage Adding Message[sourceId=form1:menu1,summary=Select one )

I added the ValueBinding in the setProperties() method of the Tag Handler class.

In the decode() method of the renderer class, I retrieved the submitted value and set it using the setSubmitted() method.

I am not sure what else I am missing here. Can someone PLEASE help. All that I am trying to do is create a custom JSF component which extends the UISelectOne component to have a pre-populated list of values in the drop down.

joemongoa at 2007-7-16 13:51:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Seems to be a very simple thing to do... But I am still running into issues. Can someone please help. Just to summarize, All that I am trying to do is create a custom JSF component which extends the UISelectOne component to have a pre-populated list of values in the drop down.

I am able to display the drop down. But when I select an item and click on submit, I don't see the submitted value set on the managed bean.

Some significant methods...

SelectOneComponentTag.java

protected void setProperties(UIComponent component)

{

super.setProperties(component);

selectItems = new HashMap();

selectItems.put("Item1", "Value1");

selectItems.put("Item2", "Value2");

selectItems.put("Item3", "Value3");

selectItems.put("Item4", "Value4");

SelectOneComponent custSelectComponent = (SelectOneComponent)component;

custSelectComponent.getAttributes().put("selectItems", selectItems);

custSelectComponent.getAttributes().put("styleClass", styleClass);

FacesContext context = FacesContext.getCurrentInstance();

if (isValueReference(value))

{

Application app = context.getApplication();

ValueBinding vb = app.createValueBinding(value);

custSelectComponent.setValueBinding("value", vb);

}

}

SelectOneComponentRenderer.java

public void decode(FacesContext context, UIComponent component)

{

Map requestMap = context.getExternalContext().getRequestParameterMap();

String clientId = component.getClientId(context);

String value = (String) requestMap.get(clientId);

((UISelectOne)component).setSubmittedValue(value);

}

Page1.jsp

<mytag:selectOne

id="menu1"

value="#{myBean.selectedItemValue}"

styleClass="selectOneMenu">

</mytag:selectOne>

Page2.jsp

<h:outputText

styleClass="outputText"

id="text1"

value="#{myBean.selectedItemValue}">

</h:outputText>

The message I am getting is as follows

com.sun.faces.context.FacesContextImpl addMessage Adding Message[sourceId=form1:menu1,summary=Validation Error: Value is not valid)

Can someone pleaseeee... help

Thanks

joemongoa at 2007-7-16 13:51:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

> Validation Error: Value is not valid)

As I said before, this message means the submitted value is not one of options.

The validation is done in validateValue() method of UISelectOne

and it searches the child components such as UISelectItem and UISelectItems.

So, I think you should override the method not to search the children

but to search the "selectItems" attributte instead.

yuki.yoshidaa at 2007-7-16 13:51:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Hi Joe,Could you figure your problem out? I am trying to do exactly the same thing but can't set the selected value in my backbean.Thanks.Fargaud
fargauda at 2007-7-16 13:51:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...