7121b512acada5071b1a7b0d133b00063d39b226.svn-base
13.8 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.thinkgem.jeesite.modules.cms.web.front;
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.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.servlet.ValidateCodeServlet;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.cms.entity.Article;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.cms.entity.Comment;
import com.thinkgem.jeesite.modules.cms.entity.Link;
import com.thinkgem.jeesite.modules.cms.entity.Site;
import com.thinkgem.jeesite.modules.cms.service.ArticleDataService;
import com.thinkgem.jeesite.modules.cms.service.ArticleService;
import com.thinkgem.jeesite.modules.cms.service.CategoryService;
import com.thinkgem.jeesite.modules.cms.service.CommentService;
import com.thinkgem.jeesite.modules.cms.service.LinkService;
import com.thinkgem.jeesite.modules.cms.service.SiteService;
import com.thinkgem.jeesite.modules.cms.utils.CmsUtils;
/**
* 网站Controller
* @author ThinkGem
* @version 2013-5-29
*/
@Controller
@RequestMapping(value = "${frontPath}")
public class FrontController extends BaseController{
@Autowired
private ArticleService articleService;
@Autowired
private ArticleDataService articleDataService;
@Autowired
private LinkService linkService;
@Autowired
private CommentService commentService;
@Autowired
private CategoryService categoryService;
@Autowired
private SiteService siteService;
/**
* 网站首页
*/
@RequestMapping
public String index(Model model) {
Site site = CmsUtils.getSite(Site.defaultSiteId());
model.addAttribute("site", site);
model.addAttribute("isIndex", true);
return "modules/cms/front/themes/"+site.getTheme()+"/frontIndex";
}
/**
* 网站首页
*/
@RequestMapping(value = "index-{siteId}${urlSuffix}")
public String index(@PathVariable String siteId, Model model) {
if (siteId.equals("1")){
return "redirect:"+Global.getFrontPath();
}
Site site = CmsUtils.getSite(siteId);
// 子站有独立页面,则显示独立页面
if (StringUtils.isNotBlank(site.getCustomIndexView())){
model.addAttribute("site", site);
model.addAttribute("isIndex", true);
return "modules/cms/front/themes/"+site.getTheme()+"/frontIndex"+site.getCustomIndexView();
}
// 否则显示子站第一个栏目
List<Category> mainNavList = CmsUtils.getMainNavList(siteId);
if (mainNavList.size() > 0){
String firstCategoryId = CmsUtils.getMainNavList(siteId).get(0).getId();
return "redirect:"+Global.getFrontPath()+"/list-"+firstCategoryId+Global.getUrlSuffix();
}else{
model.addAttribute("site", site);
return "modules/cms/front/themes/"+site.getTheme()+"/frontListCategory";
}
}
/**
* 内容列表
*/
@RequestMapping(value = "list-{categoryId}${urlSuffix}")
public String list(@PathVariable String categoryId, @RequestParam(required=false, defaultValue="1") Integer pageNo,
@RequestParam(required=false, defaultValue="15") Integer pageSize, Model model) {
Category category = categoryService.get(categoryId);
if (category==null){
Site site = CmsUtils.getSite(Site.defaultSiteId());
model.addAttribute("site", site);
return "error/404";
}
Site site = siteService.get(category.getSite().getId());
model.addAttribute("site", site);
// 2:简介类栏目,栏目第一条内容
if("2".equals(category.getShowModes()) && "article".equals(category.getModule())){
// 如果没有子栏目,并父节点为跟节点的,栏目列表为当前栏目。
List<Category> categoryList = Lists.newArrayList();
if (category.getParent().getId().equals("1")){
categoryList.add(category);
}else{
categoryList = categoryService.findByParentId(category.getParent().getId(), category.getSite().getId());
}
model.addAttribute("category", category);
model.addAttribute("categoryList", categoryList);
// 获取文章内容
Page<Article> page = new Page<Article>(1, 1, -1);
Article article = new Article(category);
page = articleService.findPage(page, article, false);
if (page.getList().size()>0){
article = page.getList().get(0);
article.setArticleData(articleDataService.get(article.getId()));
articleService.updateHitsAddOne(article.getId());
}
model.addAttribute("article", article);
CmsUtils.addViewConfigAttribute(model, category);
CmsUtils.addViewConfigAttribute(model, article.getViewConfig());
return "modules/cms/front/themes/"+site.getTheme()+"/"+getTpl(article);
}else{
List<Category> categoryList = categoryService.findByParentId(category.getId(), category.getSite().getId());
// 展现方式为1 、无子栏目或公共模型,显示栏目内容列表
if("1".equals(category.getShowModes())||categoryList.size()==0){
// 有子栏目并展现方式为1,则获取第一个子栏目;无子栏目,则获取同级分类列表。
if(categoryList.size()>0){
category = categoryList.get(0);
}else{
// 如果没有子栏目,并父节点为跟节点的,栏目列表为当前栏目。
if (category.getParent().getId().equals("1")){
categoryList.add(category);
}else{
categoryList = categoryService.findByParentId(category.getParent().getId(), category.getSite().getId());
}
}
model.addAttribute("category", category);
model.addAttribute("categoryList", categoryList);
// 获取内容列表
if ("article".equals(category.getModule())){
Page<Article> page = new Page<Article>(pageNo, pageSize);
//System.out.println(page.getPageNo());
page = articleService.findPage(page, new Article(category), false);
model.addAttribute("page", page);
// 如果第一个子栏目为简介类栏目,则获取该栏目第一篇文章
if ("2".equals(category.getShowModes())){
Article article = new Article(category);
if (page.getList().size()>0){
article = page.getList().get(0);
article.setArticleData(articleDataService.get(article.getId()));
articleService.updateHitsAddOne(article.getId());
}
model.addAttribute("article", article);
CmsUtils.addViewConfigAttribute(model, category);
CmsUtils.addViewConfigAttribute(model, article.getViewConfig());
return "modules/cms/front/themes/"+site.getTheme()+"/"+getTpl(article);
}
}else if ("link".equals(category.getModule())){
Page<Link> page = new Page<Link>(1, -1);
page = linkService.findPage(page, new Link(category), false);
model.addAttribute("page", page);
}
String view = "/frontList";
if (StringUtils.isNotBlank(category.getCustomListView())){
view = "/"+category.getCustomListView();
}
CmsUtils.addViewConfigAttribute(model, category);
site =siteService.get(category.getSite().getId());
//System.out.println("else 栏目第一条内容 _2 :"+category.getSite().getTheme()+","+site.getTheme());
return "modules/cms/front/themes/"+siteService.get(category.getSite().getId()).getTheme()+view;
//return "modules/cms/front/themes/"+category.getSite().getTheme()+view;
}
// 有子栏目:显示子栏目列表
else{
model.addAttribute("category", category);
model.addAttribute("categoryList", categoryList);
String view = "/frontListCategory";
if (StringUtils.isNotBlank(category.getCustomListView())){
view = "/"+category.getCustomListView();
}
CmsUtils.addViewConfigAttribute(model, category);
return "modules/cms/front/themes/"+site.getTheme()+view;
}
}
}
/**
* 内容列表(通过url自定义视图)
*/
@RequestMapping(value = "listc-{categoryId}-{customView}${urlSuffix}")
public String listCustom(@PathVariable String categoryId, @PathVariable String customView, @RequestParam(required=false, defaultValue="1") Integer pageNo,
@RequestParam(required=false, defaultValue="15") Integer pageSize, Model model) {
Category category = categoryService.get(categoryId);
if (category==null){
Site site = CmsUtils.getSite(Site.defaultSiteId());
model.addAttribute("site", site);
return "error/404";
}
Site site = siteService.get(category.getSite().getId());
model.addAttribute("site", site);
List<Category> categoryList = categoryService.findByParentId(category.getId(), category.getSite().getId());
model.addAttribute("category", category);
model.addAttribute("categoryList", categoryList);
CmsUtils.addViewConfigAttribute(model, category);
return "modules/cms/front/themes/"+site.getTheme()+"/frontListCategory"+customView;
}
/**
* 显示内容
*/
@RequestMapping(value = "view-{categoryId}-{contentId}${urlSuffix}")
public String view(@PathVariable String categoryId, @PathVariable String contentId, Model model) {
Category category = categoryService.get(categoryId);
if (category==null){
Site site = CmsUtils.getSite(Site.defaultSiteId());
model.addAttribute("site", site);
return "error/404";
}
model.addAttribute("site", category.getSite());
if ("article".equals(category.getModule())){
// 如果没有子栏目,并父节点为跟节点的,栏目列表为当前栏目。
List<Category> categoryList = Lists.newArrayList();
if (category.getParent().getId().equals("1")){
categoryList.add(category);
}else{
categoryList = categoryService.findByParentId(category.getParent().getId(), category.getSite().getId());
}
// 获取文章内容
Article article = articleService.get(contentId);
if (article==null || !Article.DEL_FLAG_NORMAL.equals(article.getDelFlag())){
return "error/404";
}
// 文章阅读次数+1
articleService.updateHitsAddOne(contentId);
// 获取推荐文章列表
List<Object[]> relationList = articleService.findByIds(articleDataService.get(article.getId()).getRelation());
// 将数据传递到视图
model.addAttribute("category", categoryService.get(article.getCategory().getId()));
model.addAttribute("categoryList", categoryList);
article.setArticleData(articleDataService.get(article.getId()));
model.addAttribute("article", article);
model.addAttribute("relationList", relationList);
CmsUtils.addViewConfigAttribute(model, article.getCategory());
CmsUtils.addViewConfigAttribute(model, article.getViewConfig());
Site site = siteService.get(category.getSite().getId());
model.addAttribute("site", site);
// return "modules/cms/front/themes/"+category.getSite().getTheme()+"/"+getTpl(article);
return "modules/cms/front/themes/"+site.getTheme()+"/"+getTpl(article);
}
return "error/404";
}
/**
* 内容评论
*/
@RequestMapping(value = "comment", method=RequestMethod.GET)
public String comment(String theme, Comment comment, HttpServletRequest request, HttpServletResponse response, Model model) {
Page<Comment> page = new Page<Comment>(request, response);
Comment c = new Comment();
c.setCategory(comment.getCategory());
c.setContentId(comment.getContentId());
c.setDelFlag(Comment.DEL_FLAG_NORMAL);
page = commentService.findPage(page, c);
model.addAttribute("page", page);
model.addAttribute("comment", comment);
return "modules/cms/front/themes/"+theme+"/frontComment";
}
/**
* 内容评论保存
*/
@ResponseBody
@RequestMapping(value = "comment", method=RequestMethod.POST)
public String commentSave(Comment comment, String validateCode,@RequestParam(required=false) String replyId, HttpServletRequest request) {
if (StringUtils.isNotBlank(validateCode)){
if (ValidateCodeServlet.validate(request, validateCode)){
if (StringUtils.isNotBlank(replyId)){
Comment replyComment = commentService.get(replyId);
if (replyComment != null){
comment.setContent("<div class=\"reply\">"+replyComment.getName()+":<br/>"
+replyComment.getContent()+"</div>"+comment.getContent());
}
}
comment.setIp(request.getRemoteAddr());
comment.setCreateDate(new Date());
comment.setDelFlag(Comment.DEL_FLAG_AUDIT);
commentService.save(comment);
return "{result:1, message:'提交成功。'}";
}else{
return "{result:2, message:'验证码不正确。'}";
}
}else{
return "{result:2, message:'验证码不能为空。'}";
}
}
/**
* 站点地图
*/
@RequestMapping(value = "map-{siteId}${urlSuffix}")
public String map(@PathVariable String siteId, Model model) {
Site site = CmsUtils.getSite(siteId!=null?siteId:Site.defaultSiteId());
model.addAttribute("site", site);
return "modules/cms/front/themes/"+site.getTheme()+"/frontMap";
}
private String getTpl(Article article){
if(StringUtils.isBlank(article.getCustomContentView())){
String view = null;
Category c = article.getCategory();
boolean goon = true;
do{
if(StringUtils.isNotBlank(c.getCustomContentView())){
view = c.getCustomContentView();
goon = false;
}else if(c.getParent() == null || c.getParent().isRoot()){
goon = false;
}else{
c = c.getParent();
}
}while(goon);
return StringUtils.isBlank(view) ? Article.DEFAULT_TEMPLATE : view;
}else{
return article.getCustomContentView();
}
}
}