Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

ComboBox trong DataGrid với JSF

Bài này sử dụng lại ví dụ chúng ta đã tạo ở phần EJB3: JPA EJB3 – ví dụ mối quan hệ giữa các bảng trong CSDL. Ở đây chúng ta sử dụng mô hình 3-tiers để phát triển ứng dụng: web đóng vai trò presentation, truy xuất đến business logic là session bean, session bean truy xuất đến csdl thông qua entitybean. Sessionbean và entitybean chúng ta đã tạo và đã triển khai. Bây giờ ta có 1 ví dụ sử dụng ComboBox(h:selectOneMenu) và DataTable(h:dataTable). Kết quả sau khi xong bài này ta được trang jsf như hình sau:https://vovanhai.files.wordpress.com/2008/11/comboxingrid_01.png
Chúng ta bắt đầu như sau:

Tạo 1 Dynamic Web Project. Nhớ modify configuration có bao gồm JavaServer faces.
Thêm vào 3 backing bean có nội dung như sau:

PublisherBean.java

package vovanhai.wordpress.jsf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.faces.model.SelectItem;
import javax.naming.InitialContext;
import vovanhai.wordpress.jpa.Publisher;
import vovanhai.wordpress.jpa.PublisherDAO;

public class PublisherBean {
private int pubid;
private String pubname;

private List<SelectItem> publishers=null;

public void setPublishers(List<SelectItem> publishers) {
this.publishers = publishers;
}

public List<SelectItem> getPublishers() {
if (publishers == null) {
publishers = new ArrayList<SelectItem>( );
Collection<Publisher> cols=getAllPublishers();
for(Publisher pub:cols){
publishers.add(new SelectItem(pub.getPubid(),pub.getPubname()));
}
}
return publishers;
}

public PublisherBean() {
super();
}
public PublisherBean(int pubid, String pubname) {
super();
this.pubid = pubid;
this.pubname = pubname;
}
public int getPubid() {
return pubid;
}
public void setPubid(int pubid) {
this.pubid = pubid;
}
public String getPubname() {
return pubname;
}
public void setPubname(String pubname) {
this.pubname = pubname;
}
/**
* Thêm 1 publisher vào DB
* @param pub
*/
public void AddPublisher(Publisher pub){
PublisherDAO dao=getPublisherDAO();
if(dao!=null){
dao.addPublisher(pub);
}
}
/**
* Lấy tất cả publisher
* @return
*/
public Collection<Publisher>getAllPublishers(){
return getPublisherDAO().getAllPublishers();
}
/**
* Lookup DAO
* @return
*/
private PublisherDAO getPublisherDAO(){
PublisherDAO dao=null;
try {
System.setProperty(“java.naming.factory.initial”,”org.jnp.interfaces.NamingContextFactory”);
System.setProperty( “java.naming.provider.url”, “localhost:1099”);
System.setProperty( “java.naming.factory.url.pkgs”, “org.jboss.naming” );
InitialContext context=new InitialContext();
dao=(PublisherDAO)context.lookup(“PublisherDAOBean/remote”);
} catch (Exception e) {
e.printStackTrace();
}
return dao;
}
}

BookTypeBean.java

package vovanhai.wordpress.jsf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.faces.model.SelectItem;
import javax.naming.InitialContext;

import vovanhai.wordpress.jpa.BookTypeDAO;
import vovanhai.wordpress.jpa.Booktype;

public class BookTypeBean {
private int typeid;
private String typename;
private List<SelectItem> bookTypes=null;

public List<SelectItem> getBookTypes() {
if (bookTypes == null) {
bookTypes = new ArrayList<SelectItem>( );
Collection<Booktype> cols=getBookTypeDAO().getAllBookTypes();
for(Booktype type:cols){
bookTypes.add(new SelectItem(type.getTypeid(),type.getTypename()));
}
}
return bookTypes;
}
public void setBookTypes(List<SelectItem> bookTypes) {
this.bookTypes = bookTypes;
}
public int getTypeid() {
return typeid;
}
public void setTypeid(int typeid) {
this.typeid = typeid;
}
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
public BookTypeBean(int typeid, String typename) {
super();
this.typeid = typeid;
this.typename = typename;
}
public BookTypeBean() {
super();
}

private BookTypeDAO getBookTypeDAO(){
BookTypeDAO dao=null;
try {
System.setProperty(“java.naming.factory.initial”,”org.jnp.interfaces.NamingContextFactory”);
System.setProperty( “java.naming.provider.url”, “localhost:1099”);
System.setProperty( “java.naming.factory.url.pkgs”, “org.jboss.naming” );
InitialContext context=new InitialContext();
dao=(BookTypeDAO)context.lookup(“BookTypeDAOBean/remote”);
} catch (Exception e) {
e.printStackTrace();
}
return dao;
}
}

BookBean.java

package vovanhai.wordpress.jsf;

import java.util.Collection;

import javax.naming.InitialContext;

import vovanhai.wordpress.jpa.Book;
import vovanhai.wordpress.jpa.BookDAO;

public class BookBean {
private long isbn;
private String booktiitle;
private String authorname;
private String shortdesc;
private int pubid;
private int typeid;
public BookBean(long isbn, String booktiitle, String authorname,
String shortdesc, int pubID, int typeID) {
super();
this.isbn = isbn;
this.booktiitle = booktiitle;
this.authorname = authorname;
this.shortdesc = shortdesc;
this.pubid = pubID;
this.typeid = typeID;
}
public BookBean() {
super();
}
public long getIsbn() {
return isbn;
}
public void setIsbn(long isbn) {
this.isbn = isbn;
}
public String getBooktiitle() {
return booktiitle;
}
public void setBooktiitle(String booktiitle) {
this.booktiitle = booktiitle;
}
public String getAuthorname() {
return authorname;
}
public void setAuthorname(String authorname) {
this.authorname = authorname;
}
public String getShortdesc() {
return shortdesc;
}
public void setShortdesc(String shortdesc) {
this.shortdesc = shortdesc;
}
public int getPubid() {
return pubid;
}
public void setPubid(int pubID) {
this.pubid = pubID;
}
public int getTypeid() {
return typeid;
}
public void setTypeid(int typeID) {
this.typeid = typeID;
}
//==================================================
private BookDAO getBookDAO(){
BookDAO dao=null;
try {
System.setProperty(“java.naming.factory.initial”,”org.jnp.interfaces.NamingContextFactory”);
System.setProperty( “java.naming.provider.url”, “localhost:1099”);
System.setProperty( “java.naming.factory.url.pkgs”, “org.jboss.naming” );
InitialContext context=new InitialContext();
dao=(BookDAO)context.lookup(“BookDAOBean/remote”);
} catch (Exception e) {
e.printStackTrace();
}
return dao;
}

public Collection<Book>getAllBooks(){
return getBookDAO().getAllBooks();
}
}

Tạo jsf page có tên ComboInDataTable.jsp, nội dung như sau

<%@taglib uri=”http://java.sun.com/jsf/core&#8221; prefix=”f”%><%@taglib
uri=”http://java.sun.com/jsf/html&#8221; prefix=”h”%><%@ page language=”java”
contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Insert title here</title>
</head>
<body>

<f:view>
<h1 align=”center”>Ví dụ Comboxbox trong DataGrid</h1>
<h3 align=”center”>Book listing</h3>
<h:form>
<h:dataTable border=”1″ width=”100%” value=”#{bookBean.allBooks}”
var=”curBook”>
<h:column id=”column1″>
<h:outputText value=”#{curBook.isbn}”></h:outputText>
<f:facet name=”header”>
<h:outputText value=”ISBN”></h:outputText>
</f:facet>
</h:column>
<h:column id=”column2″>
<h:outputText value=”#{curBook.booktiitle}” />
<f:facet name=”header”>
<h:outputText value=”Book title”></h:outputText>
</f:facet>
</h:column>
<h:column id=”column3″>
<h:outputText value=”#{curBook.authorname}” />
<f:facet name=”header”>
<h:outputText value=”Author name”></h:outputText>
</f:facet>
</h:column>
<h:column id=”column4″>
<h:selectOneMenu style=”width: 180px” value=”#{bookBean.typeid}”>
<f:selectItems value=”#{bookTypeBean.bookTypes}” />
</h:selectOneMenu>

<f:facet name=”header”>
<h:outputText value=”Book type”></h:outputText>
</f:facet>
</h:column>
<h:column id=”column5″>
<h:selectOneMenu style=”width: 180px” value=”#{bookBean.pubid}”>
<f:selectItems value=”#{publisherBean.publishers}” />
</h:selectOneMenu>

<f:facet name=”header”>
<h:outputText value=”Publisher”></h:outputText>
</f:facet>
</h:column>
<h:column id=”column6″>
<h:outputText value=”#{curBook.shortdesc}” />
<f:facet name=”header”>
<h:outputText value=”Short Description”></h:outputText>
</f:facet>
</h:column>
</h:dataTable>
</h:form>
</f:view>
</body>
</html>

Chúc thành công!

4 Responses to “ComboBox trong DataGrid với JSF”

  1. kallich said

    ga qua. post bai kieu gi the ha, the con 2 cau import “import vovanhai.wordpress.jpa.Book;”
    va “import vovanhai.wordpress.jpa.BookDAO;” thi sao day.

  2. vovanhai said

    vấn đề này khi bạn làm thì phải tự biết sửa 1 vài lỗi thông thường chứ! chưa gì đã hét toáng lên rùi!

  3. meopro said

    cái đồ vớ vẩn có biết tôn trọng là gì ko?
    thầy có ý tốt post lên cho tham khảo còn nói tào lao ko biết suy nghĩ àh? ko làm thì đừng có chê người biết chia sẻ kiến thức của mình.

  4. anhdinhquan said

    thang kia ko biet mang on con` noi’ nhu nguoi ko hoc IT vay.Mot nguoi di hoc IT it nhat’ fai biet’ Ctrl+C,Ctrl+V bien’ cai nguoi ta thanh` cua minh`.KHONG CO GI KHONG THE

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.