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?
Asked
Active
Viewed 477 times
0
-
1What is this ID for? How is this supplied? Hardcoded? Request parameter? – BalusC Dec 15 '10 at 14:08
-
This ID is used to get entries in a DB which are connected to the ID. The ID is hardcoded. – methical Dec 15 '10 at 14:58
-
Can you show how you did it in JSP? we can try in JSF – mvg Dec 16 '10 at 05:35
2 Answers
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