开发者

Read value hardcoded in page with JSF

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 r开发者_运维技巧untime (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?


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:

  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜