最近在做一个网上书店的项目,其中有一个要实现高级搜索,即输入很多条件搜索图书~我用了Criteria来实现这个功能。代码如下:
BookDAO:

public List AdvancedFindBook(Object title, Object isbn, Object edition, Object authorName, Object publisherName){

Criteria criteria = getSession().createCriteria(Booktitle.class);
if(!title.equals(null)){
criteria.add(Restrictions.like("title", title));}
else if(!isbn.equals(null)){
criteria.add(Restrictions.eq("isbn", isbn));}
else if(!edition.equals(null)){
criteria.add(Restrictions.eq("edition", edition));}
else if(!authorName.equals(null)){
criteria.add(Restrictions.eq("authorName", authorName));}
else if(!publisherName.equals(null)){
Publisher publ = new Publisher();
PublisherService publser = new PublisherService();
publ.setPublisherId(((Publisher)publser.findByPublisherName(publisherName).get(0)).getPublisherId());
criteria.add(Restrictions.eq("publisherId", publ.getPublisherId()));}
List list = criteria.list();
return list;

}
JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
请输入搜索条件:
<s:form action = "advancedsearch">
<s:textfield name = "book.title" label = "title"/>
<s:textfield name = "book.isbn" label = "isbn"/>
<s:textfield name = "book.edition" label = "edition"/>
<s:textfield name = "book.authorName" label = "authorName"/>
<s:textfield name = "publ.publisherName" label = "publisherName"/>
<s:submit value = "sure"></s:submit>
</s:form>
</body>
</html>现在的效果是必须输入所有的条件才能搜索到结果,但是我想实现的是输入部分值也可以得到结果找了好久也不知道哪里错了。。求救!!谢谢大家啦~