简单CMS项目笔记之三:搜索栏和自定义superAction



一:自定义superAction

之前看的项目里都是直接继承于ActionSupport,然后每个具体action都重新写一遍extends ActionSupport和各种XXXAware

这个项目里自定义了多个superAction。这样n多具体action直接继承于相对应的superAction就行了

public class MySuperAction extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware {
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Map session;

	public void setSession(Map session) {
		this.session=session;	
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;		
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}	
}

public class InfoSuperAction extends MySuperAction {
	protected InfoSingle infoSingle;
	protected SearchInfo searchInfo;



二:搜索栏

在action中,先初始化各个搜索的项目

		/* 获取所有信息类别 */
		String sql="select * from tb_type order by type_sign";
		typeMap=myOp.OpGetListBox(sql,null);
		if(typeMap==null)
			typeMap=new TreeMap();

		/* 初始化搜索功能的下拉列表 */
		searchMap=new TreeMap();
		searchMap.put("ID值","id");
		searchMap.put("信息标题","info_title");
		searchMap.put("信息内容","info_content");
		searchMap.put("联系人","info_linkman");
		searchMap.put("联系电话","info_phone");
		searchMap.put("E-mail地址","info_email");

存放在searchMap里(TreeMap),过会还要组装还有通过request传递给jsp


public String execute() throws Exception {
		/* 查询所有收费信息,按发布时间降序排列 */
		OpDB myOp=new OpDB();
		String sql1="select * from tb_info where (info_state='1') and (info_payfor = '1') order by info_date desc";//筛选出审核通过的,和标记为付费的信息
		List payforlist=myOp.OpListShow(sql1,null);
		request.setAttribute("payforlist",payforlist);//通过request传递给jsp		

		/* 查询免费信息,按发布时间降序排列 */
		List allsublist=new ArrayList();//全部信息的集合,由底下各个子集合组成
		if(typeMap!=null&&typeMap.size()!=0){
			Iterator itype=typeMap.keySet().iterator();//迭代器,typeMap是操作Dao查询出来的类别结果组装出来的Map			
			String sql2="SELECT TOP 5 * FROM tb_info WHERE (info_type = ?) AND (info_state='1') AND (info_payfor = '0') ORDER BY info_date DESC";//选出前5条			
			while(itype.hasNext()){//迭代器遍历
				Integer sign=(Integer)itype.next();//取出所有信息的类别
				Object[] params={sign};//把类别组装成一个数组
				List onesublist=myOp.OpListShow(sql2, params);//上诉数组传递给sql的执行语句,数组的元素充当sql的占位符,查询出单个项目的全部信息(类型、标题、时间、表述字段等等)
				allsublist.add(onesublist);//将子集合循环都放到全部信息的集合里				
			}
			
		}
		request.setAttribute("allsublist",allsublist);//上述查询信息的全集
		session.put("typeMap",typeMap);
		session.put("searchMap",searchMap);		
		
		return SUCCESS;
	}



下边是上边提到的Dao层对数据库的操作;

	public TreeMap OpGetListBox(String sql,Object[] params){
		TreeMap typeMap=new TreeMap();
		mydb.doPstm(sql, params);
		try {
			ResultSet rs=mydb.getRs();
			if(rs!=null){
				while(rs.next()){					
					Integer sign=Integer.valueOf(rs.getInt("type_sign"));
					String intro=rs.getString("type_intro");
					typeMap.put(sign,intro);					
				}
				rs.close();
			}
		} catch (SQLException e) {			
			System.out.println("OpGetListBox()方法查询失败!");			
			e.printStackTrace();
		}finally{
			mydb.closed();			
		}
		return typeMap;	
	}
	
	public List OpListShow(String sql,Object[] params){
		List onelist=new ArrayList();
		mydb.doPstm(sql, params);
		try{
			ResultSet rs=mydb.getRs();
			if(rs!=null){
				while(rs.next()){
					InfoSingle infoSingle=new InfoSingle();
					infoSingle.setId(rs.getInt("id"));
					infoSingle.setInfoType(rs.getInt("info_type"));
					infoSingle.setInfoTitle(rs.getString("info_title"));
					infoSingle.setInfoContent(rs.getString("info_content"));
					infoSingle.setInfoLinkman(rs.getString("info_linkman"));
					infoSingle.setInfoPhone(rs.getString("info_phone"));
					infoSingle.setInfoEmail(rs.getString("info_email"));
					infoSingle.setInfoDate(DoString.dateTimeChange(rs.getTimestamp("info_date")));
					infoSingle.setInfoState(rs.getString("info_state"));
					infoSingle.setInfoPayfor(rs.getString("info_payfor"));
					onelist.add(infoSingle);					
				}
			}
			rs.close();
		}catch (Exception e){
			System.out.println("查看信息列表失败!(查询数据库)");			
			e.printStackTrace();
		}finally{
			mydb.closed();			
		}
		return onelist;		
	}




三:最后是jsp显示

searchInfo是一个model,包转好了这里边要用的信息

                    <s2:form action="info_SearchShow.action" theme="simple">                        
                        <tr height="10"><td colspan="3"></td></tr>
                        <tr>
                            <td align="right" width="70">关键字:</td>
                            <td colspan="2" width="200"><s2:textfield name="searchInfo.sqlvalue" value="%{searchInfo.sqlvalue}" size="21"/></td>
                        </tr>
                        <tr>
                            <td align="right">条件:</td>
                            <td>
                                <s2:select
                                    emptyOption="true"
                                    list="#session.searchMap"
                                    listKey="value"
                                    listValue="key"                             
                                    name="searchInfo.subsql"/>
                            </td>
                            <td align="center" width="30"><s2:submit value="搜索"/></td>
                        </tr>
                        <tr>
                            <td align="right">搜索类型:</td>
                            <td colspan="2">
                                <s2:radio 
                                    list="#request.searchType"
                                    value="%{'like'}"
                                    name="searchInfo.type"/>                                
                            </td>
                        </tr>
                        <tr height="10"><td colspan="3"></td></tr>
                    </s2:form>


具体的执行的action“info_SearchShow.action”的配置放在后边一块儿说,

执行上,action中的searchShow方法

public String SearchShow() throws UnsupportedEncodingException{
		request.setAttribute("mainPage","/pages/show/searchshow.jsp");		
		
		String subsql=searchInfo.getSubsql();
		String sqlvalue=searchInfo.getSqlvalue();		
		String type=searchInfo.getType();
		
		String showType=request.getParameter("showType");
		if(showType==null)
			showType="";
		if(showType.equals("link")){		//对从超链接中获取的参数进行转码操作
			try {
				sqlvalue=new String(sqlvalue.getBytes("ISO-8859-1"),"gb2312");
			} catch (UnsupportedEncodingException e) {			
				sqlvalue="";
				e.printStackTrace();
			}
			searchInfo.setSqlvalue(sqlvalue);
		}
		
		session.put("subsql",subsql);
		session.put("sqlvalue",sqlvalue);
		session.put("type",type);
		
		String param="";
		String opname="";
		if(type.equals("like")){
			opname=" LIKE ";
			param="%"+sqlvalue+"%";			
		}
		else{		
			opname=" = ";
			param=sqlvalue;			
		}
		
		String sqlSearchAll="SELECT * FROM tb_info WHERE ("+subsql+opname+"?) ORDER BY info_date DESC";
		String sqlSearchSub="";		
		Object[] params={param};		

		int perR=8;
		String strCurrentP=request.getParameter("showpage");
		String gowhich = "info_SearchShow.action?searchInfo.subsql="+subsql+"&searchInfo.sqlvalue="+sqlvalue+"&searchInfo.type="+type+"&showType=link";
		
		OpDB myOp=new OpDB();
		CreatePage createPage=myOp.OpCreatePage(sqlSearchAll, params,perR,strCurrentP,gowhich);			//调用OpDB类中的OpCreatePage()方法计算出总记录数、总页数,并且设置当前页码,这些信息都封装到了createPage对象中
		
		int top1=createPage.getPerR();
		int currentP=createPage.getCurrentP();
		
		if(currentP==1){     		//显示第1页信息的SQL语句
			sqlSearchSub="SELECT TOP "+top1+" * FROM tb_info WHERE ("+subsql+opname+"?) ORDER BY info_date DESC";
		}
		else{						//显示除第1页外,其他指定页码信息的SQl语句
			int top2=(currentP-1)*top1;
			sqlSearchSub="SELECT TOP "+top1+" * FROM tb_info WHERE ("+subsql+opname+"?) AND (info_date < (SELECT MIN(info_date) FROM (SELECT TOP "+top2+" info_date FROM tb_info WHERE "+subsql+opname+"'"+param+"' ORDER BY info_date DESC) AS mindate)) ORDER BY info_date DESC";
//			sqlSearchSub="SELECT TOP "+top1+" * FROM tb_info WHERE ("+subsql+opname+"?) AND (info_date NOT IN (SELECT TOP "+top2+" info_date FROM tb_info WHERE "+subsql+opname+"'"+param+"' ORDER BY info_date DESC)) ORDER BY info_date DESC";				//另一种实现分页查询的SQL语句
		}
		
		List searchlist=myOp.OpListShow(sqlSearchSub, params);
		request.setAttribute("searchlist",searchlist);
		request.setAttribute("createpage", createPage);
		
		return SUCCESS;
	}























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值