Primefaces实用程序,RequestContext,EL函数,对话框框架和搜索表达框架
Primefaces是领先的JSF库,它提供了大量组件,并促进了多种实用程序和框架。
本教程将重点介绍最有用的实用程序和框架,并说明如何在开发过程中更有效地利用它们。
Primefaces RequestContext
RequestContext是一个简单的实用程序,可以像FacesContext一样获得,可以用于:
向ajax回调函数添加参数。
帮助进行运行时更新。
执行JavaScript。
在开始探索它之前,让我们先看一下RequestContext API。
Method | Description |
---|---|
isAjaxRequest() | Returns a boolean value if current request is a PrimeFaces ajax request. |
addCallBackParam(String name, Object value) | Adds parameters to ajax callbacks like oncomplete. |
update(String clientId); | Specifies component(s) to update at runtime. |
execute(String script) | Executes script after ajax request completes or on page load. |
scrollTo(String clientId) | Scrolls to the component with given clientId after ajax request completes. |
Primefaces RequestContext –回调参数
有时,您会遇到需要从ajax回调中的支持bean中获取一些值的情况。
回调参数被序列化为JSON,并作为ajax回调中的参数提供,以进行此类访问。
index.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> <script> function handleComplete(xhr,status,args){ var isValid = args.isValid; alert("isValid RequestContext Parameter Passed Is :: "+isValid); } </script> </h:head> <h:form> <p:commandButton value="Validate" action="#{utilitiesManagedBean.validate}" oncomplete="handleComplete(xhr,status,args)"></p:commandButton> </h:form> </html>
UtilitiesManagedBean.java
package com.theitroad.prime.faces.beans; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.primefaces.context.RequestContext; @ManagedBean @SessionScoped public class UtilitiesManagedBean { public String validate(){ //Acquire Request Context instance RequestContext context = RequestContext.getCurrentInstance(); //Add isValid parameter context.addCallbackParam("isValid", true); return ""; } }
您可以发送任意多个参数,每个参数都将序列化为JSON格式。
这样,您就有机会发送您想要的任何类型的实例(例如Person)并访问其字段,例如args.person.firstname等。
默认情况下,如果验证周期失败,则隐式添加validateationFailed回调参数。
Primefaces RequestContext-运行时更新
通过使用RequestContext实例,您可以有条件地更新页面的组件。
您无法使用命令的update属性有条件地更新组件。
通过使用RequestContext的更新方法,您现在很高兴可以做到这一点。
index2.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form" prependId="false"> <p:growl id="acceptedMessage"></p:growl> <p:growl id="refusedMessage"></p:growl> <p:commandButton value="Validate" action="#{utilitiesManagedBean.validate}"></p:commandButton> </h:form> </html>
UtilitiesManagedBean.java
package com.theitroad.prime.faces.beans; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import org.primefaces.context.RequestContext; @ManagedBean @SessionScoped public class UtilitiesManagedBean { public String validate(){ //Initial value for valid boolean boolean valid = false; //Do some computation if(Math.floor(Math.random()*10) % 2 == 0){ valid = true; } //Acquire Request Context instance RequestContext context = RequestContext.getCurrentInstance(); //Add isValid parameter context.addCallbackParam("isValid", valid); //If valid equal true, render accepted message if(valid){ //Add message into your FacesContext FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Your request is accepeted ! Congrats")); //Acquire RequestContext singleton and update desired components RequestContext.getCurrentInstance().update("acceptedMessage"); } //else If valid equal false, render refused message if(!valid){ //Add message into your FacesContext FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Your request is refused ! Sorry")); //Acquire RequestContext singleton and update desired components RequestContext.getCurrentInstance().update("refusedMessage"); } return ""; } }
Primefaces RequestContext-执行JavaScript
RequestContext还为您提供了一种在ajax请求完成时执行JavaScript的方法。
与传递回调参数和执行条件JavaScript相比,它更容易。
index3.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form prependId="false"> <p:commandButton value="Execute JavaScript" action="#{utilitiesManagedBean.executeJavaScript}"></p:commandButton> <p:dialog widgetVar="acceptedDialog"> <p:outputLabel value="Accepted"></p:outputLabel> </p:dialog> <p:dialog widgetVar="refusedDialog"> <p:outputLabel value="Refused"></p:outputLabel> </p:dialog> </h:form> </html>
UtilitiesManagedBean.java
package com.theitroad.prime.faces.beans; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import org.primefaces.context.RequestContext; @ManagedBean @SessionScoped public class UtilitiesManagedBean { public String validate(){ //Initial value for valid boolean boolean valid = false; //Do some computation if(Math.floor(Math.random()*10) % 2 == 0){ valid = true; } //Acquire Request Context instance RequestContext context = RequestContext.getCurrentInstance(); //Add isValid parameter context.addCallbackParam("isValid", valid); //If valid equal true, render accepted message if(valid){ //Add message into your FacesContext FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Your request is accepeted ! Congrats")); //Acquire RequestContext singleton and update desired components RequestContext.getCurrentInstance().update("acceptedMessage"); } //else If valid equal false, render refused message if(!valid){ //Add message into your FacesContext FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Your request is refused ! Sorry")); //Acquire RequestContext singleton and update desired components RequestContext.getCurrentInstance().update("refusedMessage"); } return ""; } public String executeJavaScript(){ //Initial value for valid boolean boolean valid = false; //Do some computation if(Math.floor(Math.random()*10) % 2 == 0){ valid = true; } //Hide all of dialogs RequestContext.getCurrentInstance().execute("PF('acceptedDialog').hide()"); RequestContext.getCurrentInstance().execute("PF('refusedDialog').hide()"); //If valid equal true, render accepted message if(valid){ //Acquire RequestContext singleton and execute wanted JavaScript RequestContext.getCurrentInstance().execute("PF('acceptedDialog').show()"); } //else If valid equal false, render refused message if(!valid){ //Acquire RequestContext singleton and execute wanted JavaScript RequestContext.getCurrentInstance().execute("PF('refusedDialog').show()"); } return ""; } }
Primefaces EL功能
Primefaces提供了内置的EL表达式,在很多情况下都非常有用。
在开始使用EL函数之前,请先看一下EL函数的API。
Function | Description |
---|---|
component('id') | Returns clientId of the component with provided server id parameter. This function is useful if you need to work with javascript. |
widgetVar('id') | Provides the widgetVar of a component in PF(";) format. |
Primefaces EL功能–组件和WidgetVar
Primefaces提供了一种使用组件标识符或者WidgetVars轻松定位组件的简便方法。
index4.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form"> <p:outputLabel id="text" value="This component accessed by EL function and it's ID is: #{p:component('text')}"></p:outputLabel> <p:dialog id="dlg"> <p:outputLabel value="Dialog Component"></p:outputLabel> </p:dialog> <p:separator <p:commandButton id="action2" value="Show Dialog" type="button" onclick="#{p:widgetVar('form:dlg')}.show()"></p:commandButton> </h:form> </html>
Primefaces异常处理程序
我们在Ajax行为教程中对异常处理程序进行了详细说明。
Primefaces语言环境
我们在日历组件教程中对Primefaces语言环境进行了详细说明。
Primefaces对话框架
对话框框架(DF)用于在运行时动态生成的对话框中打开外部XHTML页面。
这与我们用来显示对话框的旧样式有点不同。
旧方法需要在XHTML页面内定义p:dialog并利用show/hide来控制对话框(显示/隐藏)。
这次,我们将使用DF API,其中将使用编程API在运行时立即创建和销毁对话框。
下面的简单示例向您显示以编程方式创建和销毁的对话框。
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="https://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <application> <action-listener>org.primefaces.application.DialogActionListener</action-listener> <navigation-handler>org.primefaces.application.DialogNavigationHandler</navigation-handler> <view-handler>org.primefaces.application.DialogViewHandler</view-handler> </application> </faces-config>
dialogInitiator.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form"> <p:commandButton value="Show Dialog" action="#{dialogFrameworkManagedBean.showDialog}"></p:commandButton> </h:form> </html>
externalXHTML.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form"> <p:outputLabel value="This XHTML page will be dispalyed/hidden via using of DF API"></p:outputLabel> </h:form> </html>
DialogFrameworkManagedBean.java
package com.theitroad.prime.faces.beans; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.primefaces.context.RequestContext; @ManagedBean @SessionScoped public class DialogFrameworkManagedBean { public String showDialog(){ RequestContext.getCurrentInstance().openDialog("externalXHTML"); return ""; } }
您可以提供Optional参数,以帮助您对对话框属性进行其他控制。
以下示例显示了如何为可控制的属性提供参数和数字表。
dialogInitiator.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form"> <p:commandButton value="Show Dialog - Basic" action="#{dialogFrameworkManagedBean.showDialog}"></p:commandButton> <p:commandButton value="Show Dialog - Additional Properties" action="#{dialogFrameworkManagedBean.showDialogWithAdditionalArg}"></p:commandButton> </h:form> </html>
externalXHTML.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form"> <p:outputLabel value="This XHTML page will be dispalyed/hidden via using of DF API"></p:outputLabel> </h:form> </html>
DialogFrameworkManagedBean.java
package com.theitroad.prime.faces.beans; import java.util.HashMap; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.primefaces.context.RequestContext; @ManagedBean @SessionScoped public class DialogFrameworkManagedBean { public String showDialog(){ RequestContext.getCurrentInstance().openDialog("externalXHTML"); return ""; } public String showDialogWithAdditionalArg(){ //Fill in properties Map<String,Object> properties = new HashMap<String,Object>(); properties.put("modal", true); properties.put("resizable", true); properties.put("draggable", true); properties.put("width", 400); properties.put("height", 400); RequestContext.getCurrentInstance().openDialog("externalXHTML",properties,null); return ""; } }
在您可能想要提供的所有这些属性下面查找。
Name | Default | Type | Description |
---|---|---|---|
modal | 0 | Boolean | Controls modality of the dialog. |
resizable | 1 | Boolean | When enabled, makes dialog resizable. |
draggable | 1 | Boolean | When enabled, makes dialog draggable. |
width | auto | Integer | Width of the dialog. |
height | auto | Integer | Height of the dialog. |
contentWidth | 640 | Integer | Width of the dialog content. |
contentHeight | auto | Integer | Height of the dialog content. |
closable | true | Boolean | Whether the dialog can be closed or not. |
Primefaces对话框架–数据通信
您可以将数据传回到触发该对话框的父页面中。
dataInitiator.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form"> <p:growl id="growl" showDetail="true" showSummary="true"></p:growl> <p:commandButton value="Show Dialog - Basic" action="#{dialogFrameworkManagedBean.showDialog}"></p:commandButton> <p:commandButton value="Show Dialog - Additional Properties" action="#{dialogFrameworkManagedBean.showDialogWithAdditionalArg}"></p:commandButton> <p:commandButton value="Show Employees Dialog" action="#{dialogFrameworkManagedBean.showEmployeesDialog}"> <p:ajax event="dialogReturn" listener="#{dialogFrameworkManagedBean.onEmployeeChosen}" update="growl" </p:commandButton> </h:form> </html>
employee.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form"> <p:dataTable value="#{dialogFrameworkManagedBean.employees}" var="employee"> <p:column headerText="Employee ID"> <p:outputLabel value="#{employee.employeeId}"></p:outputLabel> </p:column> <p:column headerText="Employee Name"> <p:outputLabel value="#{employee.employeeName}"></p:outputLabel> </p:column> <p:column headerText="Select Employee"> <p:commandButton icon="ui-icon-search" action="#{dialogFrameworkManagedBean.selectEmployeeFromDialog(employee)}" </p:column> </p:dataTable> </h:form> </html>
DialogFrameworkManagedBean.java
package com.theitroad.prime.faces.beans; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import org.primefaces.context.RequestContext; import org.primefaces.event.SelectEvent; @ManagedBean @SessionScoped public class DialogFrameworkManagedBean { private List<Employee> employees = new ArrayList<Employee>(); public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } public String showDialog(){ RequestContext.getCurrentInstance().openDialog("externalXHTML"); return ""; } public String showDialogWithAdditionalArg(){ //Fill in properties Map<String,Object> properties = new HashMap<String,Object>(); properties.put("modal", true); properties.put("resizable", true); properties.put("draggable", true); properties.put("width", 400); properties.put("height", 400); RequestContext.getCurrentInstance().openDialog("externalXHTML",properties,null); return ""; } public String showEmployeesDialog(){ this.employees = new ArrayList<Employee>(); for(int i = 1 ; i < 10 ; i++){ Employee employee = new Employee(); employee.setEmployeeId(i); employee.setEmployeeName("Employee"+i); employee.setEmployeeAge(String.valueOf(21+i)); this.employees.add(employee); } RequestContext.getCurrentInstance().openDialog("employees"); return ""; } public void selectEmployeeFromDialog(Employee employee) { RequestContext.getCurrentInstance().closeDialog(employee); } public void onEmployeeChosen(SelectEvent event){ Employee employee = (Employee) event.getObject(); FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Employee Selected", "Id:" + employee.getEmployeeId()+"\n"+ "Name:"+employee.getEmployeeName()); FacesContext.getCurrentInstance().addMessage(null, message); } public class Employee { private int employeeId; private String employeeName; private String employeeAge; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmployeeAge() { return employeeAge; } public void setEmployeeAge(String employeeAge) { this.employeeAge = employeeAge; } } }
以下是上面列出的代码的详细说明:
包含触发操作的父页面应提供一个dialogReturn ajax事件。
RequestContext.getCurrentInstance()。
closeDialog已用于按常规方式返回的数据。
如果您尝试在不返回数据的情况下使用它,则会出现异常。从jsf 2开始,dataTable组件提供了一种新方法来指定选定的雇员。
即通过使用选择触发器直接发送选择的员工。p:commandButton和p:commandLink都支持dialogReturn事件。
Primefaces对话框框架–对话框消息
众所周知如何使用标准的FacesContext – addMessage方式将消息添加到视图甚至对话框中。
对话框框架提供了一种将消息添加到打开的对话框中的新方法。
以下示例将帮助您以非传统方式显示您的消息。
employees.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form id="form"> <p:dataTable value="#{dialogFrameworkManagedBean.employees}" var="employee"> <p:column headerText="Employee ID"> <p:outputLabel value="#{employee.employeeId}"></p:outputLabel> </p:column> <p:column headerText="Employee Name"> <p:outputLabel value="#{employee.employeeName}"></p:outputLabel> </p:column> <p:column headerText="Select Employee"> <p:commandButton icon="ui-icon-search" action="#{dialogFrameworkManagedBean.selectEmployeeFromDialog(employee)}" </p:column> </p:dataTable> <p:commandButton value="Show Message" action="#{dialogFrameworkManagedBean.showMessage}"></p:commandButton> </h:form> </html>
DialogFrameworkManagedBean.java
package com.theitroad.prime.faces.beans; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import org.primefaces.context.RequestContext; import org.primefaces.event.SelectEvent; @ManagedBean @SessionScoped public class DialogFrameworkManagedBean { private List<Employee> employees = new ArrayList<Employee>(); public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } public String showDialog(){ RequestContext.getCurrentInstance().openDialog("externalXHTML"); return ""; } public String showDialogWithAdditionalArg(){ //Fill in properties Map<String,Object> properties = new HashMap<String,Object>(); properties.put("modal", true); properties.put("resizable", true); properties.put("draggable", true); properties.put("width", 400); properties.put("height", 400); RequestContext.getCurrentInstance().openDialog("externalXHTML",properties,null); return ""; } public String showEmployeesDialog(){ this.employees = new ArrayList<Employee>(); for(int i = 1 ; i < 10 ; i++){ Employee employee = new Employee(); employee.setEmployeeId(i); employee.setEmployeeName("Employee"+i); employee.setEmployeeAge(String.valueOf(21+i)); this.employees.add(employee); } RequestContext.getCurrentInstance().openDialog("employees"); return ""; } public void selectEmployeeFromDialog(Employee employee) { RequestContext.getCurrentInstance().closeDialog(employee); } public void onEmployeeChosen(SelectEvent event){ Employee employee = (Employee) event.getObject(); FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Employee Selected", "Id:" + employee.getEmployeeId()+"\n"+ "Name:"+employee.getEmployeeName()); FacesContext.getCurrentInstance().addMessage(null, message); } public String showMessage(){ RequestContext.getCurrentInstance().showMessageSan Francecolog(new FacesMessage("Message Is Shown")); return ""; } public class Employee { private int employeeId; private String employeeName; private String employeeAge; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmployeeAge() { return employeeAge; } public void setEmployeeAge(String employeeAge) { this.employeeAge = employeeAge; } } }
- 可以使用RequestContext.getCurrentInstance()。
showMessage(" your_msg")代码将新消息添加到打开的对话框中。
Primefaces搜索表达框架
我们介绍了一些使用搜索表达式框架的教程。
但是值得深入了解以了解更多信息。
核心JSF组件引用仅基于具有基本关键字支持的组件标识符。
Primefaces搜索表达框架提供了服务器端和客户端扩展,从而使引用组件变得更加容易。
让我们查看所有可用于引用可能位于视图中的组件的关键字。
Keyword | Type | Description |
---|---|---|
@this | Standard | Current component. |
@all | Standard | Whole view. |
@form | Standard | Closest ancestor form of current component. |
@none | Standard | No component. |
@namingcontainer | PrimeFaces | Closest ancestor naming container of current component. |
@parent | PrimeFaces | Parent of the current component. |
@composite | PrimeFaces | Closest composite component ancestor. |
@child(n) | PrimeFaces | nth child. |
@previous | PrimeFaces | Previous sibling. |
@next | PrimeFaces | Next sibling. |
@widgetVar(name) | PrimeFaces | Component with given widgetVar. |
现在,让我们看一些示例,说明如何利用这些关键字在页面上查找所需组件。
SEF.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form> <p:growl id="message"></p:growl> <p:outputLabel id="output1" value="Enter Username">#{' '}</p:outputLabel><p:inputText id="username" value="#{searchExtensionFramework.username}"></p:inputText> <p:separator <p:outputLabel id="output2" value="Enter Password">#{' '}</p:outputLabel><p:password id="password" value="#{searchExtensionFramework.password}"></p:password> <p:separator <p:commandButton value="Reference Message Component" action="#{searchExtensionFramework.doAction}" process="@this" update="@form:@child(0)"></p:commandButton> </h:form> </html>
SearchExtensionFramework.java
package com.theitroad.prime.faces.beans; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; @ManagedBean @SessionScoped public class SearchExtensionFramework { private String username = ""; private String password = ""; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String doAction(){ FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Growl Component Updated and Referenced By Using SEF","Username: " + this.username + " :: Password: " + password)); return ""; } }
我们先使用@form引用了表单,然后使用@child(0)引用了咆哮组件。
@form:@child(0)被称为复合表达式。
SEF.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form> <p:growl widgetVar="message"></p:growl> <p:outputLabel id="output1" value="Enter Username">#{' '}</p:outputLabel><p:inputText id="username" value="#{searchExtensionFramework.username}"></p:inputText> <p:separator <p:outputLabel id="output2" value="Enter Password">#{' '}</p:outputLabel><p:password id="password" value="#{searchExtensionFramework.password}"></p:password> <p:separator <p:commandButton value="Reference Message Component" action="#{searchExtensionFramework.doAction}" process="@this" update="@widgetVar(message)"></p:commandButton> </h:form> </html>
当单独使用@widgetVar时,您将获得相同的结果。
Primefaces搜索表达框架– Primefaces选择器
Primefaces将jQuery Selector API与JSF组件引用模型集成在一起,因此可以使用jQuery Selector API代替基于代码ID的JSF模型来进行引用。
SEF.xhtml
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:ui="https://java.sun.com/jsf/facelets" xmlns:h="https://java.sun.com/jsf/html" xmlns:f="https://java.sun.com/jsf/core" xmlns:p="https://primefaces.org/ui"> <h:head> <script name="jquery/jquery.js" library="primefaces"></script> </h:head> <h:form> <p:growl></p:growl> <p:outputLabel id="output1" value="Enter Username">#{' '}</p:outputLabel><p:inputText id="username" value="#{searchExtensionFramework.username}"></p:inputText> <p:separator <p:outputLabel id="output2" value="Enter Password">#{' '}</p:outputLabel><p:password id="password" value="#{searchExtensionFramework.password}"></p:password> <p:separator <p:commandButton value="Reference Message Component" action="#{searchExtensionFramework.doAction}" process="@this" update="@(form:first)"></p:commandButton> </h:form> </html>
当您使用JQuery表达式形式时,您将获得相同的结果。
假设您的输入具有.myStyle
样式;通过使用@(。 myStyle)
,您可以指定具有它的组件。如果您在更新或者过程中使用了@(:input),则尝试更新或者处理您拥有的所有输入组件。
您可以在Primefaces和jQuery引用技术之间进行组合。