今天学习了 JSF的Composite Components。
这个Composite Components是一个特别的模板可以做component,其他的都还好理解,就是这个composite:editableValueHolder, Oracle的官方文档写的英文非常的绕口,读了半天也没有完全明白。
| Declares that the composite component whose contract is declared by the |
看了例子,大概理解如下。 首先这个editableValueHolder是用来指向 composite implementation,
<composite:interface>
<composite:attribute name="namePrompt" default="Username: "/>
<composite:attribute name="passwordPrompt" default="Password: "/>
<composite:attribute name="loginButtonText" default="Log In"/>
<composite:attribute name="loginAction"
method-signature="java.lang.String action()"/>
<composite:attribute name="myLoginBean"/>
<composite:editableValueHolder name="vals" targets="form:name"/>
<composite:editableValueHolder name="passwordVal" targets="form:password"/>
</composite:interface>
注意,target是指向implementation的,这里有个form:,我查了很多文档没有提到,我把form:拿掉,就会报错,后来发现是下面implementation是在form里面,如果我把implementation从form里面拿出来,就没有错了。注意这个target接受多个,用逗号分开,但是因为我们需要在using page验证这两个field,合在一起无法分开验证。
下面是implementation
<composite:implementation>
<h:form id="form">
<h:outputStylesheet library="css" name="default.css"
target="head"/>
<h:panelGrid columns="2" role="presentation">
<h:outputLabel for="name"
value="#{cc.attrs.namePrompt}"/>
<h:inputText id="name"
value="#{cc.attrs.myLoginBean.name}"
required="true"/>
<h:outputLabel for="password"
value="#{cc.attrs.passwordPrompt}"/>
<h:inputSecret id="password"
value="#{cc.attrs.myLoginBean.password}"
required="true"/>
</h:panelGrid>
<p>
<h:commandButton id="loginButton"
value="#{cc.attrs.loginButtonText}"
action="#{cc.attrs.loginAction}"/>
</p>
</h:form>
<div class="messagecolor">
<h:messages showSummary="true"
showDetail="false"
errorStyle="color: #d20005"
infoStyle="color: blue"/>
</div>
</composite:implementation>
然后呢, 这个可以在using page里被用到
<ez:LoginPanel myLoginBean="#{myLoginBean}"
loginAction="#{myLoginBean.login}">
<f:validateLength maximum="10" minimum="4" for="vals" />
<f:validateRegex pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,10})"
for="passwordVal"/>
</ez:LoginPanel>
以上就是我今天学习心得。