您的位置:首页 > 其它

简单CMS项目笔记之四:信息显示(列表显示和显示具体单项)

2012-12-06 17:03 330 查看
一:struts配置

所有前台信息显示都用了占位符:info_占位符,占位符被解析成方法

<!-- 前台信息处理 -->
<action name="info_*" class="com.yxq.action.InfoAction" method="{1}">
<result>/view/IndexTemp.jsp</result>
<result name="input">/view/IndexTemp.jsp</result>
</action>


二:列表显示

每次显示分两部分,一是付费的,二是免费的,两部分都放进request

每一个组是单独组装sql语句(只有payfor标识不一样而已)

从request里get参数,JDBC操作完的结果再送回request

这儿的数据库是SQLserver,Oracle的取前几个的方法之前的日志写过

public String ListShow(){
request.setAttribute("mainPage","/pages/show/listshow.jsp");

String infoType=request.getParameter("infoType");//获取需要显示的类别
Object[] params={infoType};//通过数组传递,后边的方法也是一样
String sqlPayfor="SELECT * FROM tb_info WHERE (info_type = ?) AND (info_state='1') AND (info_payfor = '1') ORDER BY info_date DESC";

OpDB myOp=new OpDB();

/* 获取所有的付费信息 */
List onepayforlist=myOp.OpListShow(sqlPayfor, params);				//获取所有付费信息
request.setAttribute("onepayforlist",onepayforlist);		//写到request一会儿传给jsp

/* 获取当前页要显示的免费信息 */
String sqlFreeAll="SELECT * FROM tb_info WHERE (info_type = ?) AND (info_state='1') AND (info_payfor = '0') ORDER BY info_date DESC";
String sqlFreeSub="";
int perR=3;
String strCurrentP=request.getParameter("showpage");	//分页页码
String gowhich="info_ListShow.action?infoType="+infoType;	//留给分页类,分页类把这句组装进跳转信息
CreatePage createPage=myOp.OpCreatePage(sqlFreeAll, params,perR,strCurrentP,gowhich);			//调用OpDB类中的OpCreatePage()方法计算出总记录数、总页数,并且设置当前页码,这些信息都封装到了createPage对象中

int top1=createPage.getPerR();
int currentP=createPage.getCurrentP();
if(currentP==1){     		//设置显示第1页信息的SQL语句
sqlFreeSub="SELECT TOP "+top1+" * FROM tb_info WHERE (info_type = ?) AND (info_state = '1') AND (info_payfor = '0') ORDER BY info_date DESC";
}
else{						//设置显示除第1页外,其他指定页码信息的SQl语句
int top2=(currentP-1)*top1;
sqlFreeSub="SELECT TOP "+top1+" * FROM tb_info i WHERE (info_type = ?) AND (info_state = '1') AND (info_payfor = '0') AND (info_date < (SELECT MIN(info_date) FROM (SELECT TOP "+top2+" (info_date) FROM tb_info WHERE (info_type = i.info_type) AND (info_state = '1') AND (info_payfor = '0') ORDER BY info_date DESC) AS mindate)) ORDER BY info_date DESC";
//			sqlFreeSub="SELECT TOP "+top1+" * FROM tb_info i WHERE (info_type = ?) AND (info_state = '1') AND (info_payfor = '0') AND (info_date NOT IN (SELECT TOP "+top2+" info_date FROM tb_info WHERE (info_type = i.info_type) AND (info_state = '1') AND (info_payfor = '0') ORDER BY info_date DESC)) ORDER BY info_date DESC";         //另一种实现分页查询的SQL语句
}

List onefreelist=myOp.OpListShow(sqlFreeSub, params);				//获取免费信息,信息数量由createPage.setPerR(int PerR)方法指定
request.setAttribute("onefreelist",onefreelist);
request.setAttribute("createpage", createPage);

return SUCCESS;
}


列表的显示

<s2:set name="onepayforlist" value="#request.onepayforlist"/>
<table border="0" width="670" cellspacing="0" cellpadding="0" style="margin-top:5">
<s2:if test="#onepayforlist==null||#onepayforlist.size()==0">
<tr height="30"><td align="center">★★★ 缴费后,您发布信息就可在这里显示!★★★</td></tr>
</s2:if>
<s2:else>
<tr height="30"><td style="text-indent:5" valign="bottom"><font color="#004790"><b>■推荐<s2:property value="#session.typeMap[#onepayforlist[0].infoType]"/></b>『缴费专区』</font></td></tr>
<s2:iterator status="onepayforStatus" value="onepayforlist">
<s2:if test="#onepayforStatus.odd">
<tr><td align="center" style="border:1 solid" bgcolor="#F0F0F0">
</s2:if>
<s2:else>
<tr><td align="center" style="border:1 solid" bgcolor="white">
</s2:else>
<table border="0" width="655" cellpadding="3" style="word-break:break-all">
<tr height="30">
<td colspan="2">【<s2:property value="#session.typeMap[infoType]"/>】</td>
<td align="right">发布时间:『<s2:property value="infoDate"/>』 </td>
</tr>
<tr height="80"><td colspan="3"><s2:property value="infoContent"/></td></tr>
<tr height="30" align="center">
<td>联系电话:<s2:property value="infoPhone"/></td>
<td>联 系 人:<s2:property value="infoLinkman"/></td>
<td>E-mail:<s2:property value="infoEmail"/></td>
</tr>
</table>
</td>
</tr>
<tr height="1"><td></td></tr>
</s2:iterator>
</s2:else>
</table>


三:具体某一项的显示

列表显示了一大堆,通过id访问其中某一个就是具体的显示:信息名称,发布时间,发布人,具体表述等

public String SingleShow(){
request.setAttribute("mainPage","/pages/show/singleshow.jsp");//和列表显示一样,要替换页面中显示信息

String id=request.getParameter("id");//根据id区分
String sql="SELECT * FROM tb_info WHERE (id = ?)";
Object[] params={id};

OpDB myOp=new OpDB();
infoSingle=myOp.OpSingleShow(sql, params);//根据查找出具体信息
if(infoSingle==null){
request.setAttribute("mainPage","/pages/error.jsp");
addFieldError("SingleShowNoExist",getText("city.singleshow.no.exist"));
}
return SUCCESS;
}


这儿最后没有把找出的结果放进request,作者解释是:此时会把这个信息放到堆栈顶。我觉得还是setAttribute靠谱一点

jsp的输出上大同小异

<s2:else>
<tr height="40">
<td width="20%" style="text-indent:20">信息类别:</td>
<td><s2:property value="#session.typeMap[infoSingle.infoType]"/></td>
</tr>
<tr height="40">
<td style="text-indent:20">发布时间:</td>
<td><s2:property value="infoSingle.infoDate"/></td>
</tr>
<tr height="40">
<td style="text-indent:20">信息标题:</td>
<td><s2:property value="infoSingle.infoTitle"/></td>
</tr>
<tr height="40"><td colspan="2" style="text-indent:20">信息内容:</td></tr>
<tr>
<td colspan="2" align="center">
<table border="1" width="630" cellspacing="0" cellpadding="10" rules="none" frame="below" style="word-break:break-all" bordercolor="lightgrey" bordercolorlight="lightgrey" bordercolordark="white">
<tr height="200" bgcolor="white"><td colspan="3" valign="top" style="border:1 solid"><s2:property value="infoSingle.infoContent" escape="false"/></td></tr>
<tr height="20" align="center">
<td>联系电话:<s2:property value="infoSingle.infoPhone"/></td>
<td>联系人:<s2:property value="infoSingle.infoLinkman"/></td>
<td>E-mial:<s2:property value="infoSingle.infoEmail"/></td>
</tr>
</table>
</td>
</tr>
<tr height="63"><td align="center" colspan="2"><a href="javascript:window.history.back(-1)">返回</a></td></tr>
</s2:else>


显示过程中#session.v1"-等同于#session['v1'],这个struts2标签的语法比较怪
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: