0

I'm trying to send parameter via URL and it fails, because I somewhat can't assign value to the bean or I can't concat value with URL string. By the way when I write color value by hand everything works, for example:

    <h:button outcome="viewParams?c=red"/>

This is the page code from which I'm trying to send parameter.

                <h:form>
                    <h:selectOneMenu value="#{viewMngr.selectedColor}">
                        <f:selectItems value="#{viewMngr.colors}" var="c"
                            itemLabel="#{c}" itemValue="#{c}" />
                        <f:ajax listener="#{viewMngr.valueChanged}" />
                    </h:selectOneMenu>
                    <h:button outcome="viewParams?c=#{viewMngr.selectedColor}"/>
                </h:form>

This is viewManager bean code (as far as I can see bean value is set, because I'm able to print it out)

@ManagedBean
public class ViewMngr {
private ArrayList<String> colors = new ArrayList<String>();

private String selectedColor;

public ViewMngr()
{
    getColors().add("red");
    getColors().add("green");
    getColors().add("blue");
    getColors().add("gray");
    getColors().add("yellow");
    getColors().add("orange");
}


public ArrayList<String> getColors() {
    return colors;
}

public void setColors(ArrayList<String> colors) {
    this.colors = colors;
}

public String getSelectedColor() {
    return selectedColor;
    }

public void setSelectedColor(String selectedColor) {
    System.out.println("Selected color: " + selectedColor);
    this.selectedColor = selectedColor;
}

public void valueChanged() {
    System.out.println("Value changed!");
}
}

Now here is the code from second page which tries to catch that parameter:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core">

            <f:metadata>
                <f:viewParam name="c" value="#{color.color}"/>
            </f:metadata>

            <h:body style="background-color:#{color.color}">
              <fieldset>
                <h:form>
                  <h:outputText value="Some text!"/>
                </h:form>
              </fieldset>
            </h:body>
    </html>

And here is color bean:

@ManagedBean
public class Color {
private String color;

public String getColor() {

    return color;
}

public void setColor(String color) {
    if(color != null)
        System.out.println(color);
    this.color = color;
}
}

2 Answers 2

1

Being your destination page viewParams.xtml:

<h:button outcome="viewParams">
    <f:param name="c" value="#{viewMngr.selectedColor}" />
</h:button>

Takes the view id and appends the params to the url.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your answer. It shows that there is more than one way to solve the same task. Your code works, but you have to add id="buttonId" to that button and render it once color value is changed <f:ajax listener="#{viewMngr.valueChanged}" render="buttonId"/> . If you edit your code I will give you +1
I think you need to rerender your button because you have the param directly wrapped into your outcome (so it is given to client side with the current color concatenation). However, if using my solution, you check the #{viewMngr.selectedColor} when clicking your button, so it gets the color you have in that property at the same moment. Don't need to rerender the button while changing the value. Ah, about the '+1', you should consider accepting the answers clicking on the checkmark (âś“ mark below the vote counter), because I see you have most of your questions still without them ;-)
Actually you need to add id to button and rerender it, because without it c doesn't get set and sending parameter via url fails. It's not about bean value it's about c value.
Check using @ViewScoped below your @ManagedBean annotation in ViewMngr. That way you'll maintain the selection done by the h:selectOneMenu. By the way, Color must not be a @ManagedBean.
ViewMngr scope is right (I tried ViewScope though), however I don't need to use ViewScope since I want to pass value and forget what was in ViewMngr bean so request scope is good here. And I don't see the reason why Color must not be @ManagedBean since I use Color to get param values.
|
0

It turned out that button needed to be rerendered because c value didn't get set. The tricky part was that viewMngr.selectedColor was set but to set parameter c, button needed to be rerendered. Here is the edited code:

    <h:body>
            <fieldset>

                <h:form prependId="false">
                    <h:selectOneMenu value="#{viewMngr.selectedColor}">
                        <f:selectItems value="#{viewMngr.colors}" var="c"
                            itemLabel="#{c}" itemValue="#{c}" />
                    <f:ajax listener="#{viewMngr.valueChanged}" render="buttonId"/>
                    </h:selectOneMenu>
                    <h:button id="buttonId" outcome="viewParams?c=#{viewMngr.selectedColor}"/>

                </h:form>

            </fieldset>
    </h:body>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.