1

I wonder what is the common way to use get request, parameters and ajax requests together with JSF?

What I want to achieve is to pass an id parameter to a JSF page, retrieve the entity with the given parameter from the database and display the entity on the JSF page. Then, I want to make some changes to the entity and populate it back to the database (via ajax).

I stuck on the step, where I want to populate the changes back to the database. Here is what I have so far:

I have a very simple JSF page and a controller (ManagedBean).

JSF page

<h:body>
    <h:form id="myForm">
        <p:messages />
        <h:panelGrid columns="3">
            <h:outputLabel value="#{requestController.id}" />
            <p:commandButton value="Update" action="#{requestController.updateEntity}" update="myForm" />
        </h:panelGrid>
    </h:form>
</h:body>

Controller

@Component("requestController")
@Scope("request")
public class RequestController {

@Value("#{request.getParameter('id')}")
private String id;

private String entity;

@PostConstruct
public void init() {
    if(id == null || id.equals("")) {
        entity = "Entity not found";
    }
    else if("1".equals(id)) {
        entity = "FirstEntity";
    }
    else {
        entity = "SecondEntity";
    }
}

public String getEntity() {
    return entity;
}

public void updateEntity() {
    entity += "_updated";
}

public String getId() {
    return id;
}
}

I can open the JSF page with an id parameter and the entity will be displayed correctly. But, when I click on the update button, the Controller will be newly instantiated and the id parameter is gone.

What is the common way to handle request parameters together with ajax requests in JSF?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
flash
  • 6,730
  • 7
  • 46
  • 70
  • in JSF 2 you use ManagedBean and ViewScoped annotations, I don't know why you are using Component and Scope, I guess they are spring annotations.. but that's not jsf – damian Aug 14 '12 at 11:50
  • @Damian you can use `@Component` and `@Scope` when you're using spring to manage your beans. That's totally valid and it is indeed JSF. – flash Aug 14 '12 at 11:53
  • I don't do Spring so I have no idea which role it plays in this specific construct, but in JSF EL you'd reference the request parameter in EL by `#{param.id}`, not by `#{request.getParameter('id')}`. At least, that's how it works when using JSF `@ManagedBean`/`@ManagedProperty` instead of those Spring annotations. – BalusC Aug 14 '12 at 15:05
  • there is no Scope("view") in spring? – damian Aug 15 '12 at 11:45
  • @Damian OOTB there is no view-scope in spring, but you can easily build your own which behaves like view-scope. However, this doesn't solve the problem, because request parameters as injected in sample code, do not work in view scope. – flash Aug 15 '12 at 13:21

1 Answers1

2

Solution is to add the request parameter at every request in the page. This can be done with the <f:param> tag.

So, to make the above code work, you just have to include the initial request parameter to the command button:

<p:commandButton value="Update" action="#{requestController.updateEntity}" update="myForm" >
    <f:param name="id" value="#{requestController.id}" />
</p:commandButton>
Aritz
  • 30,971
  • 16
  • 136
  • 217
flash
  • 6,730
  • 7
  • 46
  • 70