JSF2.0复合组件调用自定义数据类型的set方法

时间:2014-03-26 11:04:26

标签: jsf jsf-2

问候语, 我已经创建了一个JSF复合组件,它接受自定义类的对象,并查看其内容,一切顺利,但我需要组件在持有自定义类对象的bean中调用set方法。

示例示例:

1-组件 - componentTest.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:composite="http://java.sun.com/jsf/composite">

    <composite:interface>
        <composite:attribute name="valueHolder" required="true" type="com.max.ValueHolder" />
    </composite:interface>
    <composite:implementation>
        <h:inputText value="#{cc.attrs.valueHolder.value}" />
    </composite:implementation>
</html>

请注意该组件使用自定义类型的属性:

<composite:attribute name="valueHolder" required="true" type="com.max.ValueHolder" />

2-测试页面 - default.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:max="http://java.sun.com/jsf/composite/max" >
     <h:body>       
        <h:form target="@self">      
            <max:componentTest valueHolder="#{user.valueHolder}" /> 
            <h:commandButton value="test" action="#{myBean.action}" />
        </h:form>
    </h:body>

</html>

最后是bean - MyBean.java:

package com.max;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.max.ValueHolder;

@ManagedBean(name="myBean")
@SessionScoped
public class MyBean{
    private ValueHolder valueHolder= new ValueHolder();

    public ValueHolder getValueHolder() {
        return valueHolder;
    }
    public void setValueHolder(ValueHolder valueHolder) {
        this.valueHolder = valueHolder;
    }

    public String action(){
        return "default";
    }
}
  • 我的问题是没有调用方法Mybean.setValueHolder(),因为h:inputText保存ValueHolder中的值而不是ValueHolder本身。

我尝试用

之类的东西设置值
<c:set var="cachedValueHolder" value="${cc.attrs.valueHolder}"/>
<a4j:ajax event="change" >
    <a4j:param value="#{cachedValueHolder}" assignTo="#{cc.attrs.valueHolder}" />
</a4j:ajax>

但它没有用,我不知道为什么!

此致

0 个答案:

没有答案