iCodingプロジェクト----問題解決ページの実現


iCodingプロジェクト----問題解決ページの実現
問題解決ページの実装
  • 問題解き動的展示とページめくり①構想と問題ライブラリの差が少ない②異なるところは、ハイパーリンクをクリックして問題解き詳細ページにリンクするときに3つのPageListを返し、問題解き詳細ページのページめくりを容易にすることを目的としている.サーブレットコードは
  • 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>