iCoding项目----题解页面实现
题解页面实现
- 题解动态展示和翻页
①思路跟题库差不多
②不同的地方是,点击超链接到题解详细页面的时候返回三个PageList,目的是方便在题解详细页面翻页。Servlet代码为
if("answer".equals(type))
{
List<Code> codes = new ArrayList<Code>();
String uidStr = request.getParameter("uid");
code.setUid(Integer.valueOf(uidStr));
GetQuestionByidService gs = new GetQuestionByidServiceImpl();
codes = gs.getQuestionByidAnsService(code);
request.setAttribute("qid", codes.get(1).getQid());
request.setAttribute("tit", codes.get(1).getQuestionTitle());
request.setAttribute("des", codes.get(1).getQuestionDescription());
request.setAttribute("code", codes.get(1).getCode());
request.setAttribute("author",codes.get(1).getUsername());
request.setAttribute("pertit", codes.get(0).getQuestionTitle());
request.setAttribute("perqid", codes.get(0).getQid());
request.setAttribute("peruid", codes.get(0).getUid());
request.setAttribute("nexttit", codes.get(2).getQuestionTitle());
request.setAttribute("nextqid", codes.get(2).getQid());
request.setAttribute("nextuid", codes.get(2).getUid());
request.getRequestDispatcher("questionAns.jsp").forward(request, response);
}
③Dao层代码为
@Override
public List<Code> getQuestionByidAnsDao(Code code) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Code> Codes = new ArrayList();
try {
conn = JDBCUtil.getConnection();
String sql = "select questionDescription from questionbank where qid = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,code.getQid());
rs = ps.executeQuery();
rs.next();
code.setQuestionDescription(rs.getString(1));
sql = "select * from codes where qid = ? and uid = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,code.getQid());
ps.setInt(2, code.getUid());
rs = ps.executeQuery();
rs.next();
code.setQuestionTitle(rs.getString(2));
code.setCode(rs.getString(3));
sql = "select *from codes inner join user on codes.uid = `user`.id and codes.uid = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,code.getUid());
rs = ps.executeQuery();
rs.next();
code.setUsername(rs.getString(7));
//changeAnsPage所需
sql = "select * from codes inner join user on codes.uid = `user`.id";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
Code preCode = new Code();
Code nextCode = new Code();
Code tempCode = new Code();
int count=0;
boolean flag = false;
while(rs.next())
{
count++;
if(code.getQid()==rs.getInt(1)&&code.getUid()==rs.getInt(5))
{
System.out.println(code.getQid());
System.out.println(code.getUid());
preCode = tempCode;
if(rs.next())
{
nextCode.setQid(rs.getInt(1));
nextCode.setQuestionTitle(rs.getString(2));
nextCode.setCode(rs.getString(3));
nextCode.setUid(rs.getInt(5));
nextCode.setUsername(rs.getString(7));
}
else
{
flag=true;
}
break;
}
tempCode.setQid(rs.getInt(1));
tempCode.setQuestionTitle(rs.getString(2));
tempCode.setCode(rs.getString(3));
tempCode.setUid(rs.getInt(5));
tempCode.setUsername(rs.getString(7));
}
if(count==1)
{
preCode.setQid(0);
}
if(flag)
{
nextCode.setQid(0);
}
Codes.add(preCode);
Codes.add(code);
Codes.add(nextCode);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
JDBCUtil.closeConnection(rs, ps, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return Codes;
}
④翻页html代码为
<div>
<c:if test="${perqid!=0}">
<a href="getquestionByid?type=answer&qid=${perqid}&uid=${peruid}">
上一篇:${pertit}
</a>
</c:if>
<c:if test="${perqid==0 }">
<a href="#">
当前为第一篇
</a>
</c:if>
<br><br>
<c:if test="${nextqid!=0 }">
<a href="getquestionByid?&type=answer&qid=${nextqid}&uid=${nextuid}">
下一篇:${nexttit}
</a>
</c:if>
<c:if test="${nextqid==0}">
<a href="#">
当前为最后一篇
</a>
</c:if>
</div>