CMS-day02

    xiaoxiao2025-02-14  26

    cms-day02

    1、富文本框

    1.1、传值到前台

    让图片描述内容,隐藏的输入框来接收输入的内容然后将其内容返回给前台

    <input type="hidden" id="txtIntro" name="intro" /> ... <script type="text/javascript"> var E = window.wangEditor //获取到咱们的编辑器位置 var editor = new E('#intro'); //获到取相应的元素(提交的intro元素) var txtIntro = $('#txtIntro'); //监听编辑器的修改事件(html就是编辑器中的内容) editor.customConfig.onchange = function(html) { //把富文框的内容放进去 txtIntro.val(html); } editor.create(); </script>

    1.2、内容回显

    需要保存的是被选中的那一个input加上checked属性富文本框的回显 <input type="hidden" id="txtIntro" name="intro" /> ... <script type="text/javascript"> var E = window.wangEditor //获取到咱们的编辑器位置 var editor = new E('#intro'); //获到取相应的元素(提交的intro元素) var txtIntro = $('#txtIntro'); //监听编辑器的修改事件(html就是编辑器中的内容) editor.customConfig.onchange = function(html) { //把富文框的内容放进去 txtIntro.val(html); } editor.create(); editor.txt.html('${img.intro}'); txtIntro.val(editor.txt.html()); </script>

    2、前端页面展示与图片轮播

    去从后台拿到所有图片把图片在相应的轮播位置进行循环注意: 要保证第一个图片有.active样式的 <!-- varStatus[LoopTagSupport]:每次循环的状态(包含下标,次数,...) --> <c:forEach items="${imgs}" var="img" varStatus="s"> <div class="item <c:if test="${s.index==0}"> active </c:if> "> <img src="${img.storepath}" style="max-height: 500px;width:100%;" alt="码农"> <div class="carousel-caption"> ${img.intro} </div> </div> </c:forEach>

    3、分页查询

    3.1、真假分页

    真分页(limit):匀速,用户体验还可以,用得多一点假分页(内存中):每一次非常慢(后面就快),还会占内存

    3.2、设计一个分页对象

    /** * 分页对象 * @author Administrator * */ public class PageList<T> { //当前页 -> 前台传过来 private int currentPage = 1; //每页条数 -> 前台传过来/自己定义 private int pageSize = 10; //首页(第一页) private int firstPage = 1; //上一页 计算出来 currentPage>1?currentPage-1:1 private int prevPage; //下一页 计算出来 currentPage<lastPage?currentPage+1:lastPage private int nextPage; //尾页 == 总页数 private int lastPage; //总页数 计算出来 totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1 private int totalPage; //总条数 -> 数据库中查询出来 private int totalCount; //当前页的数据 -> 数据库中查询出来 private List<T> data = new ArrayList<>(); public PageList(){} /** * @param currentPage:前台传过来 * @param pageSize:前台传过来 * @param totalCount:数据库中查询 * @param data:数据库中查询 */ public PageList(int currentPage, int pageSize, int totalCount, List<T> data) { this.currentPage = currentPage; this.pageSize = pageSize; this.totalCount = totalCount; this.data = data; //计算上一页 当前页>1 ? 当前页-1 : 1 this.prevPage = this.currentPage>1 ? this.currentPage-1 : 1; //计算总页数 总条数%每页条数==0? 总条数/每页条数:总条数/每页条数+1 this.totalPage = this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1; this.lastPage = this.totalPage; //最后一页就是总页数 //计算下一页 当前页<总页数?当前页+1:总页数; this.nextPage = this.currentPage<this.totalPage?this.currentPage+1:this.totalPage; } //getter,setter省略 }

    3.3、设计SQL条件对象

    接收前台传过来的分页条件(currentPage,pageSize) public class SqlCondition { //当前页 private int currentPage = 1; //每页条数 private int pageSize = 10; //getter,setter省略 }

    3.4、在dao层中完成方法

    根据条件查询到数据,封装成PageList对象 返回 /** * 最后返回的是PageList对象,里面都要有值 * PageList(int currentPage, int pageSize, int totalCount, List<T> data) */ @Override public PageList<Images> queryAll(SqlCondition condition) { //①.拿到当前页与每页条数 int currentPage = condition.getCurrentPage(); int pageSize = condition.getPageSize(); //一.查询总条数 //1.1 准备查询总条数的sql String sql = "select count(*) from t_image"; //1.2执行sql拿到总条数 Integer totalCount = jdbcTemplate.queryForObject(sql, Integer.class); //二.查询当前页的数据 //2.1 计算当前页是从第几条数据开始的 int beginIndex = (currentPage-1) * pageSize; //2.2 准备相应的SQL String dataSql = "select * from t_image limit "+beginIndex+","+pageSize; //2.3 执行查询功能 List<Images> data= jdbcTemplate.query(dataSql, new BeanPropertyRowMapper<>(Images.class)); //三.创建PageList对象并且返回 PageList pageList = new PageList(currentPage,pageSize,totalCount,data); return pageList; }

    3.5、在controller层中修改方法

    controller中的修改 @RequestMapping("/query") public String query(SqlCondition condition,Model model){ model.addAttribute("pageList",imageService.queryAll(condition)); return "main"; }

    3.6、在前台页面展示

    页面中的展示(循环的是pageList.data)
    最新回复(0)