0

I'm currently stuck with a problem which would be solved easily using JSP. Anyhow I got a generated page which has an ID in it. Now JSF has to read this value at runtime (eg. loading of the page), and do a query on a DB with this ID and displaying the results. With JSP this would be a non-brainer but with JSF I do not know how to manipulate the data of the backing bean from the outside. Any ideas?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
methical
  • 132
  • 1
  • 7

2 Answers2

1

It's really beyond me that this ID is hardcoded in the view side, I would rather have made it part of the HTTP request someway (URI, parameter, etc) or just do it in the model, but ala.

You can use <f:viewAction> for this:

<f:metadata>
    <f:viewAction action="#{bean.onload(123)}" />
</f:metadata>
public void onload(Long id) {
    // ...
}

In case you're not on JSF 2.x + EL 3.x yet, then you can use JSTL <c:set> to set a bean property from the view on and you can use its setter method or the <f:view beforePhase> to execute some stuff on render response.

<c:set scope="request" target="#{bean}" property="id" value="123" />
<f:view beforePhase="#{bean.onload}">
    ...
</f:view>

with

public class Bean {
    private String id; // +getter+setter

    public void onload(PhaseEvent event) {
        // Value of id is available here.
        System.out.println(id); // 123
    }

    // ...
}

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks for the hint, I think this will do it. The reason beyond all this is, that the CMS generates static pages. The author can select a DB in the CMS, the ID gets rendered into the page and due to some stupid technology decisions i'll have to stick around with JSF trying to fiddle out how to do this correctly. I wouldn't have used JSF in here but I'm just a henchman. – methical Dec 17 '10 at 10:04
0

hm, do i understand correctly that you want to read out a GET argument from java code?

e.g. the url is http://www.example.com/?foo=bar

then you can read out the value in your java code with this snippet

FacesContext context = FacesContext.getCurrentInstance();
String theValue = context.getExternalContext().getRequestParameterMap().get("foo");
JohnSmith
  • 4,538
  • 9
  • 25
  • 25