zoukankan      html  css  js  c++  java
  • JavaWeb_(SSH论坛)_四、页面显示

    基于SSH框架的小型论坛项目  

      一、项目入门  传送门

      二、框架整合  传送门

      三、用户模块  传送门

      四、页面显示  传送门

      五、帖子模块  传送门

      六、点赞模块  传送门

      七、辅助模块  传送门

    帖子表与回复表

        编写帖子模块
                添加帖子
                查看帖子
                    查看帖子
                        页面的改变
                            -用户
                            -文本
                        内容去掉br标签
                        用户头像
                    分页
                        引入资源
                        分析
                            pageSize
                            currentPage
                            totalCount
                            totalPage
                            
                            所需要的参数:    
                                *起始索引
                                
                                pageSize(自己定义的)
                                currentPage
                                totalCount    select count(*) from paste
                                总页数 = 总条数/每页显示条数 + 1     Math.ceil()        2.3      3
                                    4        10      3            3.333                3        3
                                    4        12        3        4                         3.00001 4
                                
                            select * from paste limit ?,?   
                            第一个问号:起始索引
                            页数    页面大小     起始索引
                                1    2            0
                                2    2            2
                                3    2            4
                                
                                n   2         (第一个数-1)* 第二个数  
                            第二个问号:查询多少数据(pageSize)
                            通过页面分析:
                                currentPage
                                尾页:总页数
                        编码
                        
                        
                回复帖子
                    1)进入帖子
                        点击标题进入detail.jsp
                        
                        
                        
                    2)回复帖子
    帖子模块

      关系分析图

       数据库中贴子表的创建

        create table paste(
            id varchar(50)primary key,
            title varchar(1000) not null,
            content varchar(3000) not null,
            offer int not null,
            ansnum int default 0,
            createtime varchar(100) not null,
            glanceover int default 0,
            solve int default 0,
            isdelete int default 0,
            
            answerid varchar(50),
            userid varchar(50)
            
    )

       回复表

        create table answer(
            id varchar(50) primary key,
            userid varchar(50) not null,
            pasteid varchar(50) not null,
            content varchar(3000) not null,
            anstime varchar(100) not null,
            agree int default 0,
            solve int default 0
        
        )

    添加帖子

      add.jsp中添加帖子表单路径

    <form action="${pageContext.request.contextPath}/PasteAction_addPaste">

      使用模型驱动,创建一个PasteAction.class

    public class PasteAction extends ActionSupport implements ModelDriven<Paste>

      创建封装PasteAction.class(Web层)、PasteService.class(Service层)、PasteDao.class(Dao层)数据

    package com.Gary.web;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import com.Gary.domain.Paste;
    import com.Gary.domain.User;
    import com.Gary.service.PasteService;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    public class PasteAction extends ActionSupport implements ModelDriven<Paste> {
    
        public Paste paste = new Paste();
        private PasteService pasteService;
    
        public String addPaste() throws Exception {
            User user = (User)ActionContext.getContext().getSession().get("user");
            
            if(user==null)
            {
                ActionContext.getContext().put("error", "只有登陆之后才可以发帖子!!");
                return "error";
            }
            
            //private Integer ansnum;
            paste.setAnsnum(0);
            //private String createtime;
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String createtime = format.format(date);
            paste.setCreatetime(createtime);
            //private Integer glanceover;
            paste.setGlanceover(0);
            //是否结帖  0未结 1结束
            //private Integer solve;
            paste.setSolve(0);
            //private Integer isdelete;
            paste.setIsdelete(0);
            //private User user;
            
            paste.setUser(user);
            
            pasteService.addPaste(paste);
            
            
            //重定向到主页
            return "toIndex";
        }
    
        public PasteService getPasteService() {
            return pasteService;
        }
    
        public void setPasteService(PasteService pasteService) {
            this.pasteService = pasteService;
        }
    
        @Override
        public Paste getModel() {
            // TODO Auto-generated method stub
            return paste;
        }
    
    }
    PasteAction.class
    package com.Gary.service;
    
    import com.Gary.dao.PasteDao;
    import com.Gary.domain.Paste;
    
    public class PasteService {
    
        private PasteDao pasteDao;
        
        public PasteDao getPasteDao() {
            return pasteDao;
        }
    
        public void setPasteDao(PasteDao pasteDao) {
            this.pasteDao = pasteDao;
        }
    
        public void addPaste(Paste paste) {
            
            pasteDao.addPaste(paste);
            
        }
    
    }
    PasteService.class
    package com.Gary.dao;
    
    import org.hibernate.Session;
    import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
    
    import com.Gary.domain.Paste;
    
    public class PasteDao extends HibernateDaoSupport{
        
        public void addPaste(Paste paste) {
            
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            session.save(paste);
        }
        
    }
    PasteDao.class

       添加帖子属性

        private String id;
        private String title;
        private String content;
        private Integer offer;
        private Integer ansnum;
        private String createtime;
        private Integer glanceover;
        //是否结帖
        private Integer solve;
        private Integer delete;
    package com.Gary.domain;
    
    public class Paste {
        
        private String id;
        private String title;
        private String content;
        private Integer offer;
        private Integer ansnum;
        private String createtime;
        private Integer glanceover;
        //是否结帖
        private Integer solve;
        private Integer delete;
        
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public Integer getOffer() {
            return offer;
        }
        public void setOffer(Integer offer) {
            this.offer = offer;
        }
        public Integer getAnsnum() {
            return ansnum;
        }
        public void setAnsnum(Integer ansnum) {
            this.ansnum = ansnum;
        }
        public String getCreatetime() {
            return createtime;
        }
        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }
        public Integer getGlanceover() {
            return glanceover;
        }
        public void setGlanceover(Integer glanceover) {
            this.glanceover = glanceover;
        }
        public Integer getSolve() {
            return solve;
        }
        public void setSolve(Integer solve) {
            this.solve = solve;
        }
        public Integer getDelete() {
            return delete;
        }
        public void setDelete(Integer delete) {
            this.delete = delete;
        }
    
        
    }
    Paste.java

      回复帖属性

    public class PasteService {
    
        private PasteDao pasteDao;
        
        public PasteDao getPasteDao() {
            return pasteDao;
        }
    
        public void setPasteDao(PasteDao pasteDao) {
            this.pasteDao = pasteDao;
        }
    
        public void addPaste(Paste paste) {
            
            pasteDao.addPaste(paste);
            
        }
    
    }

      配置一对多属性

      Paste中配置用户帖子

      private User user;
        
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }

      User.class中代表该用户发表的所有帖子

      private Set<Paste> pasteSet = new HashSet<Paste>();
        
        public Set<Paste> getPasteSet() {
            return pasteSet;
        }
    
        public void setPasteSet(Set<Paste> pasteSet) {
            this.pasteSet = pasteSet;
        }

      User.hbm.xml中配置外键

            <set name="pasteSet">
                <!-- 外键列名 -->
                <key column="userid"></key>
                <one-to-many class="Paste"/>
            </set>

      Paste.hbx.xml中配置一对多的关系

    <many-to-one name="user" class="User" column="userid" insert="true"></many-to-one>

      配置applicationContext.xml

    <!-- 配置Action -->
        <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype">
            <property name="userService" ref="userService"></property>
        </bean>
        <bean name="pasteAction" class="com.Gary.web.PasteAction" scope="prototype">
            <property name="pasteService" ref="userService"></property>
        </bean>
        
        <!-- 配置Service -->
        <bean name="userService" class="com.Gary.service.UserService">
            <property name="userDao" ref="userDao"></property>
        </bean>
        <bean name="pasteService" class="com.Gary.service.PasteService">
            <property name="pasteDao" ref="pasteDao"></property>
        </bean>
        
        <!-- 配置Dao -->
        <bean name="userDao" class="com.Gary.dao.UserDao">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <bean name="pasteDao" class="com.Gary.dao.PasteDao">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>

      登陆成功后,测试发帖功能

    package com.Gary.dao;
    
    import org.hibernate.Session;
    import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
    
    import com.Gary.domain.Paste;
    
    public class PasteDao extends HibernateDaoSupport{
        
        public void addPaste(Paste paste) {
            
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            session.save(paste);
        }
        
    }
    PasteDao.java
    package com.Gary.domain;
    
    public class Paste {
        
        private String id;
        private String title;
        private String content;
        private Integer offer;
        private Integer ansnum;
        private String createtime;
        private Integer glanceover;
        //是否结帖
        private Integer solve;
        private Integer isdelete;
        
        private User user;
        
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }
        
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public Integer getOffer() {
            return offer;
        }
        public void setOffer(Integer offer) {
            this.offer = offer;
        }
        public Integer getAnsnum() {
            return ansnum;
        }
        public void setAnsnum(Integer ansnum) {
            this.ansnum = ansnum;
        }
        public String getCreatetime() {
            return createtime;
        }
        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }
        public Integer getGlanceover() {
            return glanceover;
        }
        public void setGlanceover(Integer glanceover) {
            this.glanceover = glanceover;
        }
        public Integer getSolve() {
            return solve;
        }
        public void setSolve(Integer solve) {
            this.solve = solve;
        }
        public Integer getIsdelete() {
            return isdelete;
        }
        public void setIsdelete(Integer isdelete) {
            this.isdelete = isdelete;
        }
    
    
        
    }
    Paste.java
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package = "com.Gary.domain">
        <class name="Paste" table="paste">
            <id name="id">
                <generator class="uuid"></generator>
            </id>
            
        <!--private String title;
            private String content;
            private Integer offer;
            private Integer ansnum;
            private String createtime;
            private Integer glanceover;
            //是否结帖
            private Integer solve;
            private Integer isdelete;-->
    
    
             <property name="title" column="title"></property>
             <property name="content" column="content"></property>
             <property name="offer" column="offer"></property>
             <property name="ansnum" column="ansnum"></property>
             <property name="createtime" column="createtime"></property>
             <property name="glanceover" column="glanceover"></property>
             <property name="solve" column="solve"></property>
             <property name="isdelete" column="isdelete"></property>
        
        
    <many-to-one name="user" class="User" column="userid" insert="true"></many-to-one>
        
        
        
        </class>
    </hibernate-mapping>
    Paste.hbm.xml
    package com.Gary.service;
    
    import com.Gary.dao.PasteDao;
    import com.Gary.domain.Paste;
    
    public class PasteService {
    
        private PasteDao pasteDao;
        
        public PasteDao getPasteDao() {
            return pasteDao;
        }
    
        public void setPasteDao(PasteDao pasteDao) {
            this.pasteDao = pasteDao;
        }
    
        public void addPaste(Paste paste) {
            
            pasteDao.addPaste(paste);
            
        }
    
    }
    PasteService.java
    package com.Gary.web;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import com.Gary.domain.Paste;
    import com.Gary.domain.User;
    import com.Gary.service.PasteService;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    public class PasteAction extends ActionSupport implements ModelDriven<Paste> {
    
        public Paste paste = new Paste();
        private PasteService pasteService;
    
        public String addPaste() throws Exception {
            User user = (User)ActionContext.getContext().getSession().get("user");
            
            if(user==null)
            {
                ActionContext.getContext().put("error", "只有登陆之后才可以发帖子!!");
                return "error";
            }
            
            //private Integer ansnum;
            paste.setAnsnum(0);
            //private String createtime;
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String createtime = format.format(date);
            paste.setCreatetime(createtime);
            //private Integer glanceover;
            paste.setGlanceover(0);
            //是否结帖  0未结 1结束
            //private Integer solve;
            paste.setSolve(0);
            //private Integer isdelete;
            paste.setIsdelete(0);
            //private User user;
            
            paste.setUser(user);
            
            pasteService.addPaste(paste);
            
            
            //重定向到主页
            return "toIndex";
        }
    
        public PasteService getPasteService() {
            return pasteService;
        }
    
        public void setPasteService(PasteService pasteService) {
            this.pasteService = pasteService;
        }
    
        @Override
        public Paste getModel() {
            // TODO Auto-generated method stub
            return paste;
        }
    
    }
    PasteAction.java
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!-- 配置数据源 -->
        <bean name="dataSource"
            class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="jdbcUrl" value="jdbc:mysqL:///garyssh_forum"></property>
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="user" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>
    
        <!-- 配置sessionFactory -->
        <bean name="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sqp">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                </props>
            </property>
    
            <property name="mappingDirectoryLocations"
                value="classpath:com/Gary/domain"></property>
    
        </bean>
    
        <!-- 配置事务的核心管理器 -->
        <bean name="transactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    
        <!-- 通知 -->
        <tx:advice id="advice"
            transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*" />
            </tx:attributes>
        </tx:advice>
    
        <!-- 织入 -->
        <aop:config>
            <!-- 切入点 -->
            <aop:pointcut
                expression="execution(* com.Gary.service.*.*(..))" id="pc" />
            <!-- 配置切面 切入点+通知 -->
            <aop:advisor advice-ref="advice" pointcut-ref="pc" />
        </aop:config>
    
        <!-- 配置Action -->
        <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype">
            <property name="userService" ref="userService"></property>
        </bean>
        <bean name="pasteAction" class="com.Gary.web.PasteAction" scope="prototype">
            <property name="pasteService" ref="pasteService"></property>
        </bean>
        <!-- 配置Service -->
        <bean name="userService" class="com.Gary.service.UserService">
            <property name="userDao" ref="userDao"></property>
        </bean>
        <bean name="pasteService" class="com.Gary.service.PasteService">
            <property name="pasteDao" ref="pasteDao"></property>
        </bean>
        <!-- 配置Dao -->
        <bean name="userDao" class="com.Gary.dao.UserDao">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <bean name="pasteDao" class="com.Gary.dao.PasteDao">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
    </beans>
    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
    
        <!--开启动态方法调用 -->
        <constant name="struts.devMode" value="true"></constant>
        <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    
        <!-- 告诉struts不用自己创建Action,Spring来帮你创建 -->
        <constant name="struts.objectFactory" value="spring"></constant>
        <package name="Gary_SSHForum" namespace="/" extends="struts-default">
            <!-- 允许全部方法 -->
            <global-allowed-methods>regex:.*</global-allowed-methods>
            <action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}">
                <result name="toLogin" type="redirect">/login.jsp</result>
                <result name="login">/login.jsp</result>
                <result name="toIndex" type="redirect">/index.jsp</result>
                <result name="error">/login.jsp</result>
                <result name="toRegisterSuccess" type="redirect">/registerSuccess.jsp</result>
            </action>
        
        <action name="PasteAction_*" class="com.Gary.web.PasteAction" method="{1}">
            <result name="toIndex" type="redirect">/index.jsp</result>
            <result name="error">/login.jsp</result>
        </action>
        
        </package>
    
    </struts>
    struts.xml

      完善发帖

      对用户登陆进行判断

          <!--描述:未登录的样子-->
                <s:if test="#session.user!=null">
                    <a class="avatar" href=""> <img src="res/images/avatar/11.jpg"> <cite><s:property value="#session.user.username" /></cite>
                    </a>
                    <div class="nav">
                        <a href=""> <i class="iconfont icon-tuichu" style="top: 0; font-size: 22px;"></i> 退出
                        </a>
                    </div>
                </s:if>
                <!--描述:未登录的样子-->
                <s:if test="#session.user==null">
                    <a class="iconfont icon-touxiang layui-hide-xs" style="margin-top: 4px; display: inline-block;"> </a>
                    <div class="nav" style="font-size: 14px; color: white; margin-top: -5px; margin-left: 1px;" />
                    <a href="login.jsp" target="_parent">登录</a>
                    <a href="register.jsp" target="_parent">注册</a>
                </s:if>

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="res/layui/css/layui.css">
    <link rel="stylesheet" href="res/css/global.css">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <script src="res/layui/layui.js"></script>
    <style>
    </style>
    </head>
    <body>
        <div class="dvhead">
            <div class="dvlogo">
                <a href="index.jsp" target="_parent" fount-size="34px">论坛</a>
            </div>+
            <div class="dvsearch">Cynical丶Gary</div>
            <div class="nav-user" style="top: 0px; right: 100px;">
    
                <!--描述:未登录的样子-->
                <s:if test="#session.user!=null">
                    <a class="avatar" href=""> <img src="res/images/avatar/11.jpg"> <cite><s:property value="#session.user.username" /></cite>
                    </a>
                    <div class="nav">
                        <a href=""> <i class="iconfont icon-tuichu" style="top: 0; font-size: 22px;"></i> 退出
                        </a>
                    </div>
                </s:if>
                <!--描述:未登录的样子-->
                <s:if test="#session.user==null">
                    <a class="iconfont icon-touxiang layui-hide-xs" style="margin-top: 4px; display: inline-block;"> </a>
                    <div class="nav" style="font-size: 14px; color: white; margin-top: -5px; margin-left: 1px;" />
                    <a href="login.jsp" target="_parent">登录</a>
                    <a href="register.jsp" target="_parent">注册</a>
                </s:if>
            </div>
        </div>
    </body>
    </html>
    head.jsp

    查询帖子

      分析查询帖子所有功能

      查询帖子所有信息,用一个List<>泛型去接收

      Web层创建一个GetDataAction.java去Service中查询数据

    public class GetDataAction extends ActionSupport{
    
        private PasteService pasteService;
    
        
        public String getAllPaste() throws Exception {
            
            List<Paste> pasteList = pasteService.findAllPaste();
            
            ActionContext.getContext().put("pasteList", pasteList);
            
            return "index";
        }
    
        public PasteService getPasteService() {
            return pasteService;
        }
    
        public void setPasteService(PasteService pasteService) {
            this.pasteService = pasteService;
        }
        
    }

       Service层中创建PasteService.java在Dao层查询数据库

    public class PasteService {
    
        private PasteDao pasteDao;
        
        
        public PasteDao getPasteDao() {
            return pasteDao;
        }
    
        public List<Paste> findAllPaste() {
        
            return pasteDao.findAllPaste();
        
        }
        
        public void setPasteDao(PasteDao pasteDao) {
            this.pasteDao = pasteDao;
        }
    
        public void addPaste(Paste paste) {
            
            pasteDao.addPaste(paste);
            
        }
    
    }

       Dao层创建PasteDao.java在使用HQL在数据库中查询数据

    public class PasteDao extends HibernateDaoSupport{
        
        public void addPaste(Paste paste) {
            
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            session.save(paste);
        }
    
        
        //HQL
        public List<Paste> findAllPaste() {
            
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            
            //书写SQL语句
            String hql = "form com.Gary.domain.Paste";
            Query query = session.createQuery(hql);
            
            List<Paste> list = query.list();
            
            return list;
        }
        
    }

      

    package com.Gary.web;
    
    import java.util.List;
    
    import com.Gary.domain.Paste;
    import com.Gary.service.PasteService;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class GetDataAction extends ActionSupport{
    
        private PasteService pasteService;
    
        
        public String getAllPaste() throws Exception {
            
            List<Paste> pasteList = pasteService.findAllPaste();
            
            ActionContext.getContext().put("pasteList", pasteList);
            
            return "index";
        }
    
        public PasteService getPasteService() {
            return pasteService;
        }
    
        public void setPasteService(PasteService pasteService) {
            this.pasteService = pasteService;
        }
        
    }
    GetDataAction.java
    package com.Gary.service;
    
    import java.util.List;
    
    import com.Gary.dao.PasteDao;
    import com.Gary.domain.Paste;
    
    public class PasteService {
    
        private PasteDao pasteDao;
        
        
        public PasteDao getPasteDao() {
            return pasteDao;
        }
    
        public List<Paste> findAllPaste() {
        
            return pasteDao.findAllPaste();
        
        }
        
        public void setPasteDao(PasteDao pasteDao) {
            this.pasteDao = pasteDao;
        }
    
        public void addPaste(Paste paste) {
            
            pasteDao.addPaste(paste);
            
        }
    
    }
    PasteService.java
    package com.Gary.dao;
    
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.query.Query;
    import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
    
    import com.Gary.domain.Paste;
    
    public class PasteDao extends HibernateDaoSupport{
        
        public void addPaste(Paste paste) {
            
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            session.save(paste);
        }
    
        
        //HQL
        public List<Paste> findAllPaste() {
            
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            
            //书写SQL语句
            String hql = "form com.Gary.domain.Paste";
            Query query = session.createQuery(hql);
            
            List<Paste> list = query.list();
            
            return list;
        }
        
    }
    PasteDao.java

      配置struts.xml

        <action name="GetDataAction_*" class="com.Gary.web.GetDataAction" method="{1}">
            <result name="index">/index.jsp</result>
        </action>

      配置application.xml

        <!-- 配置Action -->
        <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype">
            <property name="userService" ref="userService"></property>
        </bean>
        <bean name="pasteAction" class="com.Gary.web.PasteAction" scope="prototype">
            <property name="pasteService" ref="pasteService"></property>
        </bean>
        <bean name="getDataAction" class="com.Gary.web.GetDataAction" scope="prototype">
            <property name="pasteService" ref="pasteService"></property>
        </bean>

      测试发帖,动态编译获得数据库中的帖子数据

    <div class="tab">
                    <s:iterator value="pasteList" var="paste">
                        <div class="dvques">
                            <div class="quesCount">
                                <div class="count">8</div>
                                <div class="ques">
                                    <s:property value="#paste.ansnum" />
                                </div>
                            </div>
                            <div class="quesContent">
                                <div class="quesTitle">
                                    <s:property value="#paste.offer" />
                                    <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span>
                                </div>
                                <div class="qContent"><s:property value="#paste.content"/></div>
                                <div class="tags">
                                    <span class="tag">excel</span><span class="tag">程序</span>
                                </div>
                                <div class="quesUser">
                                    <image src="images/0.gif" class="imguser" />
                                    <div class="userName">
                                        <s:property value="#paste.user.username"/>
                                        <div class="liulan">浏览(<s:property value="#paste.createtime"/>) <s:property value="#paste.user.username"/></div>
                                    </div>
    
                                </div>
                            </div>
                        </div>
                    </s:iterator>
    
                </div>

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@taglib uri="/struts-tags" prefix="s"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="css/index.css">
    </head>
    <div class="dvhead">
        <div class="dvlogo">
            <a href="index.html">你问我答</a>
        </div>
        <div class="dvsearch">10秒钟注册账号,找到你的同学</div>
        <div class="dvreg">
            已有账号,立即&nbsp;<a href="login.html">登录</a>
        </div>
    </div>
    <div class="dvContent">
        <div class="dvquesleft">
    
            <div class="dvqstitle">
                <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">问答</span> <span class="back"><ab href="">《《返回上一页</a></span>
            </div>
            <div class="dvtabhead">
                <div class="tabheads tabcurrent">全部问题</div>
                <div class="tabheads">我的问题</div>
                <div class="tabheads">关注问题</div>
                <div class="tabheads">问题标签</div>
            </div>
            <div class="tabContent">
                <div class="dvtags">
                    <a class="curenttag">待解决</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解决</a>
                </div>
                <div class="tab">
                    <s:iterator value="pasteList" var="paste">
                        <div class="dvques">
                            <div class="quesCount">
                                <div class="count">8</div>
                                <div class="ques">
                                    <s:property value="#paste.ansnum" />
                                </div>
                            </div>
                            <div class="quesContent">
                                <div class="quesTitle">
                                    <s:property value="#paste.offer" />
                                    <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span>
                                </div>
                                <div class="qContent"><s:property value="#paste.content"/></div>
                                <div class="tags">
                                    <span class="tag">excel</span><span class="tag">程序</span>
                                </div>
                                <div class="quesUser">
                                    <image src="images/0.gif" class="imguser" />
                                    <div class="userName">
                                        <s:property value="#paste.user.username"/>
                                        <div class="liulan">浏览(<s:property value="#paste.createtime"/>) <s:property value="#paste.user.username"/></div>
                                    </div>
    
                                </div>
                            </div>
                        </div>
                    </s:iterator>
    
                </div>
                <div class="tab hidden">2</div>
                <div class="tab hidden">3</div>
                <div class="tab hidden">4</div>
            </div>
        </div>
        <div class="dvquesright">
            <div>
                <buton class="btnques" onclick="location.href='add.html'">提个问题</buton>
            </div>
            <div class="dvorder">
                <div class="orderTitle">专家排行榜</div>
                <div class="users">
                    <image class="userface" src="images/0.gif" />
                    <div class="dvuser">
                        <div class="userTitle">陈有龙</div>
                        <div class="userdeital">大牛6级 豆:14006</div>
                    </div>
                </div>
                <div class="users">
                    <image class="userface" src="images/1.gif" />
                    <div class="dvuser">
                        <div class="userTitle">陈有龙</div>
                        <div class="userdeital">大牛6级 豆:14006</div>
                    </div>
                </div>
                <div class="users">
                    <image class="userface" src="images/2.gif" />
                    <div class="dvuser">
                        <div class="userTitle">陈有龙</div>
                        <div class="userdeital">大牛6级 豆:14006</div>
                    </div>
                </div>
                <div class="users">
                    <image class="userface" src="images/3.gif" />
                    <div class="dvuser">
                        <div class="userTitle">陈有龙</div>
                        <div class="userdeital">大牛6级 豆:14006</div>
                    </div>
                </div>
                <div class="users">
                    <image class="userface" src="images/4.gif" />
                    <div class="dvuser">
                        <div class="userTitle">陈有龙</div>
                        <div class="userdeital">大牛6级 豆:14006</div>
                    </div>
                </div>
                <div class="users">
                    <image class="userface" src="images/5.gif" />
                    <div class="dvuser">
                        <div class="userTitle">陈有龙</div>
                        <div class="userdeital">大牛6级 豆:14006</div>
                    </div>
                </div>
                <div class="users">
                    <image class="userface" src="images/6.gif" />
                    <div class="dvuser">
                        <div class="userTitle">陈有龙</div>
                        <div class="userdeital">大牛6级 豆:14006</div>
                    </div>
                </div>
    
    
            </div>
    
        </div>
    
    </div>
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        $(function() {
    
            $(".tabheads").click(
                    function() {
                        $(".tabheads").removeClass("tabcurrent")
                                .eq($(this).index()).addClass("tabcurrent");
                        $(".tab").hide().eq($(this).index()).show();
                    });
        });
    </script>
    <body>
    </body>
    </html>
    index.jsp

      修改帖子内容样式

        <div class="quesContent" style="px;height:px;overflow:hidden;white-space:normal;text-overflow:ellipsis;">                

      修改帖子头像位置样式

    <div class="quesUser" style="margin-top:28px;margin-right:20px">

      

      设置人物头像

      在用户注册时String register()方法中随机设置用户头像

      UserAction.java

        public String register() throws Exception {
            // TODO Auto-generated method stub
        
            //没有的数据手动封装
            user.setState(0);
            user.setCode(UUID.randomUUID().toString());
            Random r = new Random();
            user.setImage("/images/"+r.nextInt(21)+".gif");
            user.setLevel(1);
            user.setCoin(1000);
                
            //是否添加成功
            userService.addUser(user);
        
            MailUtils.sendMail(user.getEmail(), "请激活", "恭喜您注册成功,请点击下面的链接激活!! <a href='http://localhost:8080/Gary_SSHForum/UserAction_active?userCode="+user.getCode()+"'>点击这里</a>");
            
            return "toRegisterSuccess";    
            
        }

      提问区动态设置提问者头像(index.jsp)

                            <div class="quesContent" style="px;height:px;overflow:hidden;white-space:normal;text-overflow:ellipsis;">
                                <div class="quesTitle">
                                    <s:property value="#paste.offer" />
                                    <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span>
                                </div>
                                <div class="qContent"><s:property value="#paste.content"/></div>
                                <div class="tags">
                                    <span class="tag">excel</span><span class="tag">程序</span>
                                </div>
                                <div class="quesUser" style="margin-top:28px;margin-right:20px">
                                    <image src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" class="imguser" />
                                    
                                    <div class="userName">
                                        <s:property value="#paste.user.username"/>
                                        <div class="liulan">浏览(<s:property value="#paste.createtime"/>) <s:property value="#paste.user.username"/></div>
                                    </div>
                                </div>

       设置登陆时登录者头像(head.jsp)

    <!--描述:登录的样子-->
                <s:if test="#session.user!=null">
                    <a class="avatar" href="">
                        <img src="${pageContext.request.contextPath }/<s:property value="#session.user.image"/>">
                        
                        <cite>
                            <s:property value="#session.user.username" />
                        </cite>
                    </a>
                    </div>
                </s:if>

      分页Div

    <div style="text-align: center">
              <div class="laypage-main"><span class="laypage-curr">1</span><a href="/jie/page/2/">2</a><a href="/jie/page/3/">3</a><a href="/jie/page/4/">4</a><a href="/jie/page/5/">5</a><span></span><a href="/jie/page/148/" class="laypage-last" title="尾页">尾页</a><a href="/jie/page/2/" class="laypage-next">下一页</a></div>
    </div>

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@taglib uri="/struts-tags" prefix="s"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="css/index.css">
    </head>
    
    <body style="margin: -2px">
        <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe>
        <div class="dvContent">
            <div class="dvquesleft">
    
                <div class="dvqstitle">
                    <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">问答</span> <span class="back"><ab href="">《《返回上一页</a></span>
                </div>
                <div class="dvtabhead">
                    <div class="tabheads tabcurrent">全部问题</div>
                    <div class="tabheads">我的问题</div>
                    <div class="tabheads">关注问题</div>
                    <div class="tabheads">问题标签</div>
                </div>
                <div class="tabContent">
                    <div class="dvtags">
                        <a class="curenttag">待解决</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解决</a>
                    </div>
                    <div class="tab">
                        <s:iterator value="pasteList" var="paste">
                            <div class="dvques">
                                <div class="quesCount">
                                    <div class="count">8</div>
                                    <div class="ques">
                                        <s:property value="#paste.ansnum" />
                                    </div>
                                </div>
                                <div class="quesContent" style=" px; height: px; overflow: hidden; white-space: normal; text-overflow: ellipsis;">
                                    <div class="quesTitle">
                                        <s:property value="#paste.offer" />
                                        <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span>
                                    </div>
                                    <div class="qContent">
                                        <s:property value="#paste.content" />
                                    </div>
                                    <div class="tags">
                                        <span class="tag">excel</span><span class="tag">程序</span>
                                    </div>
                                    <div class="quesUser" style="margin-top: 28px; margin-right: 20px">
                                        <image src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" class="imguser" />
    
                                        <div class="userName">
                                            <s:property value="#paste.user.username" />
                                            <div class="liulan">
                                                浏览(
                                                <s:property value="#paste.createtime" />
                                                )
                                                <s:property value="#paste.user.username" />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </s:iterator>
                        <div style="text-align: center">
                            <div class="laypage-main">
                                <span class="laypage-curr">1</span><a href="/jie/page/2/">2</a><a href="/jie/page/3/">3</a><a href="/jie/page/4/">4</a><a href="/jie/page/5/">5</a><span></span><a href="/jie/page/148/" class="laypage-last" title="尾页">尾页</a><a href="/jie/page/2/" class="laypage-next">下一页</a>
                            </div>
                        </div> 
    
                    </div>
                    <div class="tab hidden">2</div>
                    <div class="tab hidden">3</div>
                    <div class="tab hidden">4</div>
                </div>
            </div>
            <div class="dvquesright">
                <div>
                    <buton class="btnques" onclick="location.href='add.jsp'">提个问题</buton>
                </div>
                <div class="dvorder">
                    <div class="orderTitle">专家排行榜</div>
                    <div class="users">
                        <image class="userface" src="images/0.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/1.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/2.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/3.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/4.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/5.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/6.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
    
    
                </div>
    
            </div>
    
        </div>
        <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
            $(function() {
    
                $(".tabheads").click(
                        function() {
                            $(".tabheads").removeClass("tabcurrent").eq(
                                    $(this).index()).addClass("tabcurrent");
                            $(".tab").hide().eq($(this).index()).show();
                        });
            });
        </script>
    
    </body>
    </html>
    index.jsp

      分析四个参数

        pageSize

        currentPage

        totalCount

        totalPage

      书写PageBean.java

      PageBean需要的属性

        //页面大小
        private Integer pageSize;
        //当前页
        private Integer currentPage;
        //总条数
        private Integer totalCount;
        //总页数
        private Integer totalPage;
        
        //每一页显示的数据
        private List list;

      书写PageBean构造方法

        public PageBean(Integer currentPage,Integer totalCount,Integer pageSize)
        {
            this.currentPage = currentPage;
            this.totalCount = totalCount;
            this.pageSize=pageSize;
            
            //安全校验
            if(currentPage==null)
            {
                this.currentPage=1;
            }
            if(this.pageSize==null)
            {
                this.pageSize=8;
            }
            
            //计算totalPage
            this.totalPage=(int)Math.ceil(1.0*this.totalCount/this.pageSize);
            
            //安全校验
            if(this.currentPage>this.totalPage)
            {
                this.currentPage=this.totalPage;
            }
            if(this.currentPage<1)
            {
                this.currentPage=1;
            }
            
        }

      书写PageBean得到起始索引的方法

        public Integer getStart()
        {
            return (this.currentPage-1)*this.pageSize;
        }

      分析PageBean

       帖子Web层

    public String getData() throws Exception {
            
            PageBean pastePageBean = pasteService.getPastePageBean(currentPage);
            
            ActionContext.getContext().put("pastePageBean", pastePageBean);
            
            
            return "index";
        }

        帖子Service层

        public PageBean getPastePageBean(Integer currentPage) {
            
            Integer totalCount = pasteDao.findAllPasteNum();
            PageBean pageBean = new PageBean(currentPage,totalCount,8);
            List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize());
            pageBean.setList(list);
            
            
            return pageBean;
        }

      获得数据库中帖子Dao层

        //查找所有帖子的数量
        public Integer findAllPasteNum() {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select count(*) from paste";
            NativeQuery query = session.createSQLQuery(sql);
            BigInteger result = (BigInteger)query.uniqueResult();
            
            return result.intValue();
        }
        
        //分页
        public List<Paste> getPastePageList(Integer start, Integer pageSize) {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select * from paste limit ?,?";
            NativeQuery query = session.createSQLQuery(sql);
            query.addEntity(Paste.class);
            query.setParameter(1, start);
            query.setParameter(2, pageSize);
            List list = query.list();
            
            return list;
        }

     

    package com.Gary.utils;
    
    import java.util.List;
    
    public class PageBean {
    
    //     pageSize
    //   currentPage
    //   totalCount
    //   totalPage
        
    
        //页面大小
        private Integer pageSize;
        //当前页
        private Integer currentPage;
        //总条数
        private Integer totalCount;
        //总页数
        private Integer totalPage;
        
        //每一页显示的数据
        private List list;
    
        public PageBean(Integer currentPage,Integer totalCount,Integer pageSize)
        {
            this.currentPage = currentPage;
            this.totalCount = totalCount;
            this.pageSize = pageSize;
            
            //安全校验
            if(this.currentPage == null)
            {
                this.currentPage = 1;
            }
            if(this.pageSize == null)
            {
                this.pageSize = 8;
            }
            
            //计算totalPage
            this.totalPage = (int) Math.ceil(1.0 * this.totalCount/this.pageSize);//所有的條數之和除以每頁顯示的條數向上取整
            
            //安全校验
            if(this.currentPage > this.totalPage)
            {
                this.currentPage = this.totalPage;
            }
            if(this.currentPage < 1)
            {
                this.currentPage = 1;
            }
        }
    
        //获得起始索引
        public Integer getStart()
        {
            return (this.currentPage-1)*this.pageSize;//(第一個數-1)*第二個數
        }
       
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    
        public Integer getTotalCount() {
            return totalCount;
        }
    
        public void setTotalCount(Integer totalCount) {
            this.totalCount = totalCount;
        }
    
        public Integer getTotalPage() {
            return totalPage;
        }
    
        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }
    
        public List getList() {
            return list;
        }
    
        public void setList(List list) {
            this.list = list;
        }
        
        
    }
    PageBean.class
    package com.Gary.web;
    
    import java.util.List;
    
    import com.Gary.domain.Paste;
    import com.Gary.service.PasteService;
    import com.Gary.utils.PageBean;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class GetDataAction extends ActionSupport{
    
        private PasteService pasteService;
        private Integer currentPage;
        
    
        public String getData() throws Exception {
            
            PageBean pastePageBean = pasteService.getPastePageBean(currentPage);
            
            ActionContext.getContext().put("pastePageBean", pastePageBean);
            
            
            return "index";
        }
    
        public PasteService getPasteService() {
            return pasteService;
        }
    
        public void setPasteService(PasteService pasteService) {
            this.pasteService = pasteService;
        }
        
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    }
    GetDataAction.class
    package com.Gary.service;
    
    import java.util.List;
    
    import com.Gary.dao.PasteDao;
    import com.Gary.domain.Paste;
    import com.Gary.utils.PageBean;
    
    public class PasteService {
    
        private PasteDao pasteDao;
        
        public PageBean getPastePageBean(Integer currentPage) {
            
            Integer totalCount = pasteDao.findAllPasteNum();
            PageBean pageBean = new PageBean(currentPage,totalCount,8);
            List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize());
            pageBean.setList(list);
            
            
            return pageBean;
        }
        
        public PasteDao getPasteDao() {
            return pasteDao;
        }
    
        public List<Paste> findAllPaste() {
        
            return pasteDao.findAllPaste();
        
        }
        
        public void setPasteDao(PasteDao pasteDao) {
            this.pasteDao = pasteDao;
        }
    
        public void addPaste(Paste paste) {
            
            pasteDao.addPaste(paste);
            
        }
    
    
    }
    PasteService.class
    package com.Gary.dao;
    
    import java.math.BigInteger;
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.query.NativeQuery;
    import org.hibernate.query.Query;
    import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
    
    import com.Gary.domain.Paste;
    
    public class PasteDao extends HibernateDaoSupport{
        
        public void addPaste(Paste paste) {
            
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            session.save(paste);
        }
    
        
        //HQL
        public List<Paste> findAllPaste() {
            
             Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            
            //书写SQL语句
            String hql = "from com.Gary.domain.Paste";
            Query query = session.createQuery(hql);
            
            List<Paste> list = query.list();
            
            return list;
        }
    
        //查找所有帖子的数量
        public Integer findAllPasteNum() {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select count(*) from paste";
            NativeQuery query = session.createSQLQuery(sql);
            BigInteger result = (BigInteger)query.uniqueResult();
            
            return result.intValue();
        }
        
        //分页
        public List<Paste> getPastePageList(Integer start, Integer pageSize) {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select * from paste limit ?,?";
            NativeQuery query = session.createSQLQuery(sql);
            query.addEntity(Paste.class);
            query.setParameter(1, start);
            query.setParameter(2, pageSize);
            List list = query.list();
            
            return list;
        }
        
    }
    PasteDao.class

      修改分页Div

                        <div style="text-align: center">
                            <div class="laypage-main">
                                <a href="/jie/page/148/" class="laypage-last" title="尾页">首页</a>
                                <a href="/jie/page/2/" class="laypage-next">上一页</a>
                                <span class="laypage-curr">1</span>
                                <a href="/jie/page/2/">2</a>
                                <a href="/jie/page/3/">3</a>
                                <a href="/jie/page/4/">4</a>
                                <a href="/jie/page/5/">5</a>
                                <span></span>
                                <a href="/jie/page/148/" class="laypage-last" title="尾页">尾页</a>
                                <a href="/jie/page/2/" class="laypage-next">下一页</a>
                            </div>
                        </div> 

        

       动态实现上一页,下一页跳转

    <div style="text-align: center">
                            <div class="laypage-main">
                                <a href="/jie/page/148/" class="laypage-last" title="首页">首页</a>
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>" class="laypage-next">上一页</a>
                                <span class="laypage-curr">1</span>
                                <a href="/jie/page/2/">2</a>
                                <a href="/jie/page/3/">3</a>
                                <a href="/jie/page/4/">4</a>
                                <a href="/jie/page/5/">5</a>
                                <span></span>
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>" class="laypage-next">下一页</a>
                                <a href="/jie/page/148/" class="laypage-last" title="尾页">尾页</a>
                            </div>
                        </div> 

      实现分页菜单栏

    <div style="text-align: center">
                            <div class="laypage-main">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=1" class="laypage-last" title="首页">首页</a>
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>" class="laypage-next">上一页</a>
                                <s:if test="#pastePageBean.currentPage-3>0">
                                    <span></span>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage - 2>0">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 2"/>"><s:property value="#pastePageBean.currentPage-2"/></a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage - 1>0">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>"><s:property value="#pastePageBean.currentPage-1"/></a>
                                </s:if>
                                <!-- 当前页 -->
                                <span class="laypage-curr"> <s:property value="#pastePageBean.currentPage"/> </span>
                                
                                <s:if test="#pastePageBean.currentPage <#pastePageBean.totalPage">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>"><s:property value="#pastePageBean.currentPage+1"/></a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage + 1<#pastePageBean.totalPage">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 2"/>"><s:property value="#pastePageBean.currentPage+2"/></a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage + 2<#pastePageBean.totalPage">
                                <span></span>
                                </s:if>
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>" class="laypage-next">下一页</a>
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾页">尾页</a>
                            </div>
                        </div> 

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@taglib uri="/struts-tags" prefix="s"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="css/index.css">
    </head>
    
    <body style="margin: -2px">
        <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe>
        <div class="dvContent">
            <div class="dvquesleft">
    
                <div class="dvqstitle">
                    <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">问答</span> <span class="back"><ab href="">《《返回上一页</a></span>
                </div>
                <div class="dvtabhead">
                    <div class="tabheads tabcurrent">全部问题</div>
                    <div class="tabheads">我的问题</div>
                    <div class="tabheads">关注问题</div>
                    <div class="tabheads">问题标签</div>
                </div>
                <div class="tabContent">
                    <div class="dvtags">
                        <a class="curenttag">待解决</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解决</a>
                    </div>
                    <div class="tab">
                        <s:iterator value="#pastePageBean.list" var="paste">
                            <div class="dvques">
                                <div class="quesCount">
                                    <div class="count">8</div>
                                    <div class="ques">
                                        <s:property value="#paste.ansnum" />
                                    </div>
                                </div>
                                <div class="quesContent" style=" px; height: px; overflow: hidden; white-space: normal; text-overflow: ellipsis;">
                                    <div class="quesTitle">
                                        <s:property value="#paste.offer" />
                                        <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span>
                                    </div>
                                    <div class="qContent">
                                        <s:property value="#paste.content" />
                                    </div>
                                    <div class="tags">
                                        <span class="tag">excel</span><span class="tag">程序</span>
                                    </div>
                                    <div class="quesUser" style="margin-top: 28px; margin-right: 20px">
                                        <image src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" class="imguser" />
    
                                        <div class="userName">
                                            <s:property value="#paste.user.username" />
                                            <div class="liulan">
                                                浏览(
                                                <s:property value="#paste.createtime" />
                                                )
                                                <s:property value="#paste.user.username" />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </s:iterator>
                        <div style="text-align: center">
                            <div class="laypage-main">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=1" class="laypage-last" title="首页">首页</a>
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>" class="laypage-next">上一页</a>
                                <s:if test="#pastePageBean.currentPage-3>0">
                                    <span></span>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage - 2>0">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 2"/>"><s:property value="#pastePageBean.currentPage-2"/></a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage - 1>0">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>"><s:property value="#pastePageBean.currentPage-1"/></a>
                                </s:if>
                                <!-- 当前页 -->
                                <span class="laypage-curr"> <s:property value="#pastePageBean.currentPage"/> </span>
                                
                                <s:if test="#pastePageBean.currentPage <#pastePageBean.totalPage">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>"><s:property value="#pastePageBean.currentPage+1"/></a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage + 1<#pastePageBean.totalPage">
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 2"/>"><s:property value="#pastePageBean.currentPage+2"/></a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage + 2<#pastePageBean.totalPage">
                                <span></span>
                                </s:if>
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>" class="laypage-next">下一页</a>
                                <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾页">尾页</a>
                            </div>
                        </div> 
    
                    </div>
                    <div class="tab hidden">2</div>
                    <div class="tab hidden">3</div>
                    <div class="tab hidden">4</div>
                </div>
            </div>
            <div class="dvquesright">
                <div>
                    <buton class="btnques" onclick="location.href='add.jsp'">提个问题</buton>
                </div>
                <div class="dvorder">
                    <div class="orderTitle">专家排行榜</div>
                    <div class="users">
                        <image class="userface" src="images/0.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/1.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/2.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/3.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/4.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/5.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
                    <div class="users">
                        <image class="userface" src="images/6.gif" />
                        <div class="dvuser">
                            <div class="userTitle">陈有龙</div>
                            <div class="userdeital">大牛6级 豆:14006</div>
                        </div>
                    </div>
    
    
                </div>
    
            </div>
    
        </div>
        <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
            $(function() {
    
                $(".tabheads").click(
                        function() {
                            $(".tabheads").removeClass("tabcurrent").eq(
                                    $(this).index()).addClass("tabcurrent");
                            $(".tab").hide().eq($(this).index()).show();
                        });
            });
        </script>
    
    </body>
    </html>
    index.jsp

      分析最近热议模块

      将数据传递给前端

      最近热帖Web层

        public String getData() throws Exception {
            
            //得到帖子
            PageBean pastePageBean = pasteService.getPastePageBean(currentPage);
            ActionContext.getContext().put("pastePageBean", pastePageBean);
            
            //得到最近热帖
            PageBean glanceoverPagebean = pasteService.getGlanceoverPageBean(currentPage);
            ActionContext.getContext().put("glanceoverPagebean", glanceoverPagebean);
            
            return "index";
        }

      最近热帖Service层

        public PageBean getGlanceoverPageBean(Integer currentPage) {
            
             Integer totalCount = pasteDao.findAllPasteNum();
             PageBean glanceoverPageBean = new PageBean(currentPage,totalCount,8);
             List<Paste> list = pasteDao.getGlanceoverPageList();
             glanceoverPageBean.setList(list);
             
             
            return glanceoverPageBean;
        }
    
        
        public PageBean getPastePageBean(Integer currentPage) {
            
            Integer totalCount = pasteDao.findAllPasteNum();
            PageBean pageBean = new PageBean(currentPage,totalCount,8);
            List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize());
            pageBean.setList(list);
            
            
            return pageBean;
        }

      

      最近热帖Dao层

        //最近热议
        public List<Paste> getGlanceoverPageList() {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select * from paste order by offer desc limit 0,8";
            NativeQuery query = session.createSQLQuery(sql);
            query.addEntity(Paste.class);
            List list = query.list();
            
            return null;
        }

      动态获得数据库中热帖数据

    <dt class="fly-panel-title">最近热帖</dt>
                        <s:iterator value="#glanceoverPageBean.list" var="paste">
                            <dd>
                                <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>">
                                    <s:property value="#paste.title" />
                                </a>
                                <span>
                                    <i class="iconfont">&#xe60b;</i>
                                    <s:property value="#paste.glanceover" />
                                </span>
                            </dd>
                        </s:iterator>

      分析最近热议模块

      书写最近热议后端代码

      最近热议Web层

        public String getData() throws Exception {
            
            //得到帖子
            PageBean pastePageBean = pasteService.getPastePageBean(currentPage);
            ActionContext.getContext().put("pastePageBean", pastePageBean);
            
            //得到最近热帖
            PageBean glanceoverPageBean = pasteService.getGlanceoverPageBean(currentPage);
            ActionContext.getContext().put("glanceoverPageBean", glanceoverPageBean);
            
            //得到最近热议
            PageBean ansnumPageBean = pasteService.getAnsnumPageBean(currentPage);
            ActionContext.getContext().put("ansnumPageBean", ansnumPageBean);
            
            return "index";
        }

       最近热议Service层

        public PageBean getAnsnumPageBean(Integer currentPage) {
            //获得所有的帖子数目
            Integer totalCount = pasteDao.findAllPasteNum();
            //创建PageBean
            PageBean ansnumPageBean = new PageBean(currentPage, totalCount, 8);
            //得到List
            List<Paste> list = pasteDao.getAnsnumPageList();
            //封装List
            ansnumPageBean.setList(list);    
            return ansnumPageBean;
        }

      Dao层获得数据库中的数据

        public List<Paste> getAnsnumPageList() {
            //得到与当前线程绑定的session
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            //书写sql
            String sql = "select * from paste order by ansnum desc limit 0,8";
            //获得query
            NativeQuery query = session.createSQLQuery(sql);
            //将结果集封装为Paste对象
            query.addEntity(Paste.class);
            //获得sql的执行结果(结果为list)
            List list = query.list();
            return list;
        }

      编写最近热议前端

    <dl class="fly-panel fly-list-one">
                        <dt class="fly-panel-title">近期热议</dt>
                        <s:iterator value="#ansnumPageBean.list" var="paste">
                            <dd>
                                <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>">
                                    <s:property value="#paste.title" />
                                </a>
                                <span>
                                    <i class="iconfont">&#xe60c;</i>
                                    <s:property value="#paste.ansnum" />
                                </span>
                            </dd>
                        </s:iterator>
                    </dl>

      

      查询专家排行榜

      编写专家排行榜

      专家排行榜Web层

    private UserService userService;
    
    public String getData() throws Exception {
            
            //得到帖子
            PageBean pastePageBean = pasteService.getPastePageBean(currentPage);
            ActionContext.getContext().put("pastePageBean", pastePageBean);
            
            //得到最近热帖
            PageBean glanceoverPageBean = pasteService.getGlanceoverPageBean(currentPage);
            ActionContext.getContext().put("glanceoverPageBean", glanceoverPageBean);
            
            //得到最近热议
            PageBean ansnumPageBean = pasteService.getAnsnumPageBean(currentPage);
            ActionContext.getContext().put("ansnumPageBean", ansnumPageBean);
            
            //得到专家排行
            PageBean userPageBean = userService.getUserPageBean(currentPage);
            ActionContext.getContext().put("userPageBean", userPageBean);
            
            return "index";
        }

      专家排行榜Service层

        public PageBean getUserPageBean(Integer currentPage) {
            Integer totalCount = userDao.findAllUserNum();
            PageBean userPageBean = new PageBean(currentPage, totalCount, 8);
            List<User> list = userDao.getUserPageBean();
            userPageBean.setList(list);
            
            return userPageBean;
        }

      专家排行榜Dao层

        public Integer findAllUserNum() {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select count(*) from user";
            NativeQuery query = session.createSQLQuery(sql);
            BigInteger result = (BigInteger) query.uniqueResult();
            return result.intValue();
        }
    
    
        public List<User> getUserPageBean() {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select * from user order by concat(coin,level) desc limit 0,8";
            NativeQuery query = session.createSQLQuery(sql);
            query.addEntity(User.class);
            List list = query.list();
            return list;
        }

      专家排行榜前端

    <div class="orderTitle">专家排行榜</div>
    
                    <s:iterator value="#userPageBean.list" var="user">
                        <div class="users">
    
                            <image class="userface" src="${pageContext.request.contextPath }/<s:property value="#user.image"/>" />
                            <div class="dvuser">
                                <div class="userTitle">
                                    <s:property value="#user.username" />
                                </div>
                                <div class="userdeital">
                                    大牛
                                    <s:property value="#user.level" />
                                    级 豆:
                                    <s:property value="#user.coin" />
                                </div>
                            </div>
                        </div>
                    </s:iterator>

      修改web.xml,实现发布自动跳转到获得数据库中的数据

        <display-name>Gary_SSHForum</display-name>
        <welcome-file-list>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>

      创建一个default.jsp实现函数页面跳转

    <body>
        <% response.sendRedirect(request.getContextPath()+"/GetDataAction_getData"); %>
    </body>

      修改分页标签样式格式

    <div style="text-align: center">
                            <div class="laypage-main">
    
                                <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>" class="laypage-next">上一页</a>
                                <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=1" class="laypage-last" title="尾页">首页</a>
                                <s:if test="#pastePageBean.currentPage-3>0">
                                    <span></span>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage-2>0">
                                    <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-2"/>">
                                        <s:property value="#pastePageBean.currentPage-2" />
                                    </a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage-1>0">
                                    <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>">
                                        <s:property value="#pastePageBean.currentPage-1" />
                                    </a>
                                </s:if>
    
                                <!-- 当前页 -->
                                <span class="laypage-curr">
                                    <s:property value="#pastePageBean.currentPage" />
                                </span>
    
                                <s:if test="#pastePageBean.currentPage<#pastePageBean.totalPage">
                                    <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>">
                                        <s:property value="#pastePageBean.currentPage+1" />
                                    </a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage+1<#pastePageBean.totalPage">
                                    <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+2"/>">
                                        <s:property value="#pastePageBean.currentPage+2" />
                                    </a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage+2<#pastePageBean.totalPage">
                                    <span></span>
                                </s:if>
                                <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾页">尾页</a>
    
                                <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>" class="laypage-next">下一页</a>
                            </div>
                        </div>

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">
        <display-name>Gary_SSHForum</display-name>
        <welcome-file-list>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
    
        <!-- 让spring随着Web项目的启动而启动 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- 读取配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <!-- 扩大session的范围 -->
        <filter>
            <filter-name>openSession</filter-name>
            <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>openSession</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    
        <!-- 让struts启动 -->
        <filter>
            <filter-name>struts</filter-name>
            <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    </web-app>
    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
    
        <!--开启动态方法调用 -->
        <constant name="struts.devMode" value="true"></constant>
        <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    
        <!-- 告诉struts不用自己创建Action,Spring来帮你创建 -->
        <constant name="struts.objectFactory" value="spring"></constant>
        <package name="Gary_SSHForum" namespace="/" extends="struts-default">
            <!-- 允许全部方法 -->
            <global-allowed-methods>regex:.*</global-allowed-methods>
            <action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}">
                <result name="toLogin" type="redirect">/login.jsp</result>
                <result name="login">/login.jsp</result>
                <result name="toIndex" type="redirect">/index.jsp</result>
                <result name="error">/login.jsp</result>
                <result name="toRegisterSuccess" type="redirect">/registerSuccess.jsp</result>
            </action>
        
        <action name="PasteAction_*" class="com.Gary.web.PasteAction" method="{1}">
            <result name="toIndex" type="redirect">/index.jsp</result>
            <result name="error">/login.jsp</result>
        </action>
        
        <action name="GetDataAction_*"  class="com.Gary.web.GetDataAction" method="{1}">
            <result name="index" >/index.jsp</result>
        </action>
        
        </package>
    
    </struts>
    struts.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!-- 配置数据源 -->
        <bean name="dataSource"
            class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="jdbcUrl" value="jdbc:mysqL:///garyssh_forum"></property>
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="user" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>
    
        <!-- 配置sessionFactory -->
        <bean name="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sqp">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                </props>
            </property>
    
            <property name="mappingDirectoryLocations"
                value="classpath:com/Gary/domain"></property>
    
        </bean>
    
        <!-- 配置事务的核心管理器 -->
        <bean name="transactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    
        <!-- 通知 -->
        <tx:advice id="advice"
            transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*" />
            </tx:attributes>
        </tx:advice>
    
        <!-- 织入 -->
        <aop:config>
            <!-- 切入点 -->
            <aop:pointcut
                expression="execution(* com.Gary.service.*.*(..))" id="pc" />
            <!-- 配置切面 切入点+通知 -->
            <aop:advisor advice-ref="advice" pointcut-ref="pc" />
        </aop:config>
    
        <!-- 配置Action -->
        <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype">
            <property name="userService" ref="userService"></property>
        </bean>
        <bean name="pasteAction" class="com.Gary.web.PasteAction" scope="prototype">
            <property name="pasteService" ref="pasteService"></property>
        </bean>
        <bean name="getDataAction" class="com.Gary.web.GetDataAction" scope="prototype">
            <property name="pasteService" ref="pasteService"></property>
            <property name="userService" ref="userService"></property>
        </bean>
        
        <!-- 配置Service -->
        <bean name="userService" class="com.Gary.service.UserService">
            <property name="userDao" ref="userDao"></property>
        </bean>
        <bean name="pasteService" class="com.Gary.service.PasteService">
            <property name="pasteDao" ref="pasteDao"></property>
        </bean>
        
        <!-- 配置Dao -->
        <bean name="userDao" class="com.Gary.dao.UserDao">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <bean name="pasteDao" class="com.Gary.dao.PasteDao">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
    </beans>
    applicationContext.xml
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@taglib uri="/struts-tags" prefix="s"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <link rel="stylesheet" href="res/css/global.css">
    </head>
    
    <body style="margin: -2px">
        <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe>
        <div class="dvContent">
            <div class="dvquesleft">
    
                <div class="dvqstitle">
                    <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">问答</span> <span class="back">
                        <ab href="">《《返回上一页</a>
                    </span>
                </div>
                <div class="dvtabhead">
                    <div class="tabheads tabcurrent">全部问题</div>
                    <div class="tabheads">我的问题</div>
                    <div class="tabheads">关注问题</div>
                    <div class="tabheads">问题标签</div>
                </div>
                <div class="tabContent">
                    <div class="dvtags">
                        <a class="curenttag">待解决</a>
                        <span class="line"></span>
                        <a>高分</a>
                        <span class="line"></span>
                        <a>新回答</a>
                        <span class="line"></span>
                        <a>已解决</a>
                    </div>
                    <div class="tab">
                        <s:iterator value="#pastePageBean.list" var="paste">
                            <div class="dvques">
                                <div class="quesCount">
                                    <div class="count">
                                        <s:property value="#paste.ansnum" />
                                    </div>
                                    <div class="ques">回答数</div>
                                </div>
                                <div class="quesContent">
                                    <div class="quesTitle">
                                        <s:property value="#paste.offer" />
                                        <image src="images/bean.jpg" class="bean"> <span class="spanques">
                                            
                                            <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>">
                                                <s:property value="#paste.title" />
                                            </a>
                                        </span>
                                    </div>
                                    <div class="qContent" style=" 630px; height: 34px; overflow: hidden; white-space: normal; text-overflow: ellipsis;">
                                        <p>
                                            <s:property value="#paste.content" />
                                        </p>
                                    </div>
                                    <div class="tags">
                                        <span class="tag">excel</span>
                                        <span class="tag">程序</span>
                                    </div>
                                    <div class="quesUser" style="margin-top: 20px; margin-right: 20px">
                                        <image src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" class="imguser" />
    
                                        <div class="userName">
                                            <s:property value="#paste.user.username" />
                                            <div class="liulan">
                                                浏览(
                                                <s:property value="#paste.glanceover" />
                                                )
                                                <s:property value="#paste.createtime" />
                                            </div>
                                        </div>
    
                                    </div>
                                </div>
                            </div>
                        </s:iterator>
                        <div style="text-align: center">
                            <div class="laypage-main">
    
                                <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>" class="laypage-next">上一页</a>
                                <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=1" class="laypage-last" title="尾页">首页</a>
                                <s:if test="#pastePageBean.currentPage-3>0">
                                    <span></span>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage-2>0">
                                    <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-2"/>">
                                        <s:property value="#pastePageBean.currentPage-2" />
                                    </a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage-1>0">
                                    <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>">
                                        <s:property value="#pastePageBean.currentPage-1" />
                                    </a>
                                </s:if>
    
                                <!-- 当前页 -->
                                <span class="laypage-curr">
                                    <s:property value="#pastePageBean.currentPage" />
                                </span>
    
                                <s:if test="#pastePageBean.currentPage<#pastePageBean.totalPage">
                                    <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>">
                                        <s:property value="#pastePageBean.currentPage+1" />
                                    </a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage+1<#pastePageBean.totalPage">
                                    <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+2"/>">
                                        <s:property value="#pastePageBean.currentPage+2" />
                                    </a>
                                </s:if>
                                <s:if test="#pastePageBean.currentPage+2<#pastePageBean.totalPage">
                                    <span></span>
                                </s:if>
                                <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾页">尾页</a>
    
                                <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>" class="laypage-next">下一页</a>
                            </div>
                        </div>
                    </div>
                    <div class="tab hidden">2</div>
                    <div class="tab hidden">3</div>
                    <div class="tab hidden">4</div>
                </div>
            </div>
            <div class="dvquesright">
                <div>
                    <buton class="btnques" onclick="location.href='add.jsp'">提个问题</buton>
                </div>
    
    
    
                <div class="dvorder">
    
                    <dl class="fly-panel fly-list-one">
                        <dt class="fly-panel-title">最近热帖</dt>
                        <s:iterator value="#glanceoverPageBean.list" var="paste">
                            <dd>
                                <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>">
                                    <s:property value="#paste.title" />
                                </a>
                                <span>
                                    <i class="iconfont">&#xe60b;</i>
                                    <s:property value="#paste.glanceover" />
                                </span>
                            </dd>
                        </s:iterator>
                    </dl>
    
    
                    <dl class="fly-panel fly-list-one">
                        <dt class="fly-panel-title">近期热议</dt>
                        <s:iterator value="#ansnumPageBean.list" var="paste">
                            <dd>
                                <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>">
                                    <s:property value="#paste.title" />
                                </a>
                                <span>
                                    <i class="iconfont">&#xe60c;</i>
                                    <s:property value="#paste.ansnum" />
                                </span>
                            </dd>
                        </s:iterator>
                    </dl>
    
                    <div class="orderTitle">专家排行榜</div>
    
                    <s:iterator value="#userPageBean.list" var="user">
                        <div class="users">
    
                            <image class="userface" src="${pageContext.request.contextPath }/<s:property value="#user.image"/>" />
                            <div class="dvuser">
                                <div class="userTitle">
                                    <s:property value="#user.username" />
                                </div>
                                <div class="userdeital">
                                    大牛
                                    <s:property value="#user.level" />
                                    级 豆:
                                    <s:property value="#user.coin" />
                                </div>
                            </div>
                        </div>
                    </s:iterator>
    
    
                </div>
    
            </div>
    
        </div>
        <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
            $(function() {
    
                $(".tabheads").click(
                        function() {
                            $(".tabheads").removeClass("tabcurrent").eq(
                                    $(this).index()).addClass("tabcurrent");
                            $(".tab").hide().eq($(this).index()).show();
                        });
            });
        </script>
    
    </body>
    </html>
    index.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <% response.sendRedirect(request.getContextPath()+"/GetDataAction_getData"); %>
    </body>
    </html>
    default.jso
    package com.Gary.utils;
    
    import java.util.List;
    
    public class PageBean {
    
    //     pageSize
    //   currentPage
    //   totalCount
    //   totalPage
        
    
        //页面大小
        private Integer pageSize;
        //当前页
        private Integer currentPage;
        //总条数
        private Integer totalCount;
        //总页数
        private Integer totalPage;
        
        //每一页显示的数据
        private List list;
    
        public PageBean(Integer currentPage,Integer totalCount,Integer pageSize)
        {
            this.currentPage = currentPage;
            this.totalCount = totalCount;
            this.pageSize = pageSize;
            
            //安全校验
            if(this.currentPage == null)
            {
                this.currentPage = 1;
            }
            if(this.pageSize == null)
            {
                this.pageSize = 8;
            }
            
            //计算totalPage
            this.totalPage = (int) Math.ceil(1.0 * this.totalCount/this.pageSize);//所有的條數之和除以每頁顯示的條數向上取整
            
            //安全校验
            if(this.currentPage > this.totalPage)
            {
                this.currentPage = this.totalPage;
            }
            if(this.currentPage < 1)
            {
                this.currentPage = 1;
            }
        }
    
        //获得起始索引
        public Integer getStart()
        {
            return (this.currentPage-1)*this.pageSize;//(第一個數-1)*第二個數
        }
       
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    
        public Integer getTotalCount() {
            return totalCount;
        }
    
        public void setTotalCount(Integer totalCount) {
            this.totalCount = totalCount;
        }
    
        public Integer getTotalPage() {
            return totalPage;
        }
    
        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }
    
        public List getList() {
            return list;
        }
    
        public void setList(List list) {
            this.list = list;
        }
        
        
    }
    PageBean.class
    package com.Gary.web;
    
    import java.util.List;
    
    import com.Gary.domain.Paste;
    import com.Gary.service.PasteService;
    import com.Gary.service.UserService;
    import com.Gary.utils.PageBean;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class GetDataAction extends ActionSupport{
    
        private PasteService pasteService;
        private UserService userService;
        public UserService getUserService() {
            return userService;
        }
    
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
    
        private Integer currentPage;
        
    
        public String getData() throws Exception {
            
            //得到帖子
            PageBean pastePageBean = pasteService.getPastePageBean(currentPage);
            ActionContext.getContext().put("pastePageBean", pastePageBean);
            
            //得到最近热帖
            PageBean glanceoverPageBean = pasteService.getGlanceoverPageBean(currentPage);
            ActionContext.getContext().put("glanceoverPageBean", glanceoverPageBean);
            
            //得到最近热议
            PageBean ansnumPageBean = pasteService.getAnsnumPageBean(currentPage);
            ActionContext.getContext().put("ansnumPageBean", ansnumPageBean);
            
            //得到专家排行
            PageBean userPageBean = userService.getUserPageBean(currentPage);
            ActionContext.getContext().put("userPageBean", userPageBean);
            
            return "index";
        }
    
        public PasteService getPasteService() {
            return pasteService;
        }
    
        public void setPasteService(PasteService pasteService) {
            this.pasteService = pasteService;
        }
        
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    }
    GetDataAction.class
    package com.Gary.service;
    
    import java.util.List;
    
    import com.Gary.dao.PasteDao;
    import com.Gary.domain.Paste;
    import com.Gary.utils.PageBean;
    
    public class PasteService {
    
        private PasteDao pasteDao;
        
        public PageBean getGlanceoverPageBean(Integer currentPage) {
            Integer totalCount = pasteDao.findAllPasteNum();
            PageBean glanceoverPageBean = new PageBean(currentPage, totalCount, 8);
            List<Paste> list = pasteDao.getGlanceoverPageList();
            glanceoverPageBean.setList(list);
            
            return glanceoverPageBean;
        }
        
    
        
        public PageBean getPastePageBean(Integer currentPage) {
            
            Integer totalCount = pasteDao.findAllPasteNum();
            PageBean pageBean = new PageBean(currentPage,totalCount,8);
            List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize());
            pageBean.setList(list);
            
            
            return pageBean;
        }
        
        public PasteDao getPasteDao() {
            return pasteDao;
        }
    
        public List<Paste> findAllPaste() {
        
            return pasteDao.findAllPaste();
        
        }
        
        public void setPasteDao(PasteDao pasteDao) {
            this.pasteDao = pasteDao;
        }
    
        public void addPaste(Paste paste) {
            
            pasteDao.addPaste(paste);
            
        }
    
    
    
        public PageBean getAnsnumPageBean(Integer currentPage) {
            //获得所有的帖子数目
            Integer totalCount = pasteDao.findAllPasteNum();
            //创建PageBean
            PageBean ansnumPageBean = new PageBean(currentPage, totalCount, 8);
            //得到List
            List<Paste> list = pasteDao.getAnsnumPageList();
            //封装List
            ansnumPageBean.setList(list);    
            return ansnumPageBean;
        }
    
    
    }
    PasteService.class
    package com.Gary.service;
    
    import java.math.BigInteger;
    import java.util.List;
    
    import com.Gary.dao.UserDao;
    import com.Gary.domain.User;
    import com.Gary.utils.PageBean;
    
    public class UserService {
    
        private UserDao userDao;
    
        public PageBean getUserPageBean(Integer currentPage) {
            Integer totalCount = userDao.findAllUserNum();
            PageBean userPageBean = new PageBean(currentPage, totalCount, 8);
            List<User> list = userDao.getUserPageBean();
            userPageBean.setList(list);
            
            return userPageBean;
        }
    
        
        public User findUserByUsernameReturnUser(User user) {
            
            return userDao.findUserByUsernameReturnUser(user);
        }
    
        
        public int checkUser(User user) {
            User temp = userDao.findUserByUsernameReturnUser(user);
            //用户名不存在
            if(temp==null)
            {
                return 1;
            }
            //判断密码是否相同
            if(temp.getPassword().equals(user.getPassword()))
            {
                if(temp.getState()==1)
                {
                    //登陆成功
                    return 0;
                }else {
                    //没有激活
                    return 3;
                }
            }else {
                //密码错误
                return 2;
            }
    
        }
        
        public UserDao getUserDao() {
            return userDao;
        }
        
        public void activeUser(String userCode) {
            // TODO Auto-generated method stub
            userDao.activeUser(userCode);
        }
        
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public void addUser(User user) {
            // TODO Auto-generated method stub
             userDao.addUser(user);
        }
    
        public boolean findUserByUsername(String username) {
            // TODO Auto-generated method stub
            Long count = userDao.findUserByUsernameReturnNum(username);
            if(count==0)
                return true;
            else
                return false;
        }
    
    }
    UserService.class
    package com.Gary.domain;
    
    public class Paste {
        
        private String id;
        private String title;
        private String content;
        private Integer offer;
        private Integer ansnum;
        private String createtime;
        private Integer glanceover;
        //是否结帖
        private Integer solve;
        private Integer isdelete;
        
        private User user;
        
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }
        
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public Integer getOffer() {
            return offer;
        }
        public void setOffer(Integer offer) {
            this.offer = offer;
        }
        public Integer getAnsnum() {
            return ansnum;
        }
        public void setAnsnum(Integer ansnum) {
            this.ansnum = ansnum;
        }
        public String getCreatetime() {
            return createtime;
        }
        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }
        public Integer getGlanceover() {
            return glanceover;
        }
        public void setGlanceover(Integer glanceover) {
            this.glanceover = glanceover;
        }
        public Integer getSolve() {
            return solve;
        }
        public void setSolve(Integer solve) {
            this.solve = solve;
        }
        public Integer getIsdelete() {
            return isdelete;
        }
        public void setIsdelete(Integer isdelete) {
            this.isdelete = isdelete;
        }
    
    
        
    }
    Paste.class
    package com.Gary.dao;
    
    import java.math.BigInteger;
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.query.NativeQuery;
    import org.hibernate.query.Query;
    import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
    
    import com.Gary.domain.Paste;
    
    public class PasteDao extends HibernateDaoSupport{
        
        public void addPaste(Paste paste) {
            
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            session.save(paste);
        }
    
        
        //HQL
        public List<Paste> findAllPaste() {
            
             Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            
            //书写SQL语句
            String hql = "from com.Gary.domain.Paste";
            Query query = session.createQuery(hql);
            
            List<Paste> list = query.list();
            
            return list;
        }
    
        //查找所有帖子的数量
        public Integer findAllPasteNum() {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select count(*) from paste";
            NativeQuery query = session.createSQLQuery(sql);
            BigInteger result = (BigInteger)query.uniqueResult();
            
            return result.intValue();
        }
        
        //分页
        public List<Paste> getPastePageList(Integer start, Integer pageSize) {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select * from paste limit ?,?";
            NativeQuery query = session.createSQLQuery(sql);
            query.addEntity(Paste.class);
            query.setParameter(1, start);
            query.setParameter(2, pageSize);
            List<Paste> list = query.list();
            
            return list;
        }
    
        //最近热议
        public List<Paste> getGlanceoverPageList() {
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            String sql = "select * from paste order by glanceover desc limit 0,8";
            NativeQuery query = session.createSQLQuery(sql);
            query.addEntity(Paste.class);
            List list = query.list();
            return list;
        }
    
    
        public List<Paste> getAnsnumPageList() {
            //得到与当前线程绑定的session
            Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
            //书写sql
            String sql = "select * from paste order by ansnum desc limit 0,8";
            //获得query
            NativeQuery query = session.createSQLQuery(sql);
            //将结果集封装为Paste对象
            query.addEntity(Paste.class);
            //获得sql的执行结果(结果为list)
            List list = query.list();
            return list;
        }
        
    }
    PasteDao.class
    (如需转载学习,请标明出处)
  • 相关阅读:
    每周必写
    每周必写
    每周必写
    中国历史上成功的两人合作, 改进, 提高的例子
    每周必写(3)
    结对编程进度及自己的理解
    每周必写
    IT行业的“创新”、“模仿”
    工作时间内容,感想和思考
    周阅读内容
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/9955730.html
Copyright © 2011-2022 走看看