利用JSF RI实现批量删除

lvsiquan 2008-08-27 03:49:23
主要代码如下:

<h:form id="editHouse">
<h:dataTable id="book" value="#{ufdemobean._queryResult}" var="q"
styleClass="orders" headerClass="ordersHeader"
rowClasses="evenColumn,oddColumn" rows="4">
<h:column id="column6">
<f:facet name="header">
<f:verbatim>
<h:selectBooleanCheckbox id="allbox"
immediate="false"></h:selectBooleanCheckbox>
<hutputText value="#{msg['right.columnname.SELECT']}"></hutputText>
</f:verbatim>
</f:facet>
<h:selectBooleanCheckbox id="select" value="#{[color=#008000]q.selected}"
immediate="false"></h:selectBooleanCheckbox>[/color]
</h:column>
<h:column id="column1">
<f:facet name="header">
<f:verbatim escape="true">
<hutputText value="#{msg['list.label.NAME']}"></hutputText>
</f:verbatim>
</f:facet>
<hutputText value="#{q.name}"></hutputText>
</h:column>
……
</h:dataTable>
</h:form>

<h:form>
<h:commandButton id="delete" value="#{msg['list.button.DELETE']}"
styleClass="button" action="#{ufdemobean.deleteSelected}">
</h:commandButton>
</h:form>
------------------------------------下面是backing bean
public String deleteSelected() throws SQLException {
int result = 0;
List<Book> selectedDataList = new ArrayList<Book>();
for (Book book : this._queryResult) {
// if (book.id == null)
// continue;
if (book.getSelected()) {
selectedDataList.add(book);
}

}
result = _theBR.deleteSelected(selectedDataList);//调用删除操作的方法
if (result > 0) {
this._msg = "成功删除!";
return "deleteselected:success";
} else {
this._msg = "删除失败!";
return "deleteselected:fail";
}

}
---------------------------------Book 类代码如下
public class Book {
public int id;
public String name;
public String author;
public String press;
public double price;
public Boolean selected;

-----getter和setter方法
}

----------------------------------------------------
_queryResult的定义是List<Book> _queryResult = new ArrayList<Book>();
第一列是一组选择按钮,用于批量删除,可现在问题来了,无论将哪一行的checkbox勾上,点击删除按钮都是删除失败,似乎页面中
<h:selectBooleanCheckbox id="select" value="#{[color=#008000]q.selected}"
immediate="false"></h:selectBooleanCheckbox>[/color]该组件对应的值不会传递到q.selected,即Book类中的selected属性,请高手指点一下为什么值没有传过去?



...全文
189 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvsiquan 2008-08-27
  • 打赏
  • 举报
回复
List <Book> _queryResult = new ArrayList <Book>();
这个list声明我已经放在类的开头了,getter和setter也都有,其他的代码和你的大同小异,可是页面上那个checkbox的值却不能更改list里对应的boolean值,每次删除都是失败
cfpl12011124 2008-08-27
  • 打赏
  • 举报
回复
还有Article类:
private Integer articleId;
private Category category;
private String articleName;
private String unit;
private Double currentPrice;
private Set buyBills = new HashSet(0);

private Integer requisitionNumber;
private Double subTotalPrice;

private boolean checked;
cfpl12011124 2008-08-27
  • 打赏
  • 举报
回复
我写的一个。
JSP页面:
<table>
<tr>
<td colspan="5" align="right">
<h:commandButton styleClass="button" value="删除用品" action="#{category.deleteArticle}"/>
<h:commandButton styleClass="button" value="更新用品" action="#{category.updateArticle}"/>
</td>
</tr>
</table>
<h:dataTable id="articleList" value="#{category.articles}" var="article" headerClass="gray-bar" width="100%">
<h:column>
<f:facet name="header">
<h:outputText value="选择"/>
</f:facet>
<h:selectBooleanCheckbox id="articleIds" value="#{article.checked}" />
</h:column>

<h:column>
<f:facet name="header">
<h:outputText value="用品编号"/>
</f:facet>
<h:outputText value="#{article.articleId}"/>
</h:column>

<h:column>
<f:facet name="header">
<h:outputText value="用品名称"/>
</f:facet>
<h:inputText value="#{article.articleName}"/>
</h:column>

<h:column>
<f:facet name="header">
<h:outputText value="计量单位"/>
</f:facet>
<h:inputText value="#{article.unit}"/>
</h:column>
</h:dataTable>
申明List
//一个类别的所有用品
private List<Article> articles;
public List getArticles(){
return articles;
}

public void setArticles(List newValue){
this.articles=newValue;
}
删除方法:
//删除用品信息
public String deleteArticle() throws UserException {
int j=0;
//被选择的用品标识
Integer [] articleIds = new Integer[articles.size()];
for(int i=0;i<articles.size();i++){
Article article = (Article)articles.get(i);
if(article.isChecked()){
Integer articleId = article.getArticleId();
articleIds[j++] = articleId;
}
}
//调用服务层的删除方法
if(null != articleIds) {
ArticleService service = new ArticleServiceHibernateImpl();
if(service.deleteArticle(articleIds)) {
return "success";
}else {
return "failure";
}
}
return null;
}
cfpl12011124 2008-08-27
  • 打赏
  • 举报
回复
List <Book> _queryResult = new ArrayList <Book>();
不应该每次都new.
还有该List的定义不应在该方法中。
放到类的开头。
并且注入依赖

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧