jsf - 使用 actionListener 时更新 bean 属性
问题描述
我有一个表单中的对话框。该对话框有一个 inputTextarea 和一个 commandButton。commandButton 使用 actionListener 调用 bean 上的方法。我的问题是 inputTextarea 中的数据对我的 actionListener 的方法不可用。下面显示的注释字段在 bean 上为空。如何在我的 bean 方法中访问它的内容?
这页纸:
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"
template="/common/template.xhtml">
<ui:define name="title">test</ui:define>
<ui:define name="head">
<h:outputStylesheet name="web0020.css" library="css"/>
</ui:define>
<ui:define name="content">
<p:panel id="fileUploads" header="File Uploads" style="margin-bottom:20px">
<h:form id="form">
<p:messages id="messages" showDetail="false" closable="true">
<p:autoUpdate/>
</p:messages>
<p:dialog header="Approve" widgetVar="approveDlg" modal="true" appendTo="@(body)">
<p:panelGrid columns="1" layout="grid" styleClass="ui-noborder">
<h:outputText value="Approve Submission" style="font-weight:bold;font-size:1.3em"/>
<p:outputLabel for="comments" value="Comments:" style="font-weight:bold"/>
<p:inputTextarea id="comments" value="#{testView.comments}"
rows="1" cols="100"/>
<p:commandButton value="Save"
actionListener="#{testView.approve()}"
icon="ui-icon-check" update=":form:messages"/>
</p:panelGrid>
</p:dialog>
<p:commandButton value="Approve" onclick="PF('approveDlg').show();" icon="fa fa-thumbs-up"
update=":form:messages"/>
</h:form>
</p:panel>
</ui:define>
</ui:composition>
@Named
@ViewScoped
public class TestView implements Serializable{
@SuppressWarnings("compatibility:1287963775427900593")
private static final long serialVersionUID = 1L;
public TestView() {
super();
}
private String comments;
public void approve() {
try {
System.out.println("Comment:" + comments); //THIS IS EMPTY
} catch (Exception e) {
FacesContext.getCurrentInstance()
.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error! " + e.getMessage(), e.getMessage()));
}
}
public void setComments(String comments) {
this.comments = comments;
}
public String getComments() {
return comments;
}
}
解决方案
对此进行了回答- modal=true 的 Primefaces 对话框无法正常工作。为了制作一个模态表单,你必须使用 appendTo="@(body)" 如果你使用 appendTo,你将在页面的表单之外,所以你必须嵌入一个单独的表单(在主页的表单之外)在你的对话框中
推荐阅读
- python - DataFrame直方图分组并在同一图中
- python - 函数参数中的 Python 类型注释
- c++ - 如何使用索引命名变量以存储输入 mxArray?
- image-processing - 在此示例中,相关性如何对偶数大小的过滤器起作用?
- java - 模式匹配器返回意外值
- excel - VBA如何动态生成选项按钮的名称?
- css - 你如何使文本输入边框重叠?
- elasticsearch - GKE 上的 Elasticsearch - 无法使用 gcs 插件配置快照和还原
- apache-spark - 高效的 Spark JSON 转换
- node.js - 在 Gatsby 构建期间检索文件内容