13.5.6 公告管理

13.5.6 公告管理

处理公告的NoticeController代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.fkit.hrm.controller;

import java.util.List;
import javax.servlet.http.HttpSession;
import org.fkit.hrm.domain.Notice;
import org.fkit.hrm.domain.User;
import org.fkit.hrm.service.HrmService;
import org.fkit.hrm.util.common.HrmConstants;
import org.fkit.hrm.util.tag.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
* @Description: 处理公告请求控制器
*/

@Controller
public class NoticeController
{
/**
* 自动注入UserService
*/
@Autowired
@Qualifier("hrmService")
private HrmService hrmService;

/**
* 处理/login请求
*/
@RequestMapping(value = "/notice/selectNotice")
public String selectNotice(Model model, Integer pageIndex,
@ModelAttribute Notice notice)
{
PageModel pageModel = new PageModel();
if (pageIndex != null)
{
pageModel.setPageIndex(pageIndex);
}
/** 查询用户信息 */
List<Notice> notices = hrmService.findNotice(notice, pageModel);
model.addAttribute("notices", notices);
model.addAttribute("pageModel", pageModel);
return "notice/notice";

}

/**
* 处理添加请求
* @param Integer id 要显示的公告id
* @param Model model
*/
@RequestMapping(value = "/notice/previewNotice")
public String previewNotice(Integer id, Model model)
{

Notice notice = hrmService.findNoticeById(id);

model.addAttribute("notice", notice);
// 返回
return "notice/previewNotice";
}

/**
* 处理删除公告请求
* @param String ids 需要删除的id字符串
* @param ModelAndView mv
*/
@RequestMapping(value = "/notice/removeNotice")
public ModelAndView removeNotice(String ids, ModelAndView mv)
{
// 分解id字符串
String[] idArray = ids.split(",");
for (String id : idArray)
{
// 根据id删除公告
hrmService.removeNoticeById(Integer.parseInt(id));
}
// 设置客户端跳转到查询请求
mv.setViewName("redirect:/notice/selectNotice");
// 返回ModelAndView
return mv;
}

/**
* 处理添加请求
* @param String flag 标记, 1表示跳转到添加页面,2表示执行添加操作
* @param Notice notice 要添加的公告对象
* @param ModelAndView mv
*/
@RequestMapping(value = "/notice/addNotice")
public ModelAndView addNotice(String flag, @ModelAttribute Notice notice,
ModelAndView mv, HttpSession session)
{
if (flag.equals("1"))
{
mv.setViewName("notice/showAddNotice");
} else
{
User user = (User) session.getAttribute(HrmConstants.USER_SESSION);
notice.setUser(user);
hrmService.addNotice(notice);
mv.setViewName("redirect:/notice/selectNotice");
}
// 返回
return mv;
}

/**
* 处理添加请求
* @param String flag 标记, 1表示跳转到修改页面,2表示执行修改操作
* @param Notice notice 要添加的公告对象
* @param ModelAndView mv
*/
@RequestMapping(value = "/notice/updateNotice")
public ModelAndView updateNotice(String flag, @ModelAttribute Notice notice,
ModelAndView mv, HttpSession session)
{
if (flag.equals("1"))
{
Notice target = hrmService.findNoticeById(notice.getId());
mv.addObject("notice", target);
mv.setViewName("notice/showUpdateNotice");
} else
{
hrmService.modifyNotice(notice);
mv.setViewName("redirect:/notice/selectNotice");
}
// 返回
return mv;
}
}

单击左侧菜单”公告管理“下面的”添加公告“命令,跳转到添加公告界面.
输入需要添加的公告标题和公告内容,单击”添加“按钮,若添加成功则跳转到公告查询界面,显示所有公告信息。
选择每一行公告最后一列的”预览“按钮,则可以预览公告内容。
输入公告名称和公告内容,单击”搜索“按钮可以完成模糊査询功能
选择每一行的”操作”按钮,可以进入修改页面,对选中的公告进行修改操作
选择每一行第一列的复选框,单击”删除“按钮,则可以对选中的公告进行删除操作