struts2之分页和多条件查询

分页:

//分页查询
	public List fenye(int index,int size) {
		log.debug("finding all House instances");
		try {
			String queryString = "from House";
			Query queryObject = getSession().createQuery(queryString);
			//分页的参数设置
			queryObject.setFirstResult((index-1)*size);
			queryObject.setMaxResults(size);
			
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
//分页查询
	public String fenye() throws Exception {
		HouseDAO dao = new HouseDAO();
		int total = (dao.findAll()).size();
		count = total%size==0?total/size:total/size+1;
		System.out.println("count:"+count);
		if (index<1) {
			index=1;
		}
		if (index>count) {
			index=count;
		}
		List<House> list = dao.fenye(index,size);
		session.remove("info");
		session.put("info", list);
		return SUCCESS;
	}

xml

        <action name="fenyeAction" class="com.bdqn.action.HouseAction" method="fenye">
			<result name="success">/HouseIndex.jsp</result>
		</action>

页面

<a href="Hibernate_Strutes2/HouseAction/fenyeAction?index=0&size=${size}"><button>首页</button></a>
  <a href="Hibernate_Strutes2/HouseAction/fenyeAction?index=${index-1}&size=${size}"><button>上一页</button></a>
  <a href="Hibernate_Strutes2/HouseAction/fenyeAction?index=${index+1}&size=${size}"><button>下一页</button></a>
  <a href="Hibernate_Strutes2/HouseAction/fenyeAction?index=${count}&size=${size}"><button>尾页</button></a>
  

多条件查询:

<!-- 查询 -->
  <form action="/Hibernate_Strutes2/HouseAction/findByTiaoJianAction">
  <strong>房屋信息</strong><br>
  包含关键字:<input type="text" name="title">
  	价格:<select name="price">
  		<option value=0>不限</option>
  		<option value=1000>0~1000</option>
  		<option value=2000>1000~2000</option>
  		<option value=3000>20000~3000</option>
  	</select>
  	房屋位置:<select name="streetid">
  		<option value=0>不限</option>
  		<option value=1>天河大道</option>
  		<option value=2>黄埔大道一号</option>
  		<option value=3>黄埔大道二号</option>
  		<option value=4>越秀大道</option>
  	</select>
  	房型:<select name="type1id">
  		<option value=0>不限</option>
  		<option value=1>出租屋</option>
  		<option value=2>公寓</option>
  		<option value=3>天桥底</option>
  	</select>
  	面积:<select name="floorage">
  		<option value=0>不限</option>
  		<option value=500>500以下</option>
  		<option value=1000>1000以下</option>
  		<option value=1500>1500以下</option>
  	</select>
  	日期:<select name="pubdate">
  		<option value=0>不限</option>
  		<option value=1>一个月内</option>
  		<option value=2>两个月内</option>
  		<option value=3>三个月内</option>
  	</select><br>
  	<input type="submit" value="搜索">
  </form>
//按条件查询
	public List findBy(String title,int price,int streetid,int type1id,int floorage,int pubdate) {
//		from house where title like '%新%' and price < 2000 and street_id = 4 and 
//				type_id = 1 and floorage < 1500 and pubdate > (sysdate-3)
		log.debug("finding all House instances");
		try {
			String queryString = "from House where 1=1";
			if (title!=null) {
				queryString=queryString+" and title like '%"+title+"%'";
			}
			if (price!=0) {
				queryString=queryString+" and price <  "+price;
			}
			if (streetid!=0) {
				queryString=queryString+" and street_id = "+streetid;
			}
			if (type1id!=0) {
				queryString=queryString+" and type_id = "+type1id;
			}
			if (floorage!=0) {
				queryString=queryString+" and floorage <= "+floorage;
			}
			if (pubdate!=0) {
				queryString=queryString+" and pubdate > (sysdate-"+pubdate+")";
			}
			System.out.println(queryString);
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
//条件查询
	public String findByTiaoJian() throws Exception {
		System.out.println(title);
		System.out.println(price);
		System.out.println(streetid);
		System.out.println(type1id);
		System.out.println(floorage);
		System.out.println(pubdate);
		
		HouseDAO dao = new HouseDAO();
		List<House> list = dao.findBy(title, price, streetid, type1id, floorage, pubdate);
//		for (House house : list) {
//			System.out.println(house.getTitle());
//			System.out.println(house.getPrice());
//			System.out.println(house.getStreet());
//			System.out.println(house.getType1());
//			System.out.println(house.getFloorage());
//			System.out.println(house.getPubdate());
//		}
		session.remove("info");
		session.put("info", list);
		return SUCCESS;
	}
<action name="findByTiaoJianAction" class="com.bdqn.action.HouseAction" method="findByTiaoJian">
			<result name="success">/HouseIndex.jsp</result>
		</action>

作业:

验证是否是管理员登录如果是就可以增加和删除

//登录
	public String Login() throws Exception {
		MyuserDAO dao = new MyuserDAO();
		List<Myuser> list = dao.findByExample(myuser);
		if (list.size()>0) {
			if (("admin").equals(list.get(0).getUsername())) {
				System.out.println(list.get(0).getUsername());
				num=1;
			}
			session.put("mess", num);
			return SUCCESS;
		}
		return ERROR;
	}
<!-- 电影action -->
		<action name="UserAction" class="com.bdqn.action.MyUserAction" method="Login">
			<result name="success" type="redirectAction">ShowMovieAction</result>
			<result name="error">/MyUserLogin.jsp</result>
		</action>
		
		<action name="ShowMovieAction" class="com.bdqn.action.MyUserAction" method="show">
			<result name="success">/showMovie.jsp</result>
		</action>
		
		<action name="DeleteMovieAction" class="com.bdqn.action.MyUserAction" method="delete">
			<result name="success" type="redirectAction">ShowMovieAction</result>
		</action>
		
		<action name="AddMovieAction" class="com.bdqn.action.MyUserAction" method="add">
			<result name="success" type="redirectAction">ShowMovieAction</result>
		</action>
<table border="2">
    	<tr>
    		<td>编号</td>
    		<td>电影名</td>
    		<td>导演</td>
    		<td>演员</td>
    		<td>介绍</td>
    		<td>上映时间</td>
    		
    		<c:if test="${mess==1}">
    		<td>操作</td>
    		</c:if>
    	</tr>	
    	<s:iterator value="session.movies" var="movie">
    		<tr>
    		<td>${movie.id }</td>
    		<td>${movie.moviename }</td>
    		<td>${movie.director }</td>
    		<td>${movie.player }</td>
    		<td>${movie.introduction}</td>
    		<td>${movie.time}</td>
    		<c:if test="${mess==1}">
    		<td>
    		<a href="/Hibernate_Strutes2/MyUserAction/DeleteMovieAction?delete=${movie.id}"><button>删除</button></a>
    		<a href="AddMovie.jsp"><button>增加</button></a>
    		</td>
    		</c:if>
    	</tr>
    	</s:iterator>
    </table>

猜你喜欢

转载自blog.csdn.net/jinqianwang/article/details/82144752