代码:
搜索.xhtml
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:form id="form">
<h:body>
<h:outputLabel value="Kiosk Id"> </h:outputLabel>
<p:selectOneMenu value="#{abacusMB.kioskId}">
<f:selectItem itemLabel="-Select-" itemValue="null"></f:selectItem>
<f:selectItem itemLabel="1001" itemValue="1001"></f:selectItem>
<f:selectItem itemLabel="1002" itemValue="1002"></f:selectItem>
</p:selectOneMenu>
<br></br>
<h:outputLabel value="Kiosk Location"></h:outputLabel>
<p:selectOneMenu value="#{abacusMB.location}">
<f:selectItem itemLabel="-Select-" itemValue="null"></f:selectItem>
<f:selectItem itemLabel="Nashik" itemValue="Nashik"></f:selectItem>
<f:selectItem itemLabel="Lonavala" itemValue="Lonavala"></f:selectItem>
</p:selectOneMenu>
<br></br>
<center>
<p:commandButton value="Search" type="submit" actionListener="#{abacusMB.searchRecord}" update="seachResult"></p:commandButton>
</center>
<p:dataTable id="seachResult" rendered="#{abacusMB.recordFlag}" lazy="true" value="#{abacusMB.recordList}" var="record">
<p:column headerText="User">
<h:outputText value="#{record.userName}"></h:outputText>
</p:column>
<p:column headerText="KioskId">
<h:outputText value="#{record.kioskId}"></h:outputText>
</p:column>
<p:column headerText="Location">
<h:outputText value="#{record.location}"></h:outputText>
</p:column>
</p:dataTable>
</h:body>
</h:form>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Abacus</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>bluesky</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
RecordVO.java
package com.abacus.search.vo;
import java.io.Serializable;
public class RecordVO implements Serializable{
private static final long serialVersionUID = 11L;
private String location;
private String kioskId;
private String userName;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getKioskId() {
return kioskId;
}
public void setKioskId(String kioskId) {
this.kioskId = kioskId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
算盘MB.java
package com.abacus.search.managedbean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.abacus.search.vo.RecordVO;
public class AbacusMB implements Serializable{
private static final long serialVersionUID = 1L;
private String location;
private String kioskId;
private boolean recordFlag;
private List<RecordVO>recordList=new ArrayList<RecordVO>();
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getKioskId() {
return kioskId;
}
public void setKioskId(String kioskId) {
this.kioskId = kioskId;
}
public boolean isRecordFlag() {
return recordFlag;
}
public void setRecordFlag(boolean recordFlag) {
this.recordFlag = recordFlag;
}
public List<RecordVO> getRecordList() {
return recordList;
}
public void setRecordList(List<RecordVO> recordList) {
this.recordList = recordList;
}
public void searchRecord(){
recordFlag=true;
List<RecordVO> recordVOTempList=new ArrayList<RecordVO>();
if(this.kioskId.equals("1001") && this.location.equals("Nashik")){
System.out.println("User has selected kiosk id as 1001 and location as Nashik ");
System.out.println("We will display two records to Nashik");
for(int i=0;i<2;i++){
if(i==0){
RecordVO recordVO=new RecordVO();
recordVO.setKioskId(kioskId);
recordVO.setLocation(location);
recordVO.setUserName("Sumit");
recordVOTempList.add(recordVO);
}else{
RecordVO recordVO=new RecordVO();
recordVO.setKioskId(kioskId);
recordVO.setLocation(location);
recordVO.setUserName("Aniket");
recordVOTempList.add(recordVO);
}
}
}else{
if(this.kioskId.equals("1002") && this.location.equals("Lonavala")){
System.out.println("User has selected kiosk id as 1002 and location as Lonavala ");
System.out.println("We will display two records to Lonavala");
for(int i=0;i<2;i++){
if(i==0){
RecordVO recordVO=new RecordVO();
recordVO.setKioskId(kioskId);
recordVO.setLocation(location);
recordVO.setUserName("Aarti");
recordVOTempList.add(recordVO);
}else{
RecordVO recordVO=new RecordVO();
recordVO.setKioskId(kioskId);
recordVO.setLocation(location);
recordVO.setUserName("Rachana");
recordVOTempList.add(recordVO);
}
}//close for loop
}//close if loop
}//close else
System.out.println("Record Flag value:"+recordFlag);
System.out.println("Temp List size="+recordVOTempList.size());
this.recordList=recordVOTempList;
}
}//close class
假设用户只输入位置并且没有为 kiosk id 提供任何输入,那么在单击搜索按钮后显示的记录中不应显示 kiosk id 列。
如何实施?
我在想的一个解决方案是我将检查每个输入的值是否为空,并根据我将呈现相应的列。告诉我它是否正确?我的 bean 在视图范围内。