【问题标题】:PrettyFaces and Rewrite libraries - how can we call a bean method before PrettyFaces sets attributes from URL query parameters?PrettyFaces 和 Rewrite 库 - 我们如何在 PrettyFaces 从 URL 查询参数设置属性之前调用 bean 方法?
【发布时间】:2018-10-12 08:16:01
【问题描述】:

我们在 JSF 项目中使用 Prettyfaces 和 Rewrite 库。

有没有办法在 PrettyFaces 从 URL 中的查询参数设置这些属性的值之前重置 bean 属性(将它们设置为 null)? IE。我们可以在 PrettyFaces 设置值之前调用 bean 上的方法吗?

【问题讨论】:

  • 您使用的是哪个范围?使用 Rewrite 您通常应该使用不会出现此问题的请求范围。
  • 我们使用 Session 范围,因此我们需要重置属性。
  • 澄清问题并删除重复内容。

标签: prettyfaces


【解决方案1】:

要做到这一点,我相信最简单的方法是将 Bean 注入到 RewriteConfiguration 对象中。您需要确保优先级被覆盖,以便此提供程序出现在内置 PrettyFaces 功能之前(我相信 -10 应该这样做,但您可能需要使用此值):

public class ExampleConfigurationProvider extends HttpConfigurationProvider
{
   @Inject
   private MySessionBean bean;

   @Override
   public int priority()
   {
      return -10;
   }

   @Override
   public Configuration getConfiguration(final ServletContext context)
   {
      return ConfigurationBuilder.begin()
          .addRule()
          .when(Path.matches("/my-path").and(Direction.isInbound()))
          .perform(new HttpOperation() {
             @Override
             public void performHttp(HttpServletRewrite event, EvaluationContext context)
             {
                bean.clearValues();
             }
          });
   }
}

```

注意,.when() 规则可以包含您想要的任何条件。

可能还有其他方法可以做到这一点,但这是我能想到的最简单的方法。

【讨论】:

  • 谢谢林肯。可以ExampleConfigurationProviderspring bean 和@RewriteConfiguration 在一起吗?我发现扩展 HttpConfigurationProvider 的 Spring bean 将被 Rewrite 自动拾取以进行配置的信息。但这不起作用。
  • 您需要添加github.com/ocpsoft/rewrite/tree/master/integration-spring 模块。如果这不起作用,您可能需要查看 4 个类并为您的 Spring 容器实现一个版本!欢迎 PR!或者,您可以手动访问 ConfigurationProvider 中的 spring 上下文,以使用 Spring API 以编程方式构造或请求所需的 bean。
  • 如果我有ViewScoped Bean 怎么办?当我注入 bean 时,我得到 ContextNotActiveException: WebBeans context with scope type annotation @ViewScoped does not exist within current thread
  • @arash 在创建视图之前,您不能使用ViewScoped bean。这就是 PrettyFaces 将视图范围对象的注入和操作方法推迟到 JSF 生命周期期间的原因。请参阅以下测试以了解如何创建 HttpConfigurationProvider 来执行此操作:github.com/ocpsoft/rewrite/blob/master/integration-faces/src/…
  • @arash 或者,对于更简单的方法:github.com/ocpsoft/rewrite/blob/master/integration-cdi-tests/…
猜你喜欢
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
  • 2019-01-25
  • 2015-05-14
  • 2012-12-24
  • 2012-08-08
  • 2012-04-13
  • 1970-01-01
相关资源
最近更新 更多