练习:模仿发短信来做一个简单的论坛(部分问题)

1、分页显示信息:

public List<T> getAllById(int pageCount,int pageNum,int id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs  = null;
		List<T> list = new ArrayList<T>();
		try {
			conn = this.getConn();
			stmt = conn.prepareStatement("select * from(select t.*,rownum rk from testTablet order by id) where rk<=? and rk>? ");
			stmt.setInt(1, pageNum*pageCount);
			stmt.setInt(2, (pageNum-1)*pageCount);
			rs = stmt.executeQuery();
			while(rs.next()){
				T t= new T();
				list.add(t);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(rs, stmt, conn);
		}
	
		return list;
	}

int pageSize = 2;
int count = messBiz.pageDiv(user.getUserid());
int p =1;	
if(request.getParameter("p")!=null){
	p = Integer.parseInt(request.getParameter("p"));
}
int num = (int)Math.ceil(count/pageSize);
int prePage = p==1?1:p-1;
int nexPage = p==num?num:p+1; 
..........
 共<%=count %>条<%=num %>页第<%=p %>页
   <a href="listMsg.jsp?p=<%=prePage %>">上一页</a>
   <a href="listMsg.jsp?p=<%=nexPage %>">下一页</a>

2、发件人下拉列表的选择
<form action="doPost.jsp" method="post" id="myForm">
	<table border="1" >
	
	 <tr><td>收件人</td>
	 <td>
	 <select name = "recId">
	 	<%for(Users user:userList){ %>
	 	<option value="<%=user.getUserid() %>"><%=user.getUserName() %></option>
	 	<%} %>
	 </select>
	 <%if(request.getParameter("id")!=null) {%>
	 	<script type="text/javascript">myForm.recId.value='<%=Integer.parseInt(request.getParameter("id"))%>'</script>
	 <%} %>
	 </td></tr>
	 
	<tr><td>标题</td><td><input type="text" name="title"/></td></tr>
	<tr><td><input type="text" name="content"/></td></tr>
	</table>
	<input type="submit" name="reply" value="发送"/>	
	<input type="reset" name="summint" value="清空" />
	</form>

3、使用按钮实现页面的跳转并传递参数
	<input type="button" value="回复" onclick="window.location.href='post.jsp?id=${mess.senderId}'"/>
	<input type="button" value="删除" onclick="window.location.href='doDelete.jsp?id=${mess.messageId}'"/>

4、对于奇偶行显示不同的北京颜色(使用的是JSTL标签)
<table>
   	<tr>
   	<td></td><td>状态</td><td>标题</td><td>发件人</td><td>时间</td><td>是否删除</td>
   	</tr>
    	<c:forEach var="mess"  items="${messList}" step="1" varStatus="staus">
    	<c:choose >
    	<c:when test="${staus.index%2==0}" ><c:set var="color" value="#FAEBD7"></c:set></c:when>
    	<c:otherwise ><c:set var="color" value="#FFEBCD"></c:set></c:otherwise>
    	</c:choose>
   	<tr bgcolor=${color} }>
   	<td>${staus.index }</td>
   	<td>${mess.isRead}</td>
   	<td><a href="showMsg.jsp?id=${mess.messageId }">${mess.title }</a></td>
   	<td>${mess.senderId }</td>
   	<td>${mess.time }</td>
   	<td><input type="button" value="删除" onclick="window.location.href='doDelete.jsp?id=${mess.messageId }'"/></td>
   	</tr>
   	</c:forEach>
	
   </table>

5、退出使用时
session.removeAttribute("user");
response.sendRedirect("login.jsp");

6、设置字节码类型的Filter
public class codeFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		chain.doFilter(request, response);

	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}

6、记录用户登录的Filter
public class SessionFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		String url = req.getRequestURI();
		if(url.endsWith("login.jsp")||url.endsWith("doLogin.jsp")){
			chain.doFilter(request, response);
		}
		else{
			if(req.getSession().getAttribute("user")==null){
				res.sendRedirect("login.jsp");
			}
			else{
				chain.doFilter(request, response);
			}
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}

7、关于DAO中方法的命名规则
增删改分为使用insert、delete、update
查使用的是get
8、关于我的BaseDao
public class BaseDao {
	public Connection getConn(){
		Connection conn  = null;
		try {
			Context ic = new InitialContext(); 
			DataSource source = (DataSource) ic.lookup("java:comp/env/jdbc/message"); 
			conn = source.getConnection(); 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public void closeAll(ResultSet rs ,PreparedStatement stmt,Connection conn){
		
			try {
				if(rs != null)
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
	}
	public int executeUpdate(String sql,Object[] obj){
		Connection conn =  getConn();
		PreparedStatement stmt = null;
		int ret = 0;
		try {
			stmt = conn.prepareStatement(sql);
		for(int i=0;i<obj.length;i++){
			stmt.setObject(i+1, obj[i]);
		}
			ret = stmt.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll(null,stmt,conn);
		}
		return ret;
		}

	

}

9、关于表连接后属性的问题
可以再实体类中多加几个属性来满足表连接后的视图的问题。

你可能感兴趣的:(问题)