我的SSH项目之旅(7.个人中心之分页和Junit测试)
接下来开发个人中心
个人中心包括内容:
1、 修改个人信息
2、 列出我提出的问题
3、 列出我回答过的问题
4、 列出被采纳的问题
分页大概的流程 从后台DAO去数据
public List queryByItem(int itemid, int currentPage, int lineSize)
throws Exception {
// TODO Auto-generated method stub
List all = null;
String hql = "FROM Question AS q WHERE q.itemid=?";
Query q = super.getSession().createQuery(hql);
q.setInteger(0, itemid);
q.setFirstResult((currentPage - 1) * lineSize);
q.setMaxResults(lineSize);
all = q.list();
return all;
}
经过struts action
public ActionForward selectitem(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
List all = null;
int currentPage = 1;
int lineSize = 10;
int allRecorders = 0;
try {
currentPage = Integer.parseInt(request.getParameter("cp"));
} catch (Exception e) {
}
try {
allRecorders = this.questiondao.getByItemCount(Integer
.parseInt(request.getParameter("itemid")));
all = this.questiondao.queryByItem(Integer.parseInt(request
.getParameter("itemid")), currentPage, lineSize);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("currentPage", currentPage);
request.setAttribute("lineSize", lineSize);
request.setAttribute("allRecorders", allRecorders);
request.setAttribute("all&qu
ot;, all); request.setAttribute("jspUrl", "show.do");
request.setAttribute("status", "selectitem");
request.setAttribute("flagname", "itemid");
request.setAttribute("flagvalue", request.getParameter("itemid"));
try {
request.setAttribute("allitem", this.itemdao.queryAll());
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("showlist");
}
在JSP页面嵌入相关代码
<jsp:include page="split_page.jsp">
<jsp:param name="jspUrl" value="${jspUrl}" />
<jsp:param name="lineSize" value="${lineSize}"/>每页显示条数
<jsp:param name="allRecorders" value="${allRecorders}" />
<jsp:param name="keyWord" value="<%=request.getAttribute("keyWord")%>" />
<jsp:param name="currentPage" value="${currentPage}" />
<jsp:param name="status" value="${status}" />
<jsp:param name="flagname" value="${flagname}" />
<jsp:param name="flagvalue" value="${flagvalue}" />
<jsp:param name="searchFlag" value="T" />
</jsp:include>
分页程序部分代码 split_page.jsp
// 计算总页数
pageSize = (allRecorders+lineSize<
/font>-1)/lineSize ;
<form name="spage" action="<%=jspUrl%>" onSubmit="change()">
<input type="hidden"
name="status" value="${status}"> <input type="hidden"
name="${param.flagname}" value=${param.flagvalue}> <%
if ("T".equals(searchFlag)) {
%> 输入查询关键字:<input type="text" name="kw"
value="<%=keyWord.equals("null") ? "" : keyWord%>"> <input
type="submit" value="查询"> <br>
<br>
<%
if (allRecorders > 0) {
%> <input type="button" value="首页" onClick="openPage(1)"
<%=currentPage == 1 ? "disabled" : ""%>> <input type="button"
value="上一页" onClick="openPage(<%=currentPage - 1%>)"
<%=currentPage == 1 ? "disabled" : ""%>> <input type="button"
value="下一页" onClick="openPage(<%=currentPage + 1%>)"
<%=currentPage == pageSize ? "disabled" : ""%>> <input type="button"
value="尾页" onClick="openPage(<%=pageSize%>)"
<%=currentPage == pageSize ? "disabled" : ""%>> <input type="hidden"
name="cp" value=""> <font color="red" size="5"><%=currentPage%></font>
/ <font color="red" size="5"><%=pageSize%></font> 跳转到 <select
name="selpage" onChange="selOpenPage()">
<%
for (int x = 1; x <= pageSize; x++) {
%>
<option value="<%=x%>" <%=currentPage == x ? "selected" : ""%>><%=x%></option>
<%
}
%>
</select> 页 <%
}
%>
Spring Junit4测试
public class UserDAOTest extends AbstractDependencyInjectionSpringContextTests {
@Override复写父类的方法,找到配制文件
protected String[] getConfigLocations() {
// TODO Auto-generated method stub
return new String[]{"classpath:applicationContext.xml"};
}
//引入在Spring注入的bean
private UserDAO userdao;
public void setUserdao(UserDAO userdao) {
this.userdao = userdao;
}
public void testLoadUser() throws Exception{
// User user=this.userdao.queryByUserid("fantlam");
// assertNotNull(user);
boolean b=this.userdao.isExists("fantlam", "你在吗", "我在");
assertEquals(true,b);
}
//可以直接运行程序
public static void main(String[] args){
// junit.swingui.TestRunner.run(UserDAOTest.class);因为conmon-logging的原因,使用junit.awtui或junit.swingui包下的TestRunner会出现错误/
故采用junit.textui包
junit.textui.TestRunner.run(UserDAOTest.class);
}
}
applicationContext.xml
<bean id="userdao" class="dj.fantlam.myssh.dao.UserDAO"
abstract="true">
</bean>
<bean id="userdaoimpl" class="dj.fantlam.myssh.daoimpl.UserDAOImpl"
parent="userdao">
<property name="sessionFactory">
<ref bean=" sessionFactory " />
</property>
</bean>
关于spring junit测试的类关系 org.hibernate.QueryException: could not resolve property: qid of: dj.fantlam.myssh.vo.Answer [SELECT COUNT(q.qid) FROM dj.fantlam.myssh.vo.Question AS q WHERE q.qid IN (SELECT a.qid FROM dj.fantlam.myssh.vo.Answer AS a WHERE a.userid=?)]
解决方法:
在answer.hbm.xml添加属性
</property>
<property name="qid" type="java.lang.Integer">
<column name="qid" />
</property>
加入
<many-to-one name="question"
class="dj.fantlam.myssh.vo.Question" fetch="select"
insert="false" update="false">
<column name="qid" />
</many-to-one>
这样做了之后的确可以解决问题,但破坏了表的关联属性,导致无法进行其它插入等操作,所以这种方法不行。
根据表的一对多的关系,把整个HQL语句改成
String hql = "SELECT COUNT(q.qid) FROM Question AS q WHERE q.qid IN (SELECT a.question.qid FROM Answer AS a WHERE a.userid=? AND status=?)";
然后把之前改过的东西还原回来(把红色的都去掉) 这样就没问题了
|
Thank you for this wonderful post. It contains many details which I have to have. I am bookmark your web page by my next go to.
I found this on google, many thanks for writing this, it was just what I had been searching for!
Strange this post is totaly unrelated to what I was searching google for, but it was listed on the first page. I guess your doing something right if Google likes you enough to put you on the first page of a non related search. 🙂