选择如何使用JSF将参数传递给目标Bean/页面 [英] Choosing how to pass parameters to a target bean/page using JSF

查看:66
本文介绍了选择如何使用JSF将参数传递给目标Bean/页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用JSF几年了,但是在决定如何将参数传递到目标页面/bean时,我仍然有疑问.

I have been using JSF for a few years now, but I still have doubts when it comes to deciding how to pass parameters to a target page/bean.

我确实认为这个问题有点复杂,有些人可能会告诉我将其分解为较小的问题.但是,我还认为,以下所有问题的答案都是相关的,并且当您希望JSF执行的所有操作都是:转到该页面并将其作为参数传递"时,它可以解决缺乏直观性的问题.

I do think this question is a bit complex, and that some may tell me to break it down into smaller questions. But, I also think that the answer to all of the questions bellow are related, and that it addresses the lack of intuitiveness when all you want JSF to do is: "Go to that page and pass this as a parameter".

  1. 首先,如何确定转发和重定向?
  2. 之后,如何在h:commandLink/h:commandButtonh:linkh:outputLink之间进行选择?
  3. 然后,结合我上面选择的选项,我应该使用f:param还是f:setPropertyActionListener?两者都将独立于其范围适当地将参数传递给目标Bean吗?
  4. 最后,在目标bean/页面上,什么时候应该使用f:viewParam或以编程方式从请求中恢复参数?
  1. First, how to decide between Forward and Redirect?
  2. After that, how to choose between h:commandLink/h:commandButton, h:link or h:outputLink?
  3. Then, combined with the option I choose above, should I use f:param or f:setPropertyActionListener? Will both properly pass parameters to the target bean, independently of its scope?
  4. Finally, on the target bean/page, when should I use f:viewParam, or recover parameters from the request programmatically?

推荐答案

我将根据我的经验回答您的问题.他们中有些人如此开放,以至于不止一个答案.

I'm going to answer your questions based on my own experience. Some of them are so open that more than one answer could fit.

前进页面即可.从根本上说,页面转发比重定向更快,因为它需要更少的步骤.如果要将视图设置为书签,则需要页面重定向.

A page forward is the way to go unless you explicitly require the browser url to be changed. A page forward is basically faster than a redirection as it requires less steps. A page redirect is required if you want to make your views bookmarkable.

仅在需要 POST 服务器时才使用<h:commandLink />/<h:commandButton /> .稍后,您将能够根据方法返回的内容执行页面前移或重定向.例如:

Use <h:commandLink />/<h:commandButton /> only when you need to POST the server. Later on, you'll be able to perform a page forward or a redirection depending on what the method returns. As an example:

<h:commandLink action="#{bean.processForm}" value="Submit" />

public String processForm(){
    try{
        save();
        return "list";
    }
    catch(Excepcion e){
        addFacesMessage("Error saving");
        //Error saving the object, keep in the same view
        return null;
    }
}

在JSF应用程序中将<h:link outcome="list" value="Go to list" />用于纯页面到页面导航.您可以使用页面转发和重定向.使用<f:param />传递视图参数.

Use <h:link outcome="list" value="Go to list" /> for pure page to page navigation within the JSF application. You can use either page forward and redirect. Use <f:param /> to pass view parameters.

<h:outputLink value="www.stackoverflow.com" />可以用于到其他站点的外部链接(不适用于您的应用程序).使用<f:param />传递视图参数.但是,在这种情况下,我更喜欢将纯HTML与<a href="www.stackoverflow.com" />一起使用.

<h:outputLink value="www.stackoverflow.com" /> could be used for external links to other sites (not into your application). Use <f:param /> to pass view parameters. I however prefer to use plain HTML with <a href="www.stackoverflow.com" /> myself for this case.

关于将参数传递给 POST 请求中的操作方法的方法,您有几种选择. f:setPropertyActionListener在JSF 1.x中非常流行,但是如果您已经是2.x,我建议您使用EL 2.2,它允许方法参数声明.能否使用它取决于您使用的应用程序服务器,但是即使不可用,也可以导入自己.然后,您将可以执行以下操作:

As for passing parameters to action methods in POST requests, you've got several options. f:setPropertyActionListener was so popular in JSF 1.x, but if you're already at 2.x I would recommend you going with EL 2.2, which allows method parameter declaration. Being able to use it depends on the application server you're using, but even if not available you could import yourself. Then, you'll be able to do things like that:

<h:commandButton action="#{bean.saveCar(currentCar)}" value="Save Car" />

尽可能使用它,这将使事情变得更容易.

Use it wherever you can, it'll make things just easier.

对于视图参数,也请使用<f:viewParam />.这是解析GET请求中的参数的标准JSF方法,因此只需让框架为您完成检索工作即可!

For the view parameters, use <f:viewParam /> too. It's the standard JSF way of parsing the parameters from the GET request, so just let the framework do the retrieving work for you!

另请参见:

  • JSF 2 link, commandLink and outputLink example
  • Using EL 2.2 with Tomcat 6.0.24
  • What is the difference between redirect and navigation/forward and when to use what?

这篇关于选择如何使用JSF将参数传递给目标Bean/页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆