如何在JSF 2中获得当前结果

Rus*_*uth 0 jsf-2

我想从导航规则中获得结果值到请求范围的JSF 2 bean.我怎样才能做到这一点?

例如,当我按下a <h:link outcome="contacts">并最终进入联系人页面时,我希望得到"contacts"与导航菜单相关联的支持bean 的结果.

faces-config.xml中

<navigation-rule>
    ...
    <navigation-case>
        <from-outcome>contacts</from-outcome>
        <to-view-id>/pages/contacts.xhtml</to-view-id>
    </navigation-case>
    ...
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)

kol*_*sus 5

在JSF,AFAIK中,只有ConfigurableNavigationHandler会有这些信息.因此,创建一个自定义项ConfigurableNavigationHandler,将结果存储在请求参数中,供您在目标页面中使用.

  1. 自定义导航处理程序

    public class NavigationHandlerTest extends ConfigurableNavigationHandler {
    
    private NavigationHandlerTest concreteHandler;
    
       public NavigationHandlerTest(NavigationHandler concreteHandler) {
        this.concreteHandler = concreteHandler;
       }
    
    
    @Override
       public void handleNavigation(FacesContext context, String fromAction, String    outcome){
        //Grab a hold of the request parameter part and save the outcome in it for
        //later retrieval
         FacesContext context = FacesContext.getCurrentInstance();
         ExternalContext ctx = context.getExternalContext();
         ctx.getRequestMap().put("currentOutcome", outcome);
    
        //resume normal navigation
         concreteHandler.handleNavigation(context, fromAction, outcome);   
        }   
      } 
    
    Run Code Online (Sandbox Code Playgroud)
  2. faces-config.xml中配置处理程序

      <application>
         <navigation-handler>com.foo.bar.NavigationHandlerTest</navigation-handler>
      </application>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 检索目标bean

      @ManagedProperty(value="#{param.currentOutcome}")
      String outcome;
      //getter and setter
    
    Run Code Online (Sandbox Code Playgroud)