在JSF中将p:autoComplete值作为参数发送
在我的页面中有一个罕见的情况,我有一个p:autoComplete,它绑定到一个backingbean,我可以从backingbean中读取自动完成的项。 但问题是,当用户按下按钮时,自动完成中选择的值需要作为参数传递,并带有一些其他参数。我真的不知道怎么做 这是我的网页,有自动完成功能在JSF中将p:autoComplete值作为参数发送,jsf,primefaces,Jsf,Primefaces,在我的页面中有一个罕见的情况,我有一个p:autoComplete,它绑定到一个backingbean,我可以从backingbean中读取自动完成的项。 但问题是,当用户按下按钮时,自动完成中选择的值需要作为参数传递,并带有一些其他参数。我真的不知道怎么做 这是我的网页,有自动完成功能 <p:panel header="Employee sales" style="width:500px" toggleable="true" toggleSpeed="50
<p:panel header="Employee sales" style="width:500px"
toggleable="true" toggleSpeed="500" closeSpeed="500">
<p:autoComplete id="user_auto_complete"
value="#{salesReportMainController.userFromAutoComplete}"
completeMethod="#{salesReportMainController.completeUser}"
var="user" itemLabel="#{user.userName}" itemValue="#{user}"
converter="#{userConverter}" forceSelection="true" />
<p:commandButton id="Search" value="Generate"
action="admin_common_barchart">
<f:param name="todaysDate"
value="#{salesReportMainController.todaysDate}" />
<f:param name="beforDate"
value="#{salesReportMainController.dateBeforOneYear}" />
<f:param name="employeeName"
value="#{salesReportMainController.userFromAutoComplete.userName}" />
</p:commandButton>
</p:panel>
这是绑定到该页面的支持bean
@ViewScoped
public class SalesReportMainController implements Serializable{
private static final long serialVersionUID = 1L;
@ManagedProperty(value = "#{userService}")
public UserService userService;
public DateTime todaysDate;
public DateTime dateBeforOneYear;
public DateTime dateBeforsixMonths;
public List<User> allUsers;
public List<User> acFilterdUsers;
public User userFromAutoComplete;
@PostConstruct
public void init(){
int oneYear = ConstantConfiguration.YearsInMonths.ONE_YEAR.getValue();
int sixMonths = ConstantConfiguration.YearsInMonths.SIX_MONTH.getValue();
todaysDate = new DateTime();
dateBeforOneYear = new DateTime(todaysDate).minusMonths(oneYear);
dateBeforsixMonths = new DateTime(todaysDate).minusMonths(sixMonths);
}
// public String buttonClick(){
// System.out.println("aaaaaaaa");
// return null;
// }
public List<User> completeUser(String query) {
allUsers = userService.getAllUsers();
acFilterdUsers = new ArrayList<User>();
for (User user : allUsers) {
if(user.getUserName().toLowerCase().startsWith(query)){
acFilterdUsers.add(user);
}
}
return acFilterdUsers;
}
public String getAutoCompleteUser() {
if (userFromAutoComplete != null) {
//i can get the value of the selected item form auto complete
}
return null;
}
//getters and setters
}
这是我要加载的页面
<h:form id="common_chart_form" prependId="flase">
<p:growl id="growl" showDetail="true" autoUpdate="true"
sticky="false" />
<p:outputLabel id="labelvalue" value="aaaaaaaaaa"/>
<p:chart id="chart" type="bar"
model="#{commonChartController.barModel}" style="height:600px" />
<p:commandButton value="Print" type="button" icon="ui-icon-print">
<p:printer target="chart" />
</p:commandButton>
<p:commandButton value="Back" action="admin_sales_reports" />
</h:form>
这是上面页面的支持bean
@Component
@ManagedBean
@RequestScoped
public class CommonChartController implements Serializable{
private static final long serialVersionUID = 1L;
@ManagedProperty(value = "#{orderService}")
public OrderService orderService;
@ManagedProperty(value = "#{userService}")
public UserService userService;
List<MonthSales> salesList;
private BarChartModel barModel;
@PostConstruct
public void init() {
String dateTo = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("todaysDate");
String dateFrom = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("beforDate");
String employeeName = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("employeeName");
System.out.println("user Name : "+employeeName);
if(employeeName != null && !employeeName.equals("")){
User user = userService.getUserByUserName("admin");
salesList = orderService.getMonthlySalesByUserName(UserUtility.stringDateToJodaDateTime(dateFrom).toDate(), UserUtility.stringDateToJodaDateTime(dateTo).toDate(), user);
createBarModel(salesList, user);
}else {
salesList = orderService.getMonthlySales(UserUtility.stringDateToJodaDateTime(dateFrom).toDate(), UserUtility.stringDateToJodaDateTime(dateTo).toDate());
createBarModel(salesList);
}
//
// salesList = orderService.getMonthlySales(UserUtility.stringDateToJodaDateTime(dateFrom).toDate(), UserUtility.stringDateToJodaDateTime(dateTo).toDate());
// createBarModel(salesList);
}
}
我可以读取dateTo参数和dateFrom参数。问题是employeeName参数始终为null明显的问题:当您从自动完成列表中选择一个用户时,会填充salesReportMainController.userFromAutoComplete,并且该用户设置了一个用户名?是的,那么我应该如何传递它的值呢?我只是将我的salesReportMainController设置为同样不起作用的会话范围是,我是说你通过调试验证过了吗?对我来说似乎没问题。你误用了p:commandButton。在这里,您需要对目标页面执行一个简单的GET请求,传递一些视图参数。将其替换为p:按钮。你会发现这篇文章很有用。