刚进项目组,就开始学习JSF------JavaServer Faces,关于JSF,在理论方面懂的也不多,让我们从实践环节开始:
注意:我的J2EE配置是JDK 1.5 + Eclipes 3.2 + MyEclipes 5.5 ,其中内嵌了对JSF的支持。
第一步:打开MyEclipse,选择File--->New--->Project--->WebProject,单击Next,输入项目名称jsfDemo,确定;
第二步:右键选中刚才新建的项目jsfDemo,在下拉菜单中选择MyEclipse,在右边弹出的菜单中选择“Add JSF Facelets Ability”,此时,系统会为我们在项目中自动加入JSF RI 1.1.01 Libraies包,并在WEB-INF中自动添加faces-config.xml配置文件和html_basic.tld和jsf_core.tld模式文件。
第三步:在项目的src文件夹中单击右键,添加Package ,然后在此包中添加Class,或者直接添加Class,此时系统会提示我们必须为此类指定包名。我们在src中添加了一个包com.kumon.test,并在此包中添加了三个后台代码文件:Book.java,BookList.java,SimulateDB.java,其中SimulateDB是模拟的用来连接底层数据库的一个类。
其中Book.java的源代码如下:
package com.kumon.jsfdemo;
import java.util.Map;
import javax.faces.event.ActionEvent;
import javax.faces.component.UIParameter;
import javax.faces.context.*;
public class Book {
private long id;
private String author;
private String title;
private boolean available;
/*
* default Constructor
*/
public Book() {}
/*
* New Constructor with Parameters
*/
public Book(long id, String author, String title, boolean available) {
this.id = id;
this.author = author;
this.title = title;
this.available = available;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
/*
* The set method of Class
*/
public void setBook(Book book){
this.setId(book.getId());
this.setAuthor(book.getAuthor());
this.setTitle(book.getTitle());
this.setAvailable(book.isAvailable());
}
/*
* The get method of Class
*/
public Book getBook(){
return new Book(this.getId(),this.getAuthor(),this.getTitle(),this.isAvailable());
}
/*
* The event of iniitiasize a book
*/
public void initBook(ActionEvent event){
this.setBook(new Book());
}
/*
* The event of find a book by id
*/
public void selectBook(ActionEvent event){
SimulateDB simulateDB = new SimulateDB();
Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
UIParameter component = (UIParameter)event.getComponent().findComponent("editId");
long id = Long.parseLong(component.getValue().toString());
this.setBook(simulateDB.loadBookById(id, session));
}
/*
* The event of save a new book to Collection and return the id of this new book
*/
public void saveBook(ActionEvent event){
SimulateDB simulateDB = new SimulateDB();
Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
simulateDB.saveToDB(this.getBook(), session);
}
/*
* The event of delete a book by the id of this book
*/
public void deleteBook(ActionEvent event){
SimulateDB simulateDB = new SimulateDB();
Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
UIParameter component = (UIParameter)event.getComponent().findComponent("deleteId");
long id = Long.parseLong(component.getValue().toString());
simulateDB.deleteBookById(id, session);
}
}
BookList.java的源代码如下:
package com.kumon.jsfdemo;
import java.util.Collection;
import java.util.Map;
import javax.faces.context.*;
public class BookList {
Collection books;
/*
* Get all book from the Collection
*/
public Collection getBooks(){
SimulateDB simulateDB = new SimulateDB();
Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
books = simulateDB.getAllBooks(session);
return books;
}
/*
* Set the books of the Collection
*/
public void setBooks(Collection books){
this.books = books;
}
}
最后SimulateDB.java的源代码如下:
(阅读次数:)
共3页: 上一页 1 [2] [3] 下一页