新闻发布系统

.功能发布

 1.主页面:类型标题展示、全部新闻分页、相应类型标题新闻分页

 2.后台

新闻管理:

    添加新闻、编辑新闻、删除新闻、新闻分页

主题管理:

添加主题、修改主题、删除主题、全部新闻

三架构设计

 

3.0实体类

3.0.1 Page 原本应该是工具类,因为分页很地方需要用到。

package cn.news.entity;

import java.util.ArrayList;

import java.util.List;

public class Page {

//1.当前是第几页

  private int pageIndex;

//2.每页记录数

  private int pageSize;

  

//3.总记录数

  private int totalrecords;

//4.总页数

  private int totalpages;

//5.真实数据集合 NewInfo

  private List<NewsInfo> list=new ArrayList<NewsInfo>();

public int getPageIndex() {

return pageIndex;

}

public void setPageIndex(int pageIndex) {

this.pageIndex = pageIndex;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public int getTotalrecords() {

return totalrecords;

}

public void setTotalrecords(int totalrecords) {

this.totalrecords = totalrecords;

}

public int getTotalpages() {

return totalpages;

}

public void setTotalpages(int totalpages) {

this.totalpages = totalpages;

}

public List<NewsInfo> getList() {

return list;

}

public void setList(List<NewsInfo> list) {

this.list = list;

}

}

3.0.2  UserInfo (用户实体类)

package cn.news.entity;

/**

 * 用户实体

 * @author Happy

*/

public class UserInfo {

    private int uid  ;

private String uname;

private String upwd ;

public int getUid() {

return uid;

}

public void setUid(int uid) {

this.uid = uid;

}

public String getUname() {

return uname;

}

public void setUname(String uname) {

this.uname = uname;

}

public String getUpwd() {

return upwd;

}

public void setUpwd(String upwd) {

this.upwd = upwd;

}

}

3.0.3  Topic (新闻主题实体类)

package cn.news.entity;

/**

 * 新闻分类

 * @author Happy

 */

public class Topic {

 private int tid;

 private String tname;

public int getTid() {

return tid;

}

public void setTid(int tid) {

this.tid = tid;

}

public String getTname() {

return tname;

}

public void setTname(String tname) {

this.tname = tname;

}

}

3.0.4  NewsInfo (新闻实体类)

package cn.news.entity;

import java.util.Date;

public class NewsInfo {

    private int  nid    ;

   private String  ntitle   ;

   private String  ncontent  ;

   private Date  ncreateda ;

   private int  ncateid   ;

   private Date  nmodifyda ;

   private String  npicpath  ;

   private String  nsummary  ;

   private String nauthor;

public String getNauthor() {

return nauthor;

}

public void setNauthor(String nauthor) {

this.nauthor = nauthor;

}

public int getNid() {

return nid;

}

public void setNid(int nid) {

this.nid = nid;

}

public String getNtitle() {

return ntitle;

}

public void setNtitle(String ntitle) {

this.ntitle = ntitle;

}

public String getNcontent() {

return ncontent;

}

public void setNcontent(String ncontent) {

this.ncontent = ncontent;

}

public Date getNcreateda() {

return ncreateda;

}

public void setNcreateda(Date ncreateda) {

this.ncreateda = ncreateda;

}

public int getNcateid() {

return ncateid;

}

public void setNcateid(int ncateid) {

this.ncateid = ncateid;

}

public Date getNmodifyda() {

return nmodifyda;

}

public void setNmodifyda(Date nmodifyda) {

this.nmodifyda = nmodifyda;

}

public String getNpicpath() {

return npicpath;

}

public void setNpicpath(String npicpath) {

this.npicpath = npicpath;

}

public String getNsummary() {

return nsummary;

}

public void setNsummary(String nsummary) {

this.nsummary = nsummary;

}

}

3.1BaseDAO连接数据库(工具类)

package cn.news.dao;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

/**

 * 连接数据库的工具类

 * @author Happy

 *

 */

public class BaseDAO {

    public static final String driver="com.mysql.jdbc.Driver";

    public static final String url="jdbc:mysql://localhost:3306/news";

    public static final String username="root";

    public static final String password="1234";

    

    public Connection con;

    public PreparedStatement ps;

    public ResultSet rs;

    

    //在静态代码块中。注册类

    static{

     try {

     //字符串对应的类装载到内存

Class.forName(driver);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

    }

    //1.获取连接接口

    public Connection getConection() throws SQLException{

     if (con==null||con.isClosed()) {

     con=DriverManager.getConnection(url, username, password);

}

     return con;

    }

    

    

    //1.1根据JNDI获取数据源,进而获得数据连接对象

    public Connection getConectionNew() throws SQLException{

     if (con==null||con.isClosed()) {

     Context ctx;

try {

ctx = new InitialContext();

 DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/news");

 System.out.println(ds+"==================");

 con= ds.getConnection();

} catch (NamingException e) {

}

    

}

     return con;

    }

    

    

    

    //2.关闭资源的方法

    public void closeResources() throws SQLException{

     rs.close();

     ps.close();

     con.close();

    }

    //3.执行增删改

    //select count(1) from userinfo where uname=? and pwd=?

    public int exeuteUpdate(String sql,Object...objs) throws SQLException{

     //1.根据con创建ps对象

     ps= con.prepareStatement(sql);

     //参数赋值

     for (int i = 0; i < objs.length; i++) {

ps.setObject(i+1, objs[i]);

}

     int count = ps.executeUpdate();

     return count;

    }

    

    

    //4.获取结果集ResultSet

    public ResultSet executeQuery(String sql,Object...objs) throws SQLException{

     ps= con.prepareStatement(sql);

     for (int i = 0; i < objs.length; i++) {

ps.setObject(i+1, objs[i]);

}

     rs=ps.executeQuery();

     return rs;

    }

}

3.2INewsDAO   来定义对新闻的接口

package cn.news.dao;

import java.sql.SQLException;

import java.util.List;

import cn.news.entity.NewsInfo;

import cn.news.entity.Topic;

public interface INewsDAO {

//1.检索所有新闻集合

   public List<NewsInfo> getAllNews() throws SQLException;

   

  //2.检索某一页的新闻数据

   public List<NewsInfo> getOnePageData(int pageindex,int pageSize) throws SQLException;

   

   //3.获取新闻总记录的方法

   public int getCount() throws SQLException;

   

   //添加新闻

   public  boolean addNews(NewsInfo news)throws Exception;

   //根据新闻类型查找tid

public  int getIdByTopicName(Topic topic)throws Exception;

  

//2.检索某一页的新闻数据

    public List<NewsInfo> getOnePageData(int tid,int pageindex,int pageSize) throws SQLException;

    //依照tid来获取所有的页数

    public int getCountById(int tid)throws SQLException;

}

3.3NewsInfoDAOImpl  对新闻接口的实现

package cn.news.dao.impl;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import org.junit.Test;

import cn.news.dao.BaseDAO;

import cn.news.dao.INewsDAO;

import cn.news.entity.NewsInfo;

import cn.news.entity.Topic;

public class NewsInfoDAOImpl extends BaseDAO implements INewsDAO {

//@Test

/*public void testAllNews() throws SQLException{

List<NewsInfo> list= getOnePageData(2,2);

for (NewsInfo item : list) {

System.out.println(item.getNtitle());

}

int count= getCount() ;

System.out.println(count);

}*/

//获取所有新闻条数

    public int getCount() throws SQLException {

int count=0;

String sql="select count(1) as mycount from newsinfo";

getConection();

rs=executeQuery(sql);

if (rs.next()) {

 count= rs.getInt("mycount");

}

return count;

}

//2.检索某一页的新闻数据

public List<NewsInfo> getOnePageData(int pageindex, int pageSize)

throws SQLException {

if (pageindex<=0) {

pageindex=1;

}

List<NewsInfo> list=new ArrayList<NewsInfo>();

    String sql="SELECT * FROM newsinfo LIMIT ?,? ";

    Object[] paras={(pageindex-1)*pageSize,pageSize};

    getConection();

    rs=executeQuery(sql, paras);

    while(rs.next()){

NewsInfo info=new NewsInfo();

info.setNtitle(rs.getString("ntitle"));

info.setNcreateda( rs.getDate("ncreatedate"));

list.add(info);

}

return list;

}

//获取新闻列表

public List<NewsInfo> getAllNews() throws SQLException {

List<NewsInfo> list=new ArrayList<NewsInfo>();

String sql="select * from newsinfo";

getConection();

rs=executeQuery(sql);

while(rs.next()){

NewsInfo info=new NewsInfo();

info.setNtitle(rs.getString("ntitle"));

info.setNcreateda( rs.getDate("ncreatedate"));

list.add(info);

}

return list;

}

@Test   

public void TestAdd() throws Exception{

NewsInfo news=new NewsInfo();

//无法存储中文

news.setNtitle("123");

Topic topic =new Topic();

topic.setTname("国内新闻");

 int ncateid=getIdByTopicName(topic);

 news.setNcontent("最忌刘");

 news.setNcateid(ncateid);

 news.setNauthor("瞿亮");

 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");

 //Date date=new Date();

 String nowTimeString= sdf.format(new Date());

 Date nowTime=sdf.parse(nowTimeString);

 news.setNcreateda(nowTime);

   boolean flag=addNews(news);

   if (flag) {

System.out.println("添加成功");

}

}

   //添加新闻

public boolean addNews(NewsInfo news) throws Exception {

getConection();

Topic topic =new Topic();

topic.getTname();

   int tid=getIdByTopicName(topic);

       boolean flag=false;

String sqlString="insert into  news.`newsinfo` (`ntitle`,`ncontent`,`ncreatedate`,`ncateid`,`nsummary`,`nauthor`) values(?,?,?,?,?,?)";

       int count=0;

       count=exeuteUpdate(sqlString,news.getNtitle(),news.getNcontent(),news.getNcreateda(),tid,news.getNsummary(),news.getNauthor());

       if (count>0) {

flag=true;

}

return flag;

}

//获取tid值的单测

/*@Test

public void TestGetId() throws Exception{

Topic topic =new Topic();

topic.setTname("国内新闻");

int id= getIdByTopicName(topic);

System.out.println(id);

}*/

public int getIdByTopicName(Topic topic) throws Exception {

  getConection();

       String sqlString="select tid from topic where tname=?";

//int exeuteUpdate(sqlString,topic.getTname());

       ResultSet rs = executeQuery(sqlString,topic.getTname());

      int tidString=0;

       while (rs.next()) {

        tidString= rs.getInt("tid");

   }

return tidString;

}

public List<NewsInfo> getOnePageData(int tid, int pageindex,

int pageSize) throws SQLException {

if (pageindex<1) {

pageindex=1;

}

getConection();

List<NewsInfo> list=new ArrayList<NewsInfo>();

String sqlString="select * from newsinfo  where ncateid =? limit ?,?";

 ResultSet rs=executeQuery(sqlString,tid,pageindex,pageSize);

 while(rs.next()){

NewsInfo info=new NewsInfo();

info.setNtitle(rs.getString("ntitle"));

info.setNcreateda( rs.getDate("ncreatedate"));

list.add(info);

}

return list;

}

@Test

public void TestCount() throws SQLException{

int num=getCountById(1);

System.out.println(num);

}

public int getCountById(int tid) throws SQLException {

getConection();

String sqlString="select COUNT(1) as mycount from newsinfo where ncateid =?";

 ResultSet rs=executeQuery(sqlString,tid);

int sum=0;

 while (rs.next()) {

 sum=rs.getInt("mycount");

}

return sum;

}

}

3.4 INewsInfoService  

package cn.news.service;

import java.sql.SQLException;

import java.util.List;

import cn.news.entity.NewsInfo;

import cn.news.entity.Topic;

public interface INewsInfoService {

   //获取所有新闻

   public List<NewsInfo> getAllNews() throws SQLException;

    //2.检索某一页的新闻数据

    public List<NewsInfo> getOnePageData(int pageindex,int pageSize) throws SQLException;

   

    //3.获取新闻总记录的方法

    public int getCount() throws SQLException;

   

    //添加新闻

    public  boolean addNews(NewsInfo news)throws Exception;

    //根据新闻类型查找tid

public  int getIdByTopicName(Topic topic)throws Exception;

//2.检索某一页的新闻数据

    public List<NewsInfo> getOnePageData(int tid,int pageindex,int pageSize) throws SQLException;

        //依照tid来获取所有的页数

    public int getCountById(int tid)throws SQLException;

}

3.5  NewsInfoServiceImpl  

package cn.news.service.impl;

import java.sql.SQLException;

import java.util.List;

import org.junit.Test;

import cn.news.dao.INewsDAO;

import cn.news.dao.impl.NewsInfoDAOImpl;

import cn.news.entity.NewsInfo;

import cn.news.entity.Topic;

import cn.news.service.INewsInfoService;

public class NewsInfoServiceImpl implements INewsInfoService {

    private INewsDAO dao=new NewsInfoDAOImpl();

    

public List<NewsInfo> getAllNews() throws SQLException {

return dao.getAllNews();

}

public List<NewsInfo> getOnePageData(int pageindex, int pageSize)

throws SQLException {

return dao.getOnePageData(pageindex, pageSize);

}

public int getCount() throws SQLException {

return dao.getCount();

}

public boolean addNews(NewsInfo news) throws Exception {

return dao.addNews(news);

}

public int getIdByTopicName(Topic topic) throws Exception {

return dao.getIdByTopicName(topic);

}

@Test

public void  TestGet() throws SQLException{

List<NewsInfo> list=getOnePageData(0, 1, 2);

for (NewsInfo newsInfo : list) {

System.out.println(newsInfo.getNcateid());

}

}

public List<NewsInfo> getOnePageData(int tid, int pageindex, int pageSize)

throws SQLException {

return dao.getOnePageData(tid, pageindex, pageSize);

}

public int getCountById(int tid) throws SQLException {

return dao.getCountById(tid);

}

}

4.1ITopicDAO  新闻主题的接口

package cn.news.dao;

import java.sql.SQLException;

import java.util.List;

import cn.news.entity.Topic;

public interface ITopicDAO{

   //查询所有的主题

public List<Topic> getAllTopics() throws SQLException;

//添加标题

    public boolean addTopic(Topic topic) throws SQLException;

     

    //区别是否有重复的标题

    public int isRepeat(Topic topic) throws SQLException;

    

    //更新标题信息

    public int updateTopic(Topic topic) throws SQLException;

    

    //删除标题

    public  boolean deleteTopic(Topic topic)throws SQLException;

    

    //按编号查询对应的新闻

    Public tring searchTopicNameById(Topic topic)throws SQLException;

    //

}

4.2TopicDAOImpl  对新闻主题接口的实现

package cn.news.dao.impl;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import org.junit.Test;

import cn.news.dao.BaseDAO;

import cn.news.dao.ITopicDAO;

import cn.news.entity.Topic;

public class TopicDAOImpl extends BaseDAO implements ITopicDAO{

/*@Test

public void testALl() throws SQLException{

List<Topic> list= getAllTopics();

for (Topic topic : list) {

System.out.println(topic.getTname());

}

}*/

//获取所有标题

public List<Topic> getAllTopics() throws SQLException {

List<Topic> list=new ArrayList<Topic>();

String sql="select * from topic";

getConection();

rs=executeQuery(sql);

while(rs.next()){

Topic topic=new Topic();

topic.setTid(rs.getInt("tid"));

topic.setTname(rs.getString("tname"));

list.add(topic);

}

closeResources();

return list;

}

@Test

public void TestAddTopic() throws SQLException{

Topic topic=new Topic();

topic.setTname("国内新闻");

boolean flag=addTopic(topic);

if (flag) {

System.out.println("添加成功");

}

}

//添加新闻

public boolean addTopic(Topic topic) throws SQLException {

getConection();

boolean flag=false;

String sqlString="insert into topic values(?,?)";

        int count=0;

        try {

count=exeuteUpdate(sqlString,topic.getTid(),topic.getTname());

if (count>0) {

flag=true;

}

} catch (Exception e) {

e.printStackTrace();

}

return flag;

}

@Test

public void TestRepeat() throws SQLException{

Topic topic=new Topic();

topic.setTname("国内新闻");

int count=isRepeat(topic);

if (count>0) {

System.out.println("重复!");

}

}

//是否存在重复的数据

public int isRepeat(Topic topic) throws SQLException {

getConection();

        String sqlString="select count(1)  from Topic where tname=?";

      int count=0;

     try {

    ResultSet rs = executeQuery(sqlString,topic.getTname());

  

    if(rs.next()){

    count=rs.getInt(1);

  }

    

   }catch(Exception e){

      e.printStackTrace();    

   }

        return count;

        }

/*@Test

public void TestUpdateTopic() throws SQLException{

Topic topic =new Topic();

topic.setTid(6);

topic.setTname("二奇");

int count=updateTopic(topic);

System.out.println(count);

}*/

//根据编号修改新闻名称

public int updateTopic(Topic topic) throws SQLException {

getConection();

 String sqlString="update topic set tname=? where tid=?";

       int count= exeuteUpdate(sqlString,topic.getTname(),topic.getTid());

       return count;

}

@Test

public void TestDeleteTopic() throws SQLException{

Topic topic=new Topic();

topic.setTid(0);

boolean flag= deleteTopic(topic);

if (flag) {

System.out.println("删除成功");

}else {

System.out.println("删除失败");

}

}

public boolean deleteTopic(Topic topic) throws SQLException {

getConection();

         String sqlString="delete from topic where tid=?";

         boolean flag=false;

        int count=exeuteUpdate(sqlString,topic.getTid());

        if (count>0) {

flag=true;

}

return flag;

}

@Test

public void searchTopicNameById() throws SQLException{

Topic topic =new Topic();

topic.setTid(1);

String nameString= searchTopicNameById(topic);

System.out.println("------------"+nameString);

}

public String searchTopicNameById(Topic topic) throws SQLException {

getConection();

String sqlString="select tname from topic where tid=?";

  ResultSet rs=executeQuery(sqlString,topic.getTid());

  String tempString=null ;

while (rs.next()) {

tempString=rs.getString("tname");

}

return tempString;    //返回为空对象的话,就说明数据库中没有这条数据

}

}

4.3 ITopicService

package cn.news.service;

import java.sql.SQLException;

import java.util.List;

import cn.news.entity.Topic;

public interface ITopicService {

 //查询所有的主题

     public List<Topic> getAllTopics() throws SQLException;

     

   //添加标题

     public boolean addTopic(Topic topic) throws SQLException;

      

     //区别是否有重复的标题

     public int isRepeat(Topic topic) throws SQLException;

     

     //更新标题信息

     public int updateTopic(Topic topic) throws SQLException;

     

     //删除标题

     public  boolean deleteTopic(Topic topic)throws SQLException;

     

     //按编号查询对应的新闻

     public  String searchTopicNameById(Topic topic)throws SQLException;

}

4.4TopicServiceImpl

package cn.news.service.impl;

import java.sql.SQLException;

import java.util.List;

import cn.news.dao.ITopicDAO;

import cn.news.dao.impl.TopicDAOImpl;

import cn.news.entity.Topic;

import cn.news.service.ITopicService;

public class TopicServiceImpl implements ITopicService{

//植入dao接口

private ITopicDAO dao=new TopicDAOImpl();

public List<Topic> getAllTopics() throws SQLException {

return dao.getAllTopics();

}

public boolean addTopic(Topic topic) throws SQLException {

return dao.addTopic(topic);

}

public int isRepeat(Topic topic) throws SQLException {

return dao.isRepeat(topic);

}

public int updateTopic(Topic topic) throws SQLException {

return dao.updateTopic(topic);

}

public boolean deleteTopic(Topic topic) throws SQLException {

return dao.deleteTopic(topic);

}

public String searchTopicNameById(Topic topic) throws SQLException {

return dao.searchTopicNameById(topic);

}

}

5.1UserInfoDAO  定义用户接口

package cn.news.dao;

import java.sql.SQLException;

import cn.news.entity.UserInfo;

//用户接口

public interface IUserInfoDAO {

   //登录

public boolean isLogin(UserInfo info) throws SQLException;

}

5.2UserInfoDAOImpl 对用户接口的实现

package cn.news.dao.impl;

import java.sql.SQLException;

import org.junit.Test;

import cn.news.dao.BaseDAO;

import cn.news.dao.IUserInfoDAO;

import cn.news.entity.UserInfo;

public class UserInfoDAOImpl extends BaseDAO implements IUserInfoDAO {

@Test

public void loginTest() throws SQLException{

UserInfo info =new UserInfo();

info.setUname("admin2");

info.setUpwd("admin");

 boolean flag= isLogin(info);

 if (flag) {

System.out.println("login success!");

}

}

public boolean isLogin(UserInfo info) throws SQLException {

boolean flag=false;

String sql="select count(1) as mycount from userinfo where uname=? and upwd=?";

Object[] paras={info.getUname(),info.getUpwd()};

getConection();

rs=executeQuery(sql,paras);

if (rs.next()) {

int count = rs.getInt("mycount");

if (count>0) {

//登录成功

flag=true;

}

}

closeResources();

return flag;

}

}

5.3  IUserInfoService  

package cn.news.service;

import java.sql.SQLException;

import cn.news.entity.UserInfo;

public interface IUserInfoService {

  //登录

public boolean isLogin(UserInfo info) throws SQLException;

}

5.4UserinfoServiceImpl

package cn.news.service.impl;

import java.sql.SQLException;

import cn.news.dao.IUserInfoDAO;

import cn.news.dao.impl.UserInfoDAOImpl;

import cn.news.entity.UserInfo;

import cn.news.service.IUserInfoService;

public class UserInfoServiceImpl implements IUserInfoService{

//植入dao接口

private IUserInfoDAO dao=new UserInfoDAOImpl();

public boolean isLogin(UserInfo info) throws SQLException {

return dao.isLogin(info);

}

}

6.servlet

6.1 UserInfoServlet

package cn.news.serlvet;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.SQLException;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import cn.news.dao.impl.NewsInfoDAOImpl;

import cn.news.dao.impl.TopicDAOImpl;

import cn.news.entity.NewsInfo;

import cn.news.entity.Page;

import cn.news.entity.Topic;

import cn.news.entity.UserInfo;

import cn.news.service.INewsInfoService;

import cn.news.service.IUserInfoService;

import cn.news.service.impl.NewsInfoServiceImpl;

import cn.news.service.impl.UserInfoServiceImpl;

public class UserInfoServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost( request,  response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//准备新闻数据

INewsInfoService newsservice=new NewsInfoServiceImpl();

Page page=new Page();

//页大小

int pagesize=5;

page.setPageSize(pagesize);

//3.总记录数

int count=0;

try {

count = newsservice.getCount();

} catch (SQLException e1) {

e1.printStackTrace();

}

page.setTotalrecords(count);

             String tempString= request.getParameter("tid");

int sum=0;

if (tempString!=null) {

sum=Integer.parseInt(tempString.trim());

}

System.out.println(count);

int pageCount=0;

  if (sum!=0) {    //说明选择了类型

try {

count=  newsservice.getCountById(sum);

} catch (SQLException e) {

e.printStackTrace();

}

  }else {

//4.总页数

  try {

count=newsservice.getCount();

} catch (SQLException e) {

e.printStackTrace();

}

  }

  

  //总页数

  pageCount=count%pagesize==0 ? count/pagesize : count/pagesize+1;

  System.out.println(pagesize);

  System.out.println("##############总页数"+pageCount);

page.setTotalpages(pageCount);

       

//2.当前是第几页

int pageIndex=1;

String pagesIndex =request.getParameter("pageIndex");

try {

if (request.getParameter("pageIndex")!=null) {

pageIndex=Integer.parseInt(pagesIndex);

System.out.println("当前index"+pageIndex);

}

if (pageIndex<1) {

pageIndex=1;

}else if (pageIndex>pageCount) {

pageIndex=pageCount;

}

} catch (Exception e) {

pageIndex=1;

}

 System.out.println("当前页:"+pageIndex);

page.setPageIndex(pageIndex);

//3.page中的List集合赋值

     //新闻类型下的分页

           TopicDAOImpl impl=new TopicDAOImpl();

           NewsInfoDAOImpl news=new NewsInfoDAOImpl();

           List<NewsInfo> list=null;

           System.out.println("是否选择新闻类型:"+sum);

       if (sum!=0) {   //点击选择新闻类型的时候

        System.out.println("if");

        try {

        request.setAttribute("tid",sum);

        list=news.getOnePageData(sum,page.getPageIndex(),page.getPageSize());

System.out.println(list.size());

        } catch (SQLException e) {

e.printStackTrace();

}

}else{

System.out.println("else");

try {

list = newsservice.getOnePageData(page.getPageIndex(), page.getPageSize());

} catch (SQLException e) {

e.printStackTrace();

}

}

       page.setList(list);

//放入request作用域

request.setAttribute("onePage",page);    

     

     

  //logout

String action=request.getParameter("action");

if ("logout".equals(action)) {

request.getSession().removeAttribute("uname");

//2.跳转到index

/*response.sendRedirect("/NewsManagerSystem/index.jsp");*/

request.getRequestDispatcher("/index.jsp").forward(request, response);

}else if("login".equals(action)) {

//code write here

//1.解决乱码

request.setCharacterEncoding("utf-8");

//2.解析前台传递的表单数据

String uname=request.getParameter("uname");

String upwd=request.getParameter("upwd");

//拼接成一个用户对象

UserInfo info=new UserInfo();

info.setUname(uname);

info.setUpwd(upwd);

IUserInfoService service=new UserInfoServiceImpl();

try {

boolean flag = service.isLogin(info);

if (flag) {  //登录成功

list=newsservice.getOnePageData(pageIndex, pagesize);

 page.setList(list);

 request.setAttribute("onePage",page);

//3.记录session

request.getSession().setAttribute("uname",uname);

//4.转发或者重定向   转发不需要加项目名称

request.getRequestDispatcher("/newspages/admin.jsp").forward(request, response);

}else {

List<Topic> topiclist=null;

try {

 topiclist= impl.getAllTopics();

} catch (SQLException e) {

e.printStackTrace();

}

 request.setAttribute("list",topiclist);

list=newsservice.getOnePageData(pageIndex, pagesize);

 page.setList(list);

 request.setAttribute("onePage",page);

request.getRequestDispatcher("/index.jsp").forward(request, response);

}

} catch (SQLException e) {

e.printStackTrace();

}

}else {    //所有的新闻类型

       List<Topic> topiclist=null;

try {

 topiclist= impl.getAllTopics();

} catch (SQLException e) {

e.printStackTrace();

}

 request.setAttribute("list",topiclist);

//第一次访问首页

request.getRequestDispatcher("/index.jsp").forward(request, response);

}

}

}

  

原文地址:https://www.cnblogs.com/1234wwww/p/6854911.html