新闻web小酌
首页如上
类图如下:
添加新闻的方法(dao):
1 public boolean Add(News news) { 2 boolean flag=false; 3 Connection con =getConnectionData(); 4 QueryRunner queryRunner = new QueryRunner(); 5 String sql="INSERT INTO newscon(newstype,title,author,abstrac ,content,picture) VALUE(?,?,?,?,?,?)"; 6 Object[] paras={news.getNewstype(),news.getTitle(),news.getAuthor(),news.getAbstrac(),news.getContent(),news.getUrl()}; 7 int count=0; 8 try { 9 count= queryRunner.update(con,sql,paras); 10 } catch (SQLException e) { 11 throw new RuntimeException(e.getMessage(),e); 12 } 13 closeAll(); 14 if(count>0){ 15 flag=true; 16 } 17 return flag; 18 }
servlet方法:
1 public class AddNews extends HttpServlet { 2 public void doGet(HttpServletRequest request, HttpServletResponse response) 3 throws ServletException, IOException { 4 doPost(request,response); 5 } 6 7 public void doPost(HttpServletRequest request, HttpServletResponse response) 8 throws ServletException, IOException { 9 request.setCharacterEncoding("utf-8"); 10 if(request.getMethod().equals("GET")){ 11 request.getRequestDispatcher("/newspages/news_add.jsp").forward(request, 12 response); 13 }else { 14 String path = request.getContextPath(); 15 String basePath = request.getScheme() + "://" + request.getServerName() 16 + ":" + request.getServerPort() + path + "/"; 17 response.setContentType("text/html;charset=UTF-8"); 18 request.setCharacterEncoding("UTF-8"); 19 HttpSession session = request.getSession(); 20 boolean flag=false; 21 try { 22 List<News> list = (List<News>) FormUtil.assembleObjectList(request,News.class); 23 News news = list.get(0); 24 NewsDaoimpl dao=new NewsDaoimpl(); 25 flag = dao.Add(news); 26 27 } catch (NoSuchFieldException e) { 28 e.printStackTrace(); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 if (flag) { 33 response.sendRedirect(path); 34 35 }else { 36 request.getRequestDispatcher("/index.jsp").forward(request, 37 response); 38 } 39 40 } 41 } 42 43 }
插入标题:
1 public class AddTitleServlet extends HttpServlet { 2 public void doGet(HttpServletRequest request, HttpServletResponse response) 3 throws ServletException, IOException { 4 doPost(request, response); 5 } 6 7 public void doPost(HttpServletRequest request, HttpServletResponse response) 8 throws ServletException, IOException { 9 request.setCharacterEncoding("utf-8"); 10 if (request.getMethod().equals("GET")) { 11 request.getRequestDispatcher("/newspages/news_add.jsp").forward( 12 request, response); 13 } else { 14 String path = request.getContextPath(); 15 String basePath = request.getScheme() + "://" 16 + request.getServerName() + ":" + request.getServerPort() 17 + path + "/"; 18 response.setContentType("text/html;charset=UTF-8"); 19 request.setCharacterEncoding("UTF-8"); 20 HttpSession session = request.getSession(); 21 boolean flag = false; 22 try { 23 List<Topic> list = (List<Topic>) FormUtil.assembleObjectList( 24 request, Topic.class); 25 Topic topic = list.get(0); 26 NewsDaoimpl dao = new NewsDaoimpl(); 27 flag = dao.AddTitle(topic); 28 29 } catch (NoSuchFieldException e) { 30 e.printStackTrace(); 31 } catch (Exception e) { 32 e.printStackTrace(); 33 } 34 if (flag) { 35 response.sendRedirect(path); 36 37 } else { 38 request.getRequestDispatcher("/index.jsp").forward(request, 39 response); 40 } 41 } 42 } 43 44 }
标题的动态加载:
index:
1 <c:forEach var="item" items="${requestScope.Toplist}"> 2 <a style="color:blue;font-size:16px;" href='${pageContext.request.contextPath }/DoIndexServlet?id=${item.nid}'> 3 ${item.newstype} 4 </a> 5 </c:forEach> 6 </li> 7 </ul> 8 <ul class="classlist"> 9 <c:forEach var="item" items="${requestScope.newsList }"> 10 <li><a href='newspages/news_read.jsp'>${item.title }</a> 11 <span> 12 <c:forEach var="item1" items="${TimeList}"> 13 ${item1.beginDatetime} 14 </c:forEach> 15 </span> 16 </li> 17 </c:forEach>
impl:
1 public List<Topic> AllTitle() { 2 3 Connection con=getConnectionData(); 4 String sql="select nid,newstype from type"; 5 QueryRunner query=new QueryRunner(); 6 List<Topic> list=null; 7 try { 8 9 list= query.query(con, sql,new BeanListHandler<Topic>(Topic.class)); 10 } catch (SQLException e) { 11 new RuntimeException(e.getMessage()); 12 13 } 14 return list; 15 }