Jsf 如何使用复杂EL(如A.B(x).C)来设置值?
Mojara 2.1.21 我正在使用primefaces编辑器组件Jsf 如何使用复杂EL(如A.B(x).C)来设置值?,jsf,jsf-2,primefaces,el,Jsf,Jsf 2,Primefaces,El,Mojara 2.1.21 我正在使用primefaces编辑器组件p:editor。编辑器中的value属性是一个复杂的EL语句 <h:form> <p:datatable value="#{bean.getItems}" var="item"> <p:column> <p:editor value="bean.A(item).value" /> </p:column> </p:datatable
p:editor
。编辑器中的value属性是一个复杂的EL语句
<h:form>
<p:datatable value="#{bean.getItems}" var="item">
<p:column>
<p:editor value="bean.A(item).value" />
</p:column>
</p:datatable>
</h:form>
如果提交表单,则调用getter Entity.getValue(),但不调用setter Entity.serValue(字符串)
我想这与编辑器无关,只是EL的一个共同特征。如果用户将在编辑器中进行某些更改,我如何指示编辑器调用setter
更新
变量
在调用setter时出现问题。但是
是可以的。以下示例可用于测试
<!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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:o="http://omnifaces.org/ui" xmlns:pe="http://primefaces.org/ui/extensions">
<ui:composition>
<h:head></h:head>
<h:body>
<h:form id="formId">
<p:dataTable value="#{multiEditorBacking.editors}" var="editor" rowIndexVar="index" >
<p:column>
<p:commandButton value="Refresh" actionListener="#{multiEditorBacking.onRefresh(index)}" update="textArea"
process="@this" />
<p:editor value="#{multiEditorBacking.eval(editor).text}" id="textArea" />
<!-- <p:editor value="#{editor.text}" id="textArea" /> -->
</p:column>
</p:dataTable>
<p:commandButton value="Save" />
</h:form>
</h:body>
</ui:composition>
</html>
在Mojarra 2.2.5中,使用EL 2.2,这对我来说是可行的。您确定已启用允许方法参数传递的EL版本吗?您需要一个可用的Servlet3.x容器(如Tomcat7),或者您需要自己添加库。但是,似乎您已经将其作为
{multiEditorBacking.eval(editor.text}
对编辑器的值进行了正确评估
顺便说一下,您的
周围的
和
是不必要的。我不喜欢你的代码中的另一件事是使用@SessionScoped
处理纯视图事务。使用@ViewScoped
,除非您明确处理与会话相关的内容
@SessionScoped
@Named
public class MultiEditorBacking implements Serializable {
private List<MultiPojo> editors;
private HashMap<Integer, MultiPojo> hash = new HashMap<Integer, MultiPojo>();
public MultiEditorBacking() {
editors = new ArrayList<MultiPojo>();
MultiPojo m = new MultiPojo();
m.setText("hey1");
editors.add(m);
hash.put(1, m);
m = new MultiPojo();
m.setText("adf2");
editors.add(m);
hash.put(2, m);
m = new MultiPojo();
m.setText("cjd3");
editors.add(m);
hash.put(3, m);
}
public MultiPojo eval(MultiPojo m) {
return m;
}
public void onRefresh(int index) {
System.out.println("Editor " + index + " refreshed");
}
public List<MultiPojo> getEditors() {
return editors;
}
public void setEditors(List<MultiPojo> editors) {
this.editors = editors;
}
public HashMap<Integer, MultiPojo> getHash() {
return hash;
}
public void save() {
System.out.println("Editors: " + editors);
}
public void setHash(HashMap<Integer, MultiPojo> hash) {
this.hash = hash;
}
public class MultiPojo {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return "MultiPojo [text=" + text + "]";
}
}
}
@SessionScoped
@命名
公共类MultiEditorBacking实现可序列化{
私人名单编辑;
私有HashMap hash=newhashmap();
公共MultiEditorBacking(){
editors=newarraylist();
MultiPojo m=新的MultiPojo();
m、 setText(“hey1”);
增加(m);
hash.put(1,m);
m=新的多点jo();
m、 setText(“adf2”);
增加(m);
hash.put(2,m);
m=新的多点jo();
m、 setText(“cjd3”);
增加(m);
hash.put(3,m);
}
公共多任务评估(多任务m){
返回m;
}
公共void onRefresh(int索引){
System.out.println(“编辑器”+索引+“刷新”);
}
公共列表getEditors(){
返回编辑器;
}
公共void集合编辑器(列表编辑器){
this.editors=编辑器;
}
公共HashMap getHash(){
返回散列;
}
公共作废保存(){
System.out.println(“编辑器:”+编辑器);
}
公共void setHash(HashMap hash){
this.hash=hash;
}
公共类多任务{
私有字符串文本;
公共字符串getText(){
返回文本;
}
公共void setText(字符串文本){
this.text=文本;
}
@凌驾
公共字符串toString(){
返回“MultiPojo[text=“+text+”]”;
}
}
}
另请参见:
<p:editor value="#{(multiEditorBacking.eval(editor)).text}"
id="textArea" />
您的实体存储在哪里?如果调用该方法时创建了实体
,您打算如何保留它?请添加有关该问题的额外信息。顺便说一句,您缺少编辑器值的#{}
语法。我用完整的示例更新了我的问题。感谢您的回答和提示。我正在使用JBoss7.1,并且启用了yes参数传递。所以您可以在示例中保存表单?如果我点击“保存”按钮,我会得到一个异常。我不想使用ViewScope,因为我使用的是没有ViewScope的CDI1.0(JSF2.1)(我不想混合使用JSF范围和CDI范围)。您可能会使用更新的EL实现。例如,这与视图或会话范围无关,您可以使用会话对其进行测试,应该可以正常工作。顺便说一句,omnifaces实用程序库有一个视图范围的注释,用于与cdi一起使用。JSF本身从2.2.0版本开始就带来了这个选择。经过一些尝试,我找到了这个问题的解决方案。我必须使用大括号来实现二传将被调用。我写这篇文章是为了回答那些有类似问题的人。这似乎毫无意义。我认为应该是关于JBossEL的实现。无论如何,很高兴看到它有所帮助;)
@SessionScoped
@Named
public class MultiEditorBacking implements Serializable {
private List<MultiPojo> editors;
private HashMap<Integer, MultiPojo> hash = new HashMap<Integer, MultiPojo>();
public MultiEditorBacking() {
editors = new ArrayList<MultiPojo>();
MultiPojo m = new MultiPojo();
m.setText("hey1");
editors.add(m);
hash.put(1, m);
m = new MultiPojo();
m.setText("adf2");
editors.add(m);
hash.put(2, m);
m = new MultiPojo();
m.setText("cjd3");
editors.add(m);
hash.put(3, m);
}
public MultiPojo eval(MultiPojo m) {
return m;
}
public void onRefresh(int index) {
System.out.println("Editor " + index + " refreshed");
}
public List<MultiPojo> getEditors() {
return editors;
}
public void setEditors(List<MultiPojo> editors) {
this.editors = editors;
}
public HashMap<Integer, MultiPojo> getHash() {
return hash;
}
public void save() {
System.out.println("Editors: " + editors);
}
public void setHash(HashMap<Integer, MultiPojo> hash) {
this.hash = hash;
}
public class MultiPojo {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return "MultiPojo [text=" + text + "]";
}
}
}
<p:editor value="#{(multiEditorBacking.eval(editor)).text}"
id="textArea" />