开发者

JSF: Link the URL to a Bean/Java file before displaying .jsp

I am using JSF framework and my requirement is to hit a URL e.g http://localhost/jsfApp/us?param1=name&param2=pass.

Now I want to make a database call and fetch some data based on param1 & param2 even before any .jsp is displayed.

So is there any way to link the URL to any Bean/Java file where I could write the code for DB call, the result of which I will store in a ArrayList in the bean which will eventually be displayed开发者_开发技巧 on the .jsp file.

Thanks, Narain


Make param1 and param2 managed properties so that JSF will set them during bean's construction.

@ManagedProperty(value="#{param.param1}")
private String param1;

@ManagedProperty(value="#{param.param2}")
private String param2;

(or if you're still on JSF 1.x, then you need to declare them as <managed-property> in faces-config.xml file instead)

Then you can do the DB retrieval job in a postconstruct method which will be invoked directly after bean's construction and all managed property setting.

@PostConstruct
public void init() {
    this.result = someDAO.find(param1, param2);
}

(or if you're still on JSF 1.1 or older, then you need to upgrade to at least JSF 1.2 or better JSF 2.x)

Finally just reference the result in the view.

<p>Your result is: #{bean.result}</p>

An alternative to @ManagedProperty in JSF 2.0 is the <f:metadata> in the view.

<f:metadata>
    <f:viewParam name="param1" value="#{bean.param1}" />       
    <f:viewParam name="param2" value="#{bean.param2}" />       
</f:metadata>

This allows for more fine-grained control of validation and conversion of them.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜