`

jee6 学习笔记 4 - CRUD 2: View Details, Primefaces row selection

    博客分类:
  • JEE
阅读更多
screenshot of view details button:



screenshot of view details of selected row:


updated backing bean "StudentSearch.java":
1. added property "Student selectedStudent"
2. added getter/setter for it, as selection action listener
package com.jxee.action.student;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.apache.log4j.Logger;
import com.jxee.ejb.student.StudentDAO;
import com.jxee.model.student.Student;

@ManagedBean(name="ss")
@SessionScoped
public class StudentSearch implements Serializable {

  private static final Logger log = Logger.getLogger(StudentSearch.class);
  
  private List<Student> searchResultList;
  @EJB private StudentDAO dao;

  private String nameFilter;
  private int maxRows = 50;
  
  // property for Primefaces single row selection
  private Student selectedStudent;
  

  public String findByName() {
    log.debug("search student by nameFilter: " + nameFilter);
    searchResultList = dao.find(this.nameFilter, maxRows);
    return "studentSearch";
  }

  public String getNameFilter() {
    return nameFilter;
  }

  public void setNameFilter(String afilter) {
    this.nameFilter = afilter;
  }

  public int getMaxRows() {
    return maxRows;
  }

  public void setMaxRows(int maxRows) {
    this.maxRows = maxRows;
  }

  public List<Student> getSearchResultList() {
    return searchResultList;
  }

  public void setSearchResultList(List<Student> searchResultList) {
    this.searchResultList = searchResultList;
  }
  
  public int getSize() {
    return this.searchResultList != null ? this.searchResultList.size() : 0;
  }
  
  public Student getSelectedStudent() {
    return selectedStudent;
  }
  
  //action lister for row selection
  public void setSelectedStudent(Student selectedStudent) {
    log.debug("student selected: " + selectedStudent);
    this.selectedStudent = selectedStudent;
  }

}



updated model Student: missing property "age" before )-;
package com.jxee.model.student;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@Entity
@Table(name="STUDENT")
public class Student implements Serializable {
  
  @Id
  @GeneratedValue
  @Column(name="id")
  private Integer id;
  
  @Column(name="name")
  @NotNull(message="name cannot be null")
  @Size(min=4,max=20,message="name must be 4-20 characters")
  private String name;
  
  @Column(name="mobile")
  @Pattern(regexp="^([0-9])(6,15)$", message="invalid mobile number")
  private String mobile;
  
  @Column(name="age")
  @Min(value=1, message="age cannot be less than 1")
  @Max(value=200, message="age cannot be greater than 200")
  private int age;
  
  @Column(name="created_date")
  // map to "TemporalType.TIMESTAMP" to output time as well
  @Temporal(TemporalType.TIMESTAMP) 
  @NotNull(message="created_date is required")
  @Past(message="created date cannot be in future")
  private Date created_date;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  
  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getMobile() {
    return mobile;
  }

  public void setMobile(String mobile) {
    this.mobile = mobile;
  }
  
  public Date getCreated_date() {
    return created_date;
  }

  public void setCreated_date(Date created_date) {
    this.created_date = created_date;
  }
  
  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append(Student.class.getName()).append(":[")
      .append("id=").append(this.getId())
      .append(";name=").append(this.getName())
      .append(";mobile=").append(this.getMobile())
      .append("age=").append(this.getAge());
    return sb.toString();
  }
}


updated page "student/studentSearch.xhtml"
1. added a <p:commandButton> to enable row selection
2. added a <p:dialog> to enable view student details
<!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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
    
<h:head>
	<title>student search</title>
</h:head>

<h:body>
    
    <h:form id="form">
	    <p:panel header="Student Search Form">
	        <h:panelGrid columns="3">
	            <h:outputLabel value="Name: "/>
	            <h:inputText value="#{ss.nameFilter}"></h:inputText>
	            <h:commandButton type="submit" value="Search" action="#{ss.findByName}"/>
	        </h:panelGrid>
	    </p:panel>
	    
	    <br/>
	
		<p:panel style="border:0px">
			students found: #{ss.size}
		</p:panel>
	
		<!-- search result list -->
	   	<p:dataTable id="stuDataTable" 
	   		var="st" value="#{ss.searchResultList}" 
	   		paginator="true" rows="3">  
	   	
		    <p:column headerText="name" sortBy="#{st.name}">  
		        <h:outputText value="#{st.name}" />  
		    </p:column> 
		    
		    <p:column headerText="mobile" sortBy="#{st.mobile}">  
		        <h:outputText value="#{st.mobile}" />  
		    </p:column>  
		    
		    <p:column headerText="age" sortBy="#{st.age}">  
		        <h:outputText value="#{st.age}" />  
		    </p:column>
		    
		    <p:column headerText="created date" sortBy="#{st.created_date}">  
		        <h:outputText value="#{st.created_date}">
		        	<f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss"/>
		        </h:outputText>  
		    </p:column>
		    
		    <p:column headerText="Action" style="width:4%">  
	        	<p:commandButton id="idSelectButton" 
	        		update=":form:idDisplay" 
	        		oncomplete="wvStuDialog.show()" 
	        		icon="ui-icon-search" 
	        		title="View Details">  
	            	<f:setPropertyActionListener value="#{st}" target="#{ss.selectedSt}" />  
	            </p:commandButton> 
	        </p:column>
		    
		</p:dataTable> 
		
		<!-- view detail dialog -->
		<p:dialog id="idStuDlg" header="Student Details" widgetVar="wvStuDialog" 
			resizable="false" showEffect="fade" hideEffect="slide" modal="true">  
	
	        <h:panelGrid id="idDisplay" columns="2" cellpadding="4" style="margin:0 auto;">  
	            <h:outputText value="id: " />  
	            <h:outputText value="#{ss.selectedSt.id}"/>  
	  
	            <h:outputText value="name: " />  
	            <h:outputText value="#{ss.selectedSt.name}"/>  
	  
	            <h:outputText value="mobile: " />  
	            <h:outputText value="#{ss.selectedSt.mobile}"/>  
	  
	            <h:outputText value="created date: " />  
	            <h:outputText value="#{ss.selectedSt.created_date}"/>  
	        </h:panelGrid>  
	    </p:dialog>  
		
    </h:form>
    
</h:body>
</html>


小姐一下:

1, Primefaces provides a few options of row selection to enable row operations.

2, we used basic single row selection to view details of the selected row.

3, basic row selection can be achieved by <p:commandButton>.

  3.1, update=":form:idDisplay": use attribute "update" to set the dom id of the element to hold row data. the value corresponds to the element id of <h:panelGrid>, which is a sub-element included in <p:dialog>.

  3.2 oncomplete="wvStuDialog.show()": use attibute "oncomplete" to activate the display of the dialog. the value corresponds to attribute "widgetVar" of <p:dialog>.

4, Primefaces makes it pretty easy.

5, it should be noted that the model object "Student" does not implement any extra interfaces at all - it's a simple POJO. So is the backing bean "StudentSearch".
  • 大小: 30.7 KB
  • 大小: 29.8 KB
分享到:
评论
2 楼 jXee 2012-07-23  
lgs0626 写道
求源码,兄弟给共享下吧

"jee6 学习笔记 5 - Struggling with JSF2 binding GET params" has an uploaded zipped project. You can download if you like...have fun
1 楼 lgs0626 2012-07-20  
求源码,兄弟给共享下吧

相关推荐

    开发工具 eclipse-jee-mars-2-win32

    开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32...

    eclipse-jee-mars-1-win32-x86_64.7z

    eclipse-jee-mars-1-win32-x86_64.7z eclipse-jee-mars-1-win32-x86_64.zip 我打的 7z 压缩包 关于有 Alt + / 不起作用解决办法: window -&gt; General -&gt; Keys -&gt; Content Assist -&gt; Binding: 改为 Alt + / When:...

    eclipse-jee-2023-09-R-win32-x86-64.zip

    "eclipse-jee-2023-09-R-win32-x86_64.zip" 是Eclipse针对2023年9月发布的一个更新版,专为Windows 64位操作系统设计。 在Windows系统上安装和使用Eclipse-JEE,你需要了解以下关键知识点: 1. **安装步骤**: - ...

    jee6 学习笔记 5 - Struggling with JSF2 binding GET params

    这篇"jee6 学习笔记 5 - Struggling with JSF2 binding GET params"主要探讨了开发者在使用JSF2绑定GET参数时可能遇到的挑战和解决方案。 JSF2是一个基于MVC(模型-视图-控制器)设计模式的Java框架,用于创建交互...

    eclipse-jee-mars-2-win32

    eclipse-jee-mars-2-win32 javaee开发工具 eclipse-jee-mars-2-win32 javaee开发工具

    eclipse-jee-ganymede-SR2-win32.zip2

    eclipse-jee-ganymede-SR2-win32.zip

    eclipse-jee-2018-09-win32-x86_64.zip

    标题 "eclipse-jee-2018-09-win32-x86_64.zip" 提供的信息表明这是一款针对Java企业级开发的Eclipse集成开发环境(IDE)的2018年9月版本,适用于Windows 32位操作系统、x86_64架构的计算机。Eclipse是一个开源的、跨...

    eclipse-jee-luna-SR2-win32-x86_64网盘下载地址(官网厂货)

    eclipse-jee-luna-SR2-win32-x86_64网盘下载地址(官网厂货); 绿色纯净版,解压缩方可运行(亲测可行)

    jee6 学习笔记 6.3 - @Asynchronous

    在Java企业版(Java EE)6中,`@Asynchronous`注解是一个非常重要的特性,它使得开发者可以方便地在应用程序中实现异步处理。这个注解是Java EE并发编程的一部分,主要应用于EJB(Enterprise JavaBeans)环境,用于...

    eclipse-jee-2022-06-R-win32-x86_64.zip

    在解压eclipse-jee-2022-06-R-win32-x86_64.zip后,我们会得到一个名为“eclipse”的文件夹,这个文件夹包含了整个IDE的所有组件和配置。启动Eclipse IDE,用户会看到熟悉的界面,包括工作区(Workspace)、透视图...

    eclipse-jee-juno-SR2-linux-gtk-x86_64.tar.gz

    标题 "eclipse-jee-juno-SR2-linux-gtk-x86_64.tar.gz" 指示的是一个特定版本的Eclipse集成开发环境(IDE)针对Java企业版(Java Enterprise Edition,简称JEE)的发行包,适用于Linux操作系统,并且是64位(x86_64...

    eclipse-jee-2023-09-R-linux-gtk-x86-64.tar.gz

    "eclipse-jee-2023-09-R-linux-gtk-x86_64.tar.gz" 文件是Eclipse专为Java企业版(Java EE)开发者设计的2023年9月版本,适用于64位的Linux操作系统。这个版本包含了对Java EE开发所需的全部工具和功能,如Web服务器...

    eclipse-jee-2021-12-R-win32-x86_64

    eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64

    eclipse-jee-2020-09-R-win32-x86_64.zip

    2. `eclipse-jee-2020-09-R-win32-x86_64.zip`:这个文件名再次确认了压缩包的内容,即Eclipse IDE的可执行安装程序。通常,用户会解压这个文件,然后运行其中的可执行文件来安装Eclipse。 Eclipse IDE for Java EE...

    eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip

    Eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip是一个针对Windows平台的64位版本的Eclipse集成开发环境(IDE)的压缩包,特别为Java企业级(J2EE)应用程序开发设计。该版本发布于2015年6月30日,是当时Eclipse ...

    eclipse-jee-kepler-SR2-win32 svn插件

    大家下载eclipse-jee-kepler-SR2-win32.zip解压后运行,发现不能直接从SVN中导入项目,下面是我自己想到的方法: 方法:找到自己使用过的带有svn的,低版本的eclipse,找到features和plugins目录,将其中的带有“org...

    eclipse-jee-juno-SR2-win32-x86_64

    eclipse-jee-juno-SR2-win32-x86_64, 百度云盘下载!

    eclipse-jee-2020-12-R-win32-x86_64

    《Eclipse IDE for Java开发者:深入解析eclipse-jee-2020-12-R-win32-x86_64》 Eclipse IDE,全称集成开发环境(Integrated Development Environment),是全球广泛使用的开源Java开发工具。该版本"eclipse-jee-...

    Eclipse64位3.6.2太阳神版eclipse-jee-helios-SR2-win32-x86_64.zip支持jdk1.5

    Eclipse64位3.6.2太阳神版eclipse-jee-helios-SR2-win32-x86_64.zip支持jdk1.5 Eclipse 支持jdk1.5 64位 helios 太阳神版 eclipse-jee-helios-SR2-win32-x86_64.zip 更多eclipse版本可看查看我的系列,欢迎下载~

    eclipse-jee-neon-1a-win_64

    eclipse-jee-neon-1a-win_64

Global site tag (gtag.js) - Google Analytics