如何通过Primefaces3.5使用JSF2.0验证并在成功的情况下打开一个新的选项卡
How to validate and if successful open a new tab using JSF 2.0 with Primefaces 3.5?
所以我读了几个解决方案,其中一些解决方案让我很接近,但还不够接近我需要实现的行为。
感觉它应该很简单,也许确实如此,但我对JSF和Primefaces还很陌生,所以这对我来说并不容易
预期行为:单击"运行报告"按钮后,如果成功,请首先验证是否执行了在新选项卡中呈现报告的方法,否则将保持在同一视图中并显示一些错误消息。请参阅下面的代码。随时提出建议,指出错误、不良做法。反馈越多越好。不过,我更喜欢实际的答案,而不是链接。我在SO上遇到了很多问题,但找不到能解决我问题的答案。
我遇到的问题是:
1-我不能覆盖表单的目标属性,所以它在一个新的选项卡中打开。
2-我不能从javascript函数handleComplete
中调用Bean方法runReport
。我得到一个未定义的"click"属性ERROR。
感谢Stack Overflow的天才程序员!!!!!
文件:report_input.xhmtl
<h:form id="pieForm" target="_self">
<p:growl widgetVar="growlMsg" id="msgs" showDetail="true" sticky="true" />
<p:panel header="Report Criteria">
<h:panelGrid columns="5" columnClasses="col-top-style">
<h:outputLabel value="Date Range:" />
<h:outputLabel value="From:" />
<h:outputLabel value="To:" />
<p:calendar id="startDate" value="#{deficiencyBean.startDate}" mindate=""
maxdate="#{deficiencyBean.todayDate}" readOnlyInputText="true" size="18" showButtonPanel="true" popupIconOnly="true" mode="popup" showOn="button" pattern="MM/dd/yyyy" />
<p:calendar id="endDate" value="#{deficiencyBean.endDate}" mindate=""
maxdate="#{deficiencyBean.todayDate}" readOnlyInputText="true" size="18" showButtonPanel="true" popupIconOnly="true" mode="popup" showOn="button" pattern="MM/dd/yyyy" />
</h:panelGrid>
<h:panelGrid id="submitButton" columns="3" columnClasses="col-top-style">
<p:commandButton icon="ui-icon-check" update="msgs" action="#{deficiencyBean.runReport}" oncomplete="handleComplete(xhr, status, args) return false;" value="Run Report" />
<h:commandButton id="realSubmit" actionListener="#{deficiencyBean.runReport}" style="display: none;"/>
<p:commandButton id="validateFunction" actionListener="#{deficiencyBean.validate}" rendered="false" update="msgs"/>
<p:commandButton id="hiddenValidVar" value="#{deficiencyBean.validInput}" oncomplete="handleComplete(xhr, status, args)"/>
</h:panelGrid>
</p:panel>
</h:form>
<script>
function handleComplete(xhr,status,args){
var valid = args.isValid;
alert(valid);
if(valid === 'true'){
document.getElementById('pieForm').setAttribute("target", "_blank"); // NOT WORKING
document.getElementById('pieForm').target="_blank";//NOT WORKING
document.getElementById('pieForm:realSubmit').click();//NOT WORKING
}
}
</script>
</ui:composition>
</html>
文件:ReportBean.java
@ManagedBean(name = "deficiencyBean")
@SessionScoped
@SuppressWarnings("serial")
public class deficiencyBean implements Serializable{
private Date startDate; //public getters n setters
private Date endDate; //public getters n setters
private Boolean validInput; //public getters n setters
public Boolean validate(){
//SOME LOGIC
RequestContext.getCurrentInstance().execute("document.getElementById('pieForm').target='_blank'");//NOT WORKING
if(validInput){
RequestContext.getCurrentInstance().addCallbackParam("isValid", validInput);
}
}
public void runReport(){
//JASPER REPORTS COMPILE, FILL REPORT and DISPLAY
}
}//END OF CLASS
我终于解决了这个问题。
所用方法的说明。单击按钮提交表单后,调用ManagedBean中的验证函数,并使用RequestContext
对象将回调参数(以及验证结果)添加到前端。
然后使用handleComplete(xhr, status, args)
函数检查服务器验证的返回值。
从JavaScript调用隐藏的commandLink以提交报告。如果验证通过,则运行报告并完成!!!!!
下次我会尽量简化我的问题。如果您不清楚,请随时询问有关我的解决方案的问题。
更改如下:
文件:report_input.xhmtl
Beginning of the file ...
<h:panelGrid id="submitButton" columns="3" columnClasses="col-top-adhoc">
<h:outputLabel></h:outputLabel>
<p:commandButton icon="ui-icon-check" action="#{deficiencyBean.validate}"
update="msgs" value="Run Report" oncomplete="handleComplete(xhr, status, args)" />
<h:commandLink action="#{deficiencyBean.runReport}" id="runReportLink" target="_blank" value="Run Report" style="display: none;">
</h:commandLink>
</h:panelGrid>
</p:panel>
</h:form>
<script>
function handleComplete(xhr,status,args){
var valid = args.isValid;
if(valid == true){
document.getElementById('pieForm:runReportLink').click();
}
}
</script>
文件:ReportBean.java
public void validate(){
//SOME LOGIC
RequestContext reqContext = RequestContext.getCurrentInstance();
reqContext.addCallbackParam("isValid", validInput);
}
public void runReport(){
//JASPER REPORTS COMPILE, FILL REPORT and DISPLAY
}
}//END OF CLASS
- 按照选项卡索引的顺序循环一个jQuery选择
- 无法在Ionic select中预先选择最后一个选项
- 一次又一次地在新的和相同的选项卡中打开一个url
- 如果类不是一个选项,如何在使用 jQuery 时控制(避免)嵌套 html 元素的样式
- AngularJS ng选项在选择一个选项后删除了默认的空白值
- 如何为下拉列表的每个选项添加一个属性
- 在下拉列表中选择一个选项的值
- PayPal按钮是否有一个简单的选项,其中金额基于一些用户输入
- 创建了一个选项卡,但点击时不会起作用-jQuery issue/newbie
- 如何通过Primefaces3.5使用JSF2.0验证并在成功的情况下打开一个新的选项卡
- HTML-从mysql生成的选择框中的一个选项更改文本区域的颜色
- 打开一个选项卡,并在firefox网络扩展中向其发出POST请求
- 我如何为列出的选项卡元素编写一个Protractor测试,它会检查它是否's是否处于活动状态
- HTML-使用mysql.php生成一个选择框.我怎样才能更改“;值“;生成的选择框选项的
- 给出<选项>标记一个类?API调用不工作
- 链接到手风琴中的另一个选项卡,嵌入的链接将不起作用
- 如何将数组附加到下拉导航栏并禁用其中的一个选项
- 如何打开一个新窗口或选项卡,并将其提供给javascript执行
- 尝试添加一个选项以使用append进行选择,但没有任何结果
- 多个选择选项一个依赖于另一个