JSF2复合组件为操作方法抛出PropertyNotFoundException [英] JSF2 composite component throws PropertyNotFoundException for action method

查看:81
本文介绍了JSF2复合组件为操作方法抛出PropertyNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复合组件:

<composite:interface>
    <composite:attribute name="actionMethod" 
        method-signature="java.lang.String action()" required="true" />
</composite:interface>

<composite:implementation>
    <h:form>
        <h:commandButton id="captureButton" value="#{msgs.capture}" 
            action="#{cc.attrs.actionMethod}" />
    </h:form>
</composite:implementation>

和一个正在调用该复合组件的页面:

and a page which is calling that composite component:

<ezcomp:captureTitle actionMethod="#{saveDecisionsBean.captureTitle}" />

和一个包含动作的bean:

and a bean which contains the action:

@Named(value="saveDecisionsBean")
@SessionScoped
public class SaveDecisionsBean extends BackingBeanBase {
    ...
    public String captureTitle() {
        ...
    }
}

现在这是我的问题.当我尝试运行此命令时,它说SaveDecisionsBean没有属性captureTitle.因此,我必须添加一个SaveDecisionsBean#getCaptureTitle()方法.当我这样做时,它运行得很好.为什么我必须定义此方法?在<composite:attribute />中说这是一种方法,并且被用作操作.

Now here is my problem. When I try to run this, it says that SaveDecisionsBean doesn't have a property captureTitle. Therefore, I have to add a SaveDecisionsBean#getCaptureTitle() method. When I do this, it runs just fine. Why should I have to define this method? It says in the <composite:attribute /> that it's a method, and it's used as an action.

这是我收到的确切错误消息:

Here is the exact error message I'm getting:

javax.el.PropertyNotFoundException: /index.xhtml @54,86 
    actionMethod="#{saveDecisionsBean.captureTitle}": 
    The class 'com.example.persistence.SaveDecisionsBean_$$_javassist_209'
    does not have the property 'captureTitle'.

(出于SEO的原因:其他实现可能会显示类名WeldClientProxy.)

(For SEO reasons: other implementations might show class name WeldClientProxy.)

推荐答案

我遇到了同样的问题,我发现这是由于我的操作方法确实抛出了IllegalArgumentException.同时,这已报告为错误:当方法抛出任何异常时,复合操作方法将抛出PropertyNotFoundException

I had the same problem and I found out that it was due to that my action method did throw IllegalArgumentException. Meanwhile this has been reported as a bug: Composite action method throws PropertyNotFoundException when method throws any exception.

棘手的部分(至少对我而言)是,直到我将部分代码移入复合组件(CC)之前,我的应用程序一直运行良好.在我的应用程序捕获IAE并显示一条不错的错误消息之前,但是在使用CC时,JSF验证(或其他...)首先捕获此错误并产生此相当混乱的错误消息.

The tricky part (at least for me) was that my app had been working fine until I moved part of the code into a Composite Component (CC). Before my app would caught the IAE and show a nice error message but when using CC, the JSF validation (or whatever...) catches this first and produce this rather confusing error message.

我通过使用BalusC提供的测试代码的修改版本来验证了这一点(请参见下文).测试页显示两个输入&提交按钮组件.如果您在文本字段中输入了某些内容(除了"panic"(不带引号)),CC版本和"inline"版本均适用(请观看std输出).如果在内联"版本中输入"panic",则会注意到IAE符合预期,但是,如果在上方的"CC-version"中输入相同的内容,则会看到PropertyNotFoundException.似乎JSF被IAE弄糊涂了,并决定该属性毕竟必须是属性而不是操作方法...不确定这是错误还是功能.是按照Spec的规定吗,有人知道吗?

I verified this by using a modified version of the test code provided by BalusC (See below). The test page shows two input & submit button components. If you enter something in the text field (apart from "panic" (without quotes)), both the CC-version and the "inline" version works (watch the std output). If you enter "panic" in the "inlined" version, you'll notice the IAE as expected, but if you enter the same thing in the upper "CC-version" you'll see the PropertyNotFoundException instead. Seems that JSF gets confused by the IAE and decides that the attribute must be a property and not an action method after all... Not sure if this is a bug or a feature. Is this according to Spec, does anybody know?

因此,这里的结论是,不能将CC中的操作方法与抛出异常的bean一起使用.对我来说,这意味着我不能使用复合材料组件.伤心!

So, the conclusion here is that you can't use action methods in CC with beans that throw exceptions. For me, this means that I can't use Composite Components. Sad!

希望这对您有帮助...

Hope this helps...

/resources/components/test.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
  <cc:attribute name="text"/>
  <cc:attribute name="action" method-signature="java.lang.String action()" required="true" />
</cc:interface>
<cc:implementation>
  <h:form>
    <h:inputText value="#{cc.attrs.text}"/>
    <h:commandButton value="submit" action="#{cc.attrs.action}" />
  </h:form>
</cc:implementation>

/test.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cc="http://java.sun.com/jsf/composite/components">
<h:head>
  <title>Test</title>
</h:head>
<h:body>
  <!-- text and cmd-button as cc -->
  <cc:test text="#{bean.text}" action="#{bean.submit}" />

  <hr/>

  <!-- text and cmd-buuton inline -->
  <h:form id="inline">
    <h:inputText value="#{bean.text}"/>
    <h:commandButton value="submit2" action="#{bean.submit}" />
  </h:form>
</h:body>
</html>

最后一个Bean:

@ManagedBean
@RequestScoped
public class Bean {

  private String text;

  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }

  public String submit() {
        if (text.equalsIgnoreCase("panic")){
          throw new IllegalArgumentException("Panic!");
        }

        System.out.println(text);

        return null;
    }
}

这篇关于JSF2复合组件为操作方法抛出PropertyNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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