在javaserver faces里做弹出对话框并不是一件容易的事情,尤其是在弹出对话框里做修改记录的操作,感觉还是蛮麻烦的. 这里主要用到了两个javascript小技巧,window.open("","popup","height=480,width=320,toolbar=no,menubar=no,scrollbars=no");打开一个空白网页的窗口,然后动态设置修改按钮提交时的target属性.另一个小技巧是 setInterval('opener.window.location.href=opener.window.location.href;window.close();',20);在编辑页面提交表单时做这个
显示数据的文件show.jsp
pressed="fail";
function setPress(newValue){
pressed=newValue;
}
function confirmSubmit(){
if(pressed="editor"){
popup = window.open("","popup","height=480,width=320,toolbar=no,menubar=no,scrollbars=no");
popup.openerFormId=document.forms["fors"].id;
popup.focus();
document.forms["fors"].target="popup";
}
}
function SetCheckedStatus()
{
var oTable=document.all['fors:data'];
var oChkAll=document.all['fors:selectall']
if(oTable != null && oChkAll != null)
{
for(j=1;j
{
oTable.rows(j).cells(0).children.item(0).checked=oChkAll.checked;
}
}
}
show book编辑页面也就弹出对话窗页面editor.jsp
editorfunction confirmSubmit(){
setInterval('opener.window.location.href=opener.window.location.href;window.close();',20);
}
backing bean CDBean.java
package test;
import java.util.*;
import javax.faces.model.ListDataModel;
import javax.faces.event.ActionEvent;
public class CDBean {
private List CDList = new ArrayList();
private ListDataModel model;
private ListDataModel editorModel;
public CDBean() {
CDList.add(new CD("计算机应用", "长江", 25.00F, ""));
CDList.add(new CD("java模式", "长江", 80.00F, ""));
CDList.add(new CD("j2EE1.4标准教材", "长江", 100.00F, ""));
}
public ListDataModel getModel() {
if (model == null) {
model = new ListDataModel(CDList);
}
return model;
}
public ListDataModel getEditorModel() {
return editorModel;
}
public void editorTriggered(ActionEvent actionEvent) {
List list = (List) model.getWrappedData();
List editorList = new ArrayList();
for (Iterator it = list.iterator(); it.hasNext(); ) {
CD cd = (CD) it.next();
if (cd.getEditor()) {
editorList.add(cd);
}
}
editorModel = new ListDataModel(editorList);
}
public void saveTriggered(ActionEvent actionEvent) {
List list = (List) editorModel.getWrappedData();
for (Iterator it = list.iterator(); it.hasNext(); ) {
CD cd = (CD) it.next();
cd.setEditor(false);
}
}
}
辅助类CD.java
package test;
public class CD implements java.io.Serializable {
private Long id;
private String artist;
private String category;
private String subCategory;
private String title;
private float price;
private boolean editor;
public CD() {
}
public CD(String aTitle, String aArtist, float aPrice, String aCategory) {
this.title = aTitle;
this.artist = aArtist;
this.price = aPrice;
this.category = aCategory;
}
public void setArtist(String aArtist) {
this.artist = aArtist;
}
public String getArtist() {
return artist;
}
public void setCategory(String aCategory) {
this.category = aCategory;
}
public String getCategory() {
return category;
}
public void setId(Long aId) {
this.id = aId;
}
public Long getId() {
return id;
}
public void setPrice(float aPrice) {
this.price = aPrice;
}
public float getPrice() {
return price;
}
public void setSubCategory(String aSubCategory) {
this.subCategory = aSubCategory;
}
public String getSubCategory() {
return subCategory;
}
public void setTitle(String aTitle) {
this.title = aTitle;
}
public String getTitle() {
return title;
}
public void setEditor(boolean editor){
this.editor=editor;
}
public boolean getEditor(){
return this.editor;
}
}
配置文件faces-config.xml
cd
test.CDBean
session
/show.jsp
editor
/editor.jsp
/editor.jsp
show
/show.jsp
最后的总结:在弹出窗口里是没法用jsf的验证机制的,原因是setInterval('opener.window.location.href=opener.window.location.href;window.close();',20);有一个javascript的opener对象的引用,当用jsf验证时如果有非法输入,弹出窗口页面就会刷新,这就会丢失对opener的引用,而且还存在另一个问题就是如果验证正确就应该关闭对话框,失败就不应该关闭对话框.这个问题也不好解决. 那么是不是就没办法解决验证的问题呢?不是的.可以在弹出页面里用frame引用两个页面,一个保持对opener的引用,另一个就是编辑作用了,具体作法就是在backing bean 里添加一个action动态导航方法里调用FacesContext.getCurrentInstance().getResponseWriter().write(); 在write 方法里打印用于控制关闭对话框的javascript代码,因为action方法能执行就表示验证一定通过了.如果验证不通过的话就会返回自身页面抛出异常.在write 方法里,最后在write 方法的最后一定不能忘了 FacesContext.getCurrentInstance().responseComplete()以跳过转向的执行,也就是跳过jsf生命的最后一个阶段.还有另一个更好的解决方案,用Ajax实现验证,具体实现就不再赘述了,因为它不是jsf的一部分:) 欢迎加入QQ群:30406099
posted on 2006-07-31 10:44 傻 瓜 阅读(5031) 评论(2) 编辑 收藏 所属分类: Java Server Faces