結組作業,第二次衝刺(3)

saccharin發表於2024-06-14

昨天完成了信箱功能,今天我們完成了公告功能,我們可以透過公告功能,把資訊傳遞給使用者,這也是很關鍵的功能,以下為今日部分程式碼:

package com.work.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.work.common.utils.BaseUtil;
import com.work.common.utils.JsonUtil;
import com.work.pojo.Notice;
import com.work.pojo.User;
import com.work.service.NoticeService;

@Controller
@RequestMapping("/server/notice")
public class NoticeController extends BaseUtil{

    @Autowired
    private NoticeService noticeService;
    
    //去公告列表
    @RequestMapping("/goNoticeList")
    public ModelAndView goNoticeList(ModelAndView mv){
        mv.setViewName("server/noticeManage/noticeList");
        return mv;
    }
    
    //獲取公告列表
    @RequestMapping("/getNoticeList")
    public void getNoticeList(HttpServletResponse response,Notice notice,Integer page,Integer limit){
        if(page == null){
            page = 1;
        }
        if(limit == null){
            limit = 10;
        }
        int totalCount = noticeService.getNoticeListCount(notice);
        List<Notice> list = noticeService.getNoticeList(notice,(page-1) * limit, limit);
        output(response,JsonUtil.buildJsonByTotalCount(list, totalCount));
    }
    
    //去新增公告
    @RequestMapping("/goAddNotice")
    public ModelAndView goAddNotice(ModelAndView mv){
        mv.setViewName("server/noticeManage/addNotice");
        return mv;
    }
    
    //新增公告
    @RequestMapping("/addNotice")
    public void addNotice(HttpServletRequest request,HttpServletResponse response,Notice notice){
        User serverUser = (User) request.getSession().getAttribute("serverUser");
        notice.setCreateTime(new Date());
        notice.setUserId(serverUser.getUserId());
        noticeService.addNotice(notice);
        output(response,JsonUtil.buildFalseJson(0, "新增成功!"));
    }
    
    //去編輯公告
    @RequestMapping("/goUpdateNotice")
    public ModelAndView goUpdateNotice(ModelAndView mv,Integer id){
        Notice notice = noticeService.getNoticeById(id);
        mv.addObject("notice", notice);
        mv.setViewName("server/noticeManage/updateNotice");
        return mv;
    }
    
    //編輯公告
    @RequestMapping("/updateNotice")
    public void updateNotice(HttpServletResponse response,Notice notice){
        noticeService.updateNotice(notice);
        output(response,JsonUtil.buildFalseJson(0, "編輯成功!"));
    }

    //刪除公告
    @RequestMapping("/deleteNotice")
    public void deleteNotice(HttpServletResponse response,Integer id){
        noticeService.deleteNoticeById(id);
        output(response,JsonUtil.buildFalseJson(0, "刪除成功!"));
    }
    
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="ck" uri="http://com.work/common"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>平臺公告</title>
    <link rel="stylesheet" type="text/css" href="${ctx}/pc/css/common.css">
    <link rel="stylesheet" type="text/css" href="${ctx}/pc/css/notice.css">
    <link rel="icon" type="image/x-icon" href="${ctx}/pc/images/titleLogo.png">
    
</head>
<body>
    <div class="page">
        <jsp:include page="/pc/index/goHeader.action" />
        <div class="div_personal"> 
            <div class="div_zhong">
                <div class="div_zhong_l">
                    <ul class="ul_zhong_l">
                    <c:forEach items="${page.rows}" var="notice">
                        <li>
                            <span class="span_z_1">></span>
                            <span class="span_z_2" title="${notice.title}" value="${notice.id}">${notice.title}</span>
                            <span class="span_z_3"><fmt:formatDate value="${notice.createTime}" pattern="yyyy-MM-dd"/></span>
                        </li>
                    </c:forEach>
                    </ul>
                    <c:if test="${not empty page.rows}">
                        <div class="ul_fenye">
                            <ck:page url="${ctx}/pc/index/goNotice.action"></ck:page>
                        </div>
                    </c:if>
                </div>
                <div class="div_zhong_r">
                    <div class="div_zhong_r_r">
                        <h3 align="center"></h2>
                        <p></p>
                        <span></span>
                    </div>
                </div>
            </div>
        </div>
        <jsp:include page="/pc/index/goFooter.action" />
    </div>
    <script type="text/javascript" src="${ctx}/pc/js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
    
    //獲取公告
    function firstNotice(noticeId){
        $.ajax({
            url:"${ctx}/pc/index/getNotice.action",
            data:{"id":noticeId},
            type:"POST",
               success:function(res){
                $(".div_zhong_r_r h3").text(res.objects.title);
                $(".div_zhong_r_r p").text(res.objects.content);
                $(".div_zhong_r_r span").text("公告時間:"+res.objects.createTimes);
           },
           error: function(res) {
                 alert("網路錯誤");
           }
        });
    }
    //頁面載入顯示第一條公告
    var noticeId = $(".ul_zhong_l li .span_z_2:eq(0)").attr("value");
    
    if(noticeId != '' && noticeId != null){
        firstNotice(noticeId);
    }
    //點選檢視公告
    var span_z_2_nums=$(".ul_zhong_l li .span_z_2");
    for(var i=0;i<span_z_2_nums.length;i++){
        $(".ul_zhong_l li .span_z_2:eq("+i+")").click(function(nums){
            return function(){
                //呼叫獲取公告函式
                firstNotice($(this).attr("value"));
            };
        }(i));
    }
    </script>
</body>
</html>

相關文章