根据URL评估jsf bean属性 [英] evaluate jsf bean property based on URL

查看:109
本文介绍了根据URL评估jsf bean属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法根据请求URL显示特定的JSF页面?

Is there a way to display a specific JSF page based on the request URL?

假设我有一个JSF页面详细信息。 XHTML 。托管bean detailsBean 包含一个对象列表,其中每个对象都有自己的ID。现在,如果用户请求页面 ../ details.xhtml?id = 1 ,则应查询列表中ID为1的对象以及生成的详细信息页面应显示此对象。

Let's say I have a JSF page "details.xhtml". The managed bean "detailsBean" has a list of objects where each object has its own ID. Now if a user requests the page "../details.xhtml?id=1", the list should be queried for an object with ID 1 and the resulting details page of this object should be displayed.

我已经编写了一个转换器实现类,它可以从对象转换为ID,反之亦然,但我不知道如何正确使用它。我是否必须通过JAX-RS规范来实现这一点,还是有更简单的解决方案?

I already wrote a converter implementation class which can convert from object to ID and vice versa, but I don't know how to use it properly. Do I have to work through the JAX-RS specification for this to work or is there a more simple solution?

推荐答案

你可以使用以下步骤使用普通JSF实现此目的

You can achieve this with plain JSF with the following steps


  1. 捕获请求中的ID以确定在您的查询中查找的对象来自request参数的 DetailsBean 。有很多方法可以实现这一点,其中一种方法是在托管bean中添加以下注释(目前只允许 @RequestScoped bean,看看为什么这里)。

  1. Capture the ID in the request to determine what object is being queried for in your DetailsBean from the request parameter. There are many ways to achieve this, one of which is adding the following annotation to your managed bean (this is currently only permitted for a @RequestScoped bean, see why here).

   @ManagedProperty(value="#{param.id}")
   int requiredObjectId;

上面的注释将捕获 id 参数从请求中并将其分配给 requiredObjectId 变量。

The annotation above will capture the id parameter from the request and assign it to the requiredObjectId variable.

使用捕获的Id,设置对象在你的bean中的 @PostConstruct 方法

Using the captured Id, setup your object in your bean in a @PostConstruct method

   @PostConstruct
   public void queryForObject(){

   //use the requiredObjectId variable to query and setup the object in the backing bean

   }

检索到的对象应该被指定为托管bean的实例变量

The object retrieved should be assigned as an instance variable of your managed bean

在您的视图中,您可以引用已在辅助bean中设置的查询对象

In your view, you could then reference the queried object that has been setup in the backing bean

  <h:panelGrid columns="2">
     <h:outputText value="Title"/>
     <h:outputText value="#{detailsBean.selectedObject.title}"/>
  </h:panelGrid>


如果您的bean的范围大于请求范围,你需要一组结构来在视图渲染之前干净地拉出请求参数。

If your bean is in a scope broader than the request scope, you'll need a combination of constructs to cleanly pull that request parameter before view rendering.


  1. 捕获请求使用

  1. Capture the request parameter within the JSF view itself using

<f:metadata>
 <f:viewParam name="id" value="#{detailsBean.requiredObjectId}" required="true" requiredMessage="You must provide an Object Id"/>         
</f:metadata>

    **OR**


  • 由于性质单独执行上述操作的JSF生命周期处理可能无法使值及时用于对象设置。您可以改用以下内容。

  • Due to the nature of JSF Lifecycle processing, doing the above alone may not make the value available for your use in time for object setup. You could use the following instead.

     <f:metadata>
        <f:event type="preRenderView" listener="#{detailsBean.setObjectId}" />         
    </f:metadata>
    

    我们在这里做的是指定一种方法(捕获 id必须在呈现视图之前执行的辅助bean中的),确保 id 参数在您需要时可用。只有在上面使用< f:event /> 时才继续执行步骤3.

    What we've done here is specify a method (that captures the id) in the backing bean that must be executed before the view is rendered, ensuring that the id parameter is available as at the time you need it. Proceed with step 3, only if you're using <f:event/> above.

    在辅助bean中,您现在定义 setObjectId 方法

    In the backing bean, you now define the setObjectId method

      public void setObjectId(){
    
      Map<String,String> requestParams =      FacesContext.getExternalContext().getRequestParameterMap();
      requiredObjectId =  Integer.parseInt(requestParams.get("id"));
    
      }
    


  • 请注意,上面的选项通常是一个解决方法/ hack而不是一个干净的解决方案

    Note that the above option is generally a work around/hack and not a clean solution as such

    这篇关于根据URL评估jsf bean属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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