0

i am aware that this has been discussed before, but the example that i created does not work.

bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.*;

import java.io.Serializable;
import java.util.Map;

@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {

private static final long serialVersionUID = 1L;

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String submit() {
    FacesContext fc = FacesContext.getCurrentInstance();
    Map<String,String> params = 
            fc.getExternalContext().getRequestParameterMap();
    System.out.println("params" + params);

        String country = params.get("country");
        System.out.println("country:" + country);

    return "welcome";
}
}

and this is the page:

</h:head>
<h:body>
    <h:outputScript library="js" name="common.js" />
    <!--  <h:outputScript library="js" name="websocket.js" /> -->
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form>
    <h:inputText value="#{helloBean.name}"></h:inputText>
    <h:commandButton value="Welcome Me"  action="#{helloBean.submit}">    </h:commandButton> 
</h:form>



</h:body>
</html>

when i call the page like this: localhost:8080/JavaServerFaces2/hello.xhtml?country=123

and press the button i would expect that the submit method prints the country value in the console. but it prints:

2018-05-13T17:06:48.872+0200|Info: params{j_idt7=j_idt7, j_idt7:j_idt8=,    j_idt7:j_idt9=Welcome Me, javax.faces.ViewState=-   4186994023180961740:1040096002697043710}
2018-05-13T17:06:48.872+0200|Info: country:null

why?

p.s. im eagerly awaiting the new feature to paste code, im sure it will be great.

James Baker
  • 107
  • 2
  • 11
  • 1
    Because you've got different requests, the first one when you GET the page and the other one when you POST it. And you're not keeping the parameter anywhere. – Aritz May 13 '18 at 16:29
  • ah ok, so reading the parameters in submit() is to late. where else would one do that? – James Baker May 13 '18 at 17:21
  • 1
    Possible duplicate of [Retaining GET request query string parameters on JSF form submit](https://stackoverflow.com/questions/17734230/retaining-get-request-query-string-parameters-on-jsf-form-submit) – Aritz May 13 '18 at 17:24

1 Answers1

0

changing the form in the .xhtml to this:

<h:form>
    <h:inputText value="#{helloBean.name}"></h:inputText>
    <h:commandButton value="Welcome Me"  action="#{helloBean.submit}"> 
    <f:param name="country" value="#{param.country}" />
    </h:commandButton>
</h:form>

and the request parameter is preserved from the first page view and it shows up in the console

James Baker
  • 107
  • 2
  • 11
  • is there maybe also a way to act on the parameters when the page is first loaded? using: @PostConstruct public void init(){ x } where x is the code from the submit() above without the return does not work. nothing is printed on the console – James Baker May 13 '18 at 18:19
  • sure just parse requestscoped parameters like in many other Q/A in stackoverflow – Kukeltje May 14 '18 at 06:01