且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

选择如何使用JSF将参数传递给目标Bean/页面

更新时间:2023-10-06 13:29:34

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

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

<h:outputLink value="www.***.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.***.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?