码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb项目开发案例精粹-第4章博客网站系统-005action层

时间:2016-03-26 18:45:26      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

1.

 1 package com.sanqing.action;
 2 
 3 import java.util.Date;
 4 import java.util.Map;
 5 
 6 import com.opensymphony.xwork2.ActionContext;
 7 import com.opensymphony.xwork2.ActionSupport;
 8 import com.sanqing.po.Article;
 9 import com.sanqing.service.ArticleService;
10 
11 public class AddArticle extends ActionSupport {
12     private String title;
13     private String content;
14     private ArticleService articleService;
15     
16     public ArticleService getArticleService() {
17         return articleService;
18     }
19     public void setArticleService(ArticleService articleService) {
20         this.articleService = articleService;
21     }
22     public String getTitle() {
23         return title;
24     }
25     public void setTitle(String title) {
26         this.title = title;
27     }
28     public String getContent() {
29         return content;
30     }
31     public void setContent(String content) {
32         this.content = content;
33     }
34     public String execute() throws Exception {
35         Map session = ActionContext.getContext().getSession();
36         String username = (String) session.get("username");
37         
38         //封装一个PO类的对象
39         Article article = new Article();
40         article.setTitle(title);
41         article.setContent(content);
42         article.setUsername(username);
43         article.setDate(new Date());
44         article.setHasread(0);
45         articleService.addArticle(article);
46         return this.SUCCESS;
47     }
48 }

 

2.

 1 package com.sanqing.action;
 2 
 3 import java.util.Map;
 4 
 5 import com.opensymphony.xwork2.ActionContext;
 6 import com.opensymphony.xwork2.ActionSupport;
 7 import com.sanqing.po.Critique;
 8 import com.sanqing.service.CritiqueService;
 9 
10 public class AddCritique extends ActionSupport {
11     private CritiqueService critiqueService;
12     private int id;
13     private String content;
14 
15     public int getId() {
16         return id;
17     }
18 
19     public void setId(int id) {
20         this.id = id;
21     }
22 
23     public String getContent() {
24         return content;
25     }
26 
27     public void setContent(String content) {
28         this.content = content;
29     }
30 
31     public CritiqueService getCritiqueService() {
32         return critiqueService;
33     }
34 
35     public void setCritiqueService(CritiqueService critiqueService) {
36         this.critiqueService = critiqueService;
37     }
38 
39     public String execute() throws Exception {
40         Map session = ActionContext.getContext().getSession();
41         String username = (String) session.get("username");
42         Critique critique = new Critique();
43         if(username == null || "".equals(username)) {
44             critique.setUsername("匿名");
45         } else {
46             critique.setUsername(username);
47         }
48         critique.setAId(id);
49         critique.setContent(content);
50 
51         // 保存评论
52         this.critiqueService.addCritique(critique);
53 
54         return this.SUCCESS;
55     }
56 
57 }

 

3.EditBlogInfo-validation.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE validators PUBLIC 
 3           "-//OpenSymphony Group//XWork Validator 1.0.2//EN" 
 4           "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
 5 <validators>
 6     <validator type="requiredstring">
 7         <param name="fieldName">blogtitle</param>
 8         <param name="trim">true</param>
 9         <message>必须输入博客标题</message>
10     </validator>
11 
12     <validator type="regex">
13         <param name="fieldName">blogtitle</param>
14         <param name="expression"><![CDATA[([\u4e00-\u9fa5,a-z,A-Z,0-9]{4,20})]]></param>
15         <message>博客标题长度必须为4至20之间</message>
16     </validator>
17 
18     <validator type="requiredstring">
19         <param name="fieldName">idiograph</param>
20         <param name="trim">true</param>
21         <message>必须输入个性签名</message>
22     </validator>
23 
24     <validator type="regex">
25         <param name="fieldName">idiograph</param>
26         <param name="expression"><![CDATA[([\u4e00-\u9fa5,a-z,A-Z,0-9]{4,20})]]></param>
27         <message>个性签名长度必须为4至20之间</message>
28     </validator>
29 </validators>

 

4.

 1 package com.sanqing.action;
 2 
 3 import java.util.Map;
 4 
 5 import com.opensymphony.xwork2.ActionContext;
 6 import com.opensymphony.xwork2.ActionSupport;
 7 import com.sanqing.po.BlogInfo;
 8 import com.sanqing.service.BlogInfoService;
 9 
10 public class EditBlogInfo extends ActionSupport {
11     private String blogtitle;
12     private String idiograph;
13     private BlogInfoService blogInfoService;
14 
15     public BlogInfoService getBlogInfoService() {
16         return blogInfoService;
17     }
18 
19     public void setBlogInfoService(BlogInfoService blogInfoService) {
20         this.blogInfoService = blogInfoService;
21     }
22 
23     public String getBlogtitle() {
24         return blogtitle;
25     }
26 
27     public void setBlogtitle(String blogtitle) {
28         this.blogtitle = blogtitle;
29     }
30 
31     public String getIdiograph() {
32         return idiograph;
33     }
34 
35     public void setIdiograph(String idiograph) {
36         this.idiograph = idiograph;
37     }
38 
39     public String execute() throws Exception {
40 //        //获得request
41 //        HttpServletRequest request = ServletActionContext.getRequest();
42 //        //获得session
43 //        HttpSession session =request.getSession();
44 //        //获得username
45 //        String username = (String) session.getAttribute("username");
46         
47         Map session = ActionContext.getContext().getSession();
48         String username = (String) session.get("username");
49         
50         BlogInfo blogInfo = new BlogInfo();
51         //设置用户名
52         blogInfo.setUsername(username);
53         //设置博客标题
54         blogInfo.setBlogtitle(blogtitle);
55         //设置个性签名
56         blogInfo.setIdiograph(idiograph);
57         //调用业务逻辑组件来完成设置
58         blogInfoService.setBlogInfo(blogInfo);
59         
60         return this.SUCCESS;
61     }
62 
63 }

 

5.

 1 package com.sanqing.action;
 2 
 3 import java.util.Map;
 4 
 5 import com.opensymphony.xwork2.ActionContext;
 6 import com.opensymphony.xwork2.ActionSupport;
 7 import com.sanqing.po.BlogInfo;
 8 import com.sanqing.service.BlogInfoService;
 9 
10 public class GetBlogInfo extends ActionSupport {
11 
12     private BlogInfoService blogInfoService;
13 
14     public BlogInfoService getBlogInfoService() {
15         return blogInfoService;
16     }
17 
18     public void setBlogInfoService(BlogInfoService blogInfoService) {
19         this.blogInfoService = blogInfoService;
20     }
21     
22     public String execute() throws Exception {
23         //获得usrname
24         Map session = ActionContext.getContext().getSession();
25         String username = (String) session.get("username");
26         
27         //通过业务逻辑组件来查询
28         BlogInfo bloginfo  = blogInfoService.getBlogInfo(username);
29         
30         if(bloginfo != null) {
31             session.put("blogtitle", bloginfo.getBlogtitle());
32             session.put("idiograph", bloginfo.getIdiograph());
33         }
34         return this.SUCCESS;
35     }
36 }

 

6.

 1 package com.sanqing.action;
 2 
 3 import java.util.Map;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 import com.opensymphony.xwork2.ActionContext;
10 import com.opensymphony.xwork2.ActionSupport;
11 import com.sanqing.po.User;
12 import com.sanqing.service.UserService;
13 
14 public class Login extends ActionSupport {
15     private String username;
16     private String password;
17     private UserService userService;
18 
19     public UserService getUserService() {
20         return userService;
21     }
22 
23     public void setUserService(UserService userService) {
24         this.userService = userService;
25     }
26 
27     public String getUsername() {
28         return username;
29     }
30 
31     public void setUsername(String username) {
32         this.username = username;
33     }
34 
35     public String getPassword() {
36         return password;
37     }
38 
39     public void setPassword(String password) {
40         this.password = password;
41     }
42 
43     public String execute() throws Exception {
44         User user = new User();
45         user.setUsername(username);
46         user.setPassword(password);
47         //获得request
48         HttpServletRequest request = ServletActionContext.getRequest();
49         if(userService.loginUser(user)) {
50             request.setAttribute("url", "user/getBlogInfo.action");
51             request.setAttribute("info", "登陆成功");
52             //将username保存到session范围中
53 //            HttpSession session =request.getSession();
54             Map session = ActionContext.getContext().getSession();
55 //            session.setAttribute("username", username);    
56             session.put("username", username);
57             return SUCCESS;
58         } else {
59             request.setAttribute("url", "login.jsp");
60             request.setAttribute("info", "登陆失败");
61             return ERROR;
62         }
63     }
64 
65 }

 

7.

 1 package com.sanqing.action;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 import java.util.Map;
 9 import java.util.UUID;
10 
11 import org.apache.commons.io.FilenameUtils;
12 import org.apache.struts2.ServletActionContext;
13 
14 import com.opensymphony.xwork2.ActionSupport;
15 
16 public class PhotoUpload  extends ActionSupport{
17     private File myFile;
18     private String myFileContentType;
19     private String myFileFileName;
20 
21     public File getMyFile() {
22         return myFile;
23     }
24 
25     public void setMyFile(File myFile) {
26         this.myFile = myFile;
27     }
28 
29     public String getMyFileContentType() {
30         return myFileContentType;
31     }
32 
33     public void setMyFileContentType(String myFileContentType) {
34         this.myFileContentType = myFileContentType;
35     }
36 
37     public String getMyFileFileName() {
38         return myFileFileName;
39     }
40 
41     public void setMyFileFileName(String myFileFileName) {
42         this.myFileFileName = myFileFileName;
43     }
44 
45     public String execute() throws Exception {
46         //获得username
47         Map session = ServletActionContext.getContext().getSession();
48         String username = (String) session.get("username");
49         
50         //创建一个输入流
51         InputStream is = new FileInputStream(myFile);
52         //设置文件保存目录
53         String photoPath = 
54             ServletActionContext.getServletContext().getRealPath("/user/photo/" + username);
55         File filePhotoPath = new File(photoPath);
56         if(!filePhotoPath.isDirectory()) {
57             filePhotoPath.mkdir();
58         }
59         
60         //解决中文文件名问题
61         String extension = FilenameUtils.getExtension(this.getMyFileFileName());
62         String filename = UUID.randomUUID().toString() + "."+ extension;
63         
64         //设置目标文件
65         File tofile = new File(photoPath,filename);
66         //使用输出流来包装目标文件
67         OutputStream os = new FileOutputStream(tofile);
68         byte[] buffer = new byte[1024];
69         int length = 0;
70         while((length = is.read(buffer)) > 0) {
71             os.write(buffer, 0, length);
72         }
73         //关闭输入流
74         is.close();
75         //关闭输出流
76         os.close();
77         
78         return this.SUCCESS;
79     }
80 
81     
82 }

 

8.Register-validation.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE validators PUBLIC 
 3           "-//OpenSymphony Group//XWork Validator 1.0.2//EN" 
 4           "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
 5 <validators>
 6     <validator type="requiredstring">
 7         <param name="fieldName">username</param>
 8         <param name="trim">true</param>
 9         <message>必须输入用户名</message>
10     </validator>
11 
12     <validator type="regex">
13         <param name="fieldName">username</param>
14         <param name="expression"><![CDATA[(\w{6,20})]]></param>
15         <message>用户名长度必须为6至20之间</message>
16     </validator>
17 
18     <validator type="requiredstring">
19         <param name="fieldName">password</param>
20         <param name="trim">true</param>
21         <message>必须输入密码</message>
22     </validator>
23 
24     <validator type="regex">
25         <param name="fieldName">password</param>
26         <param name="expression"><![CDATA[(\w{6,20})]]></param>
27         <message>密码长度必须为6至20之间</message>
28     </validator>
29 
30     <validator type="requiredstring">
31         <param name="fieldName">repassword</param>
32         <param name="trim">true</param>
33         <message>必须输入确认密码</message>
34     </validator>
35 
36     <validator type="regex">
37         <param name="fieldName">repassword</param>
38         <param name="expression"><![CDATA[(\w{6,20})]]></param>
39         <message>确认密码长度必须为6至20之间</message>
40     </validator>
41 
42     <validator type="fieldexpression">
43         <param name="fieldName">repassword</param>
44         <param name="expression"><![CDATA[(repassword==password)]]></param>
45         <message>密码和确认密码必须一致</message>
46     </validator>
47 
48     <validator type="requiredstring">
49         <param name="fieldName">nickname</param>
50         <param name="trim">true</param>
51         <message>必须输入昵称</message>
52     </validator>
53 
54     <validator type="regex">
55         <param name="fieldName">nickname</param>
56         <param name="expression"><![CDATA[([\u4e00-\u9fa5]{2,20})]]></param>
57         <message>昵称长度必须为2至20之间</message>
58     </validator>
59     
60         <validator type="requiredstring">
61         <param name="fieldName">answer</param>
62         <param name="trim">true</param>
63         <message>必须输入密码保护答案</message>
64     </validator>
65 
66     <validator type="regex">
67         <param name="fieldName">answer</param>
68         <param name="expression"><![CDATA[([\u4e00-\u9fa5]{5,50})]]></param>
69         <message>密码保护答案长度必须为5至50之间</message>
70     </validator>
71 </validators>

 

9.

 1 package com.sanqing.action;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 
 5 import org.apache.struts2.ServletActionContext;
 6 
 7 import com.opensymphony.xwork2.ActionContext;
 8 import com.opensymphony.xwork2.ActionSupport;
 9 import com.sanqing.po.User;
10 import com.sanqing.service.UserService;
11 
12 public class Register extends ActionSupport{
13     private String username;
14     private String password;
15     private String repassword;
16     private String nickname;
17     private String question;
18     private String answer;
19     private UserService userService;
20     
21     public UserService getUserService() {
22         return userService;
23     }
24     public void setUserService(UserService userService) {
25         this.userService = userService;
26     }
27     public String getUsername() {
28         return username;
29     }
30     public void setUsername(String username) {
31         this.username = username;
32     }
33     public String getPassword() {
34         return password;
35     }
36     public void setPassword(String password) {
37         this.password = password;
38     }
39     public String getRepassword() {
40         return repassword;
41     }
42     public void setRepassword(String repassword) {
43         this.repassword = repassword;
44     }
45     public String getNickname() {
46         return nickname;
47     }
48     public void setNickname(String nickname) {
49         this.nickname = nickname;
50     }
51     public String getQuestion() {
52         return question;
53     }
54     public void setQuestion(String question) {
55         this.question = question;
56     }
57     public String getAnswer() {
58         return answer;
59     }
60     public void setAnswer(String answer) {
61         this.answer = answer;
62     }
63     
64     
65     public String execute() throws Exception {
66         //封装一个user对象
67         User user = new User();
68         user.setUsername(username);
69         user.setPassword(password);
70         user.setNickname(nickname);
71         user.setQuestion(question);
72         user.setAnswer(answer);
73         //获得request
74         HttpServletRequest request = ServletActionContext.getRequest();
75         if(userService.registerUser(user)) {
76             request.setAttribute("url", "login.jsp");
77             request.setAttribute("info", "注册成功");
78             return SUCCESS;
79         }else {
80             request.setAttribute("url", "register.jsp");
81             request.setAttribute("info", "注册失败");
82             return ERROR;
83         }
84     }
85 
86 }

 

10.

 1 package com.sanqing.action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Map;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 
 9 import org.apache.struts2.ServletActionContext;
10 
11 import com.opensymphony.xwork2.ActionContext;
12 import com.opensymphony.xwork2.ActionSupport;
13 import com.sanqing.fenye.Page;
14 import com.sanqing.fenye.Result;
15 import com.sanqing.po.Article;
16 import com.sanqing.service.ArticleService;
17 
18 public class ShowAllArticle extends ActionSupport{
19     private ArticleService articleService;
20     private int currentPage;
21 
22     public int getCurrentPage() {
23         return currentPage;
24     }
25     public void setCurrentPage(int currentPage) {
26         this.currentPage = currentPage;
27     }
28     public ArticleService getArticleService() {
29         return articleService;
30     }
31     public void setArticleService(ArticleService articleService) {
32         this.articleService = articleService;
33     }
34     
35     public String execute() throws Exception {
36         //通过调用业务逻辑组件来完成查询
37         Page page = new Page();
38         page.setCurrentPage(this.getCurrentPage());
39         page.setEveryPage(10);
40         
41         Result result = articleService.showArticleByPage(page);
42         page = result.getPage();
43         List<Article> all = result.getList();
44         
45         List critiqueCounts = new ArrayList();
46         for(Article article : all) {
47             critiqueCounts.add(articleService.getCritiqueCount(article.getId()));
48         }
49         
50         //把查询到的结果保存在一个范围,request
51         HttpServletRequest request = ServletActionContext.getRequest();
52         request.setAttribute("all", all);
53         request.setAttribute("page", page);
54         request.setAttribute("critiqueCounts",critiqueCounts);
55         return this.SUCCESS;
56     }
57     
58 }

 

11.

  1 package com.sanqing.action;
  2 
  3 import java.text.SimpleDateFormat;
  4 import java.util.Date;
  5 import java.util.Map;
  6 
  7 import javax.servlet.http.HttpServletRequest;
  8 
  9 import org.apache.struts2.ServletActionContext;
 10 
 11 import com.opensymphony.xwork2.ActionContext;
 12 import com.opensymphony.xwork2.ActionSupport;
 13 import com.sanqing.fenye.Page;
 14 import com.sanqing.fenye.Result;
 15 import com.sanqing.po.Article;
 16 import com.sanqing.po.BlogInfo;
 17 import com.sanqing.service.ArticleService;
 18 import com.sanqing.service.BlogInfoService;
 19 import com.sanqing.service.CritiqueService;
 20 import com.sanqing.service.DianjiliangService;
 21 
 22 public class ShowArticle extends ActionSupport {
 23     // 业务逻辑组件属性
 24     private ArticleService articleService;
 25     // id属性
 26     private int id;
 27     // 点击量的业务逻辑组件
 28     private DianjiliangService dianjiliangService;
 29     // 评论的业务逻辑组件
 30     private CritiqueService critiqueService;
 31     //设置当前页
 32     private int currentPage;
 33     
 34     //username
 35     private String username;
 36     
 37     private BlogInfoService blogInfoService;
 38 
 39     public BlogInfoService getBlogInfoService() {
 40         return blogInfoService;
 41     }
 42 
 43     public void setBlogInfoService(BlogInfoService blogInfoService) {
 44         this.blogInfoService = blogInfoService;
 45     }
 46 
 47     public String getUsername() {
 48         return username;
 49     }
 50 
 51     public void setUsername(String username) {
 52         this.username = username;
 53     }
 54 
 55     public int getCurrentPage() {
 56         return currentPage;
 57     }
 58 
 59     public void setCurrentPage(int currentPage) {
 60         this.currentPage = currentPage;
 61     }
 62 
 63     public CritiqueService getCritiqueService() {
 64         return critiqueService;
 65     }
 66 
 67     public void setCritiqueService(CritiqueService critiqueService) {
 68         this.critiqueService = critiqueService;
 69     }
 70 
 71     public DianjiliangService getDianjiliangService() {
 72         return dianjiliangService;
 73     }
 74 
 75     public void setDianjiliangService(DianjiliangService dianjiliangService) {
 76         this.dianjiliangService = dianjiliangService;
 77     }
 78 
 79     public int getId() {
 80         return id;
 81     }
 82 
 83     public void setId(int id) {
 84         this.id = id;
 85     }
 86 
 87     public ArticleService getArticleService() {
 88         return articleService;
 89     }
 90 
 91     public void setArticleService(ArticleService articleService) {
 92         this.articleService = articleService;
 93     }
 94 
 95     public String execute() throws Exception {
 96         // 将文章设置到request范围
 97         HttpServletRequest request = ServletActionContext.getRequest();
 98         // 按ID查询文章
 99         Article article = articleService.showArticle(id);
100         // 获得用户IP
101         String IP = request.getRemoteAddr();
102         // 获得当前时间
103         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
104         String stime = sdf.format(new Date());
105         Date time = sdf.parse(stime);
106 
107         if (!dianjiliangService.isVistor(id, IP, time)) {
108             // 点击量增加
109             article.setHasread(article.getHasread() + 1);
110         }
111         // 将更新的Article保存到数据表中
112         articleService.addArticle(article);
113         
114         
115         //显示评论
116         Page page = new Page();
117         page.setCurrentPage(this.getCurrentPage());
118         page.setEveryPage(5);
119         
120         Result result = critiqueService.showCritiqueByPage(id, page);
121         
122         request.setAttribute("page", result.getPage());
123         request.setAttribute("allCri", result.getList());
124         request.setAttribute("article", article);
125         
126         //取得个性化设置
127         //通过业务逻辑组件来查询
128         if(username != null || !"".equals(username)) {
129             Map session = ActionContext.getContext().getSession();
130             BlogInfo bloginfo  = blogInfoService.getBlogInfo(username);
131             if(bloginfo != null) {
132                 session.put("blogtitle", bloginfo.getBlogtitle());
133                 session.put("idiograph", bloginfo.getIdiograph());
134             }
135         }
136         return this.SUCCESS;
137     }
138 
139 }

 

12.

 1 package com.sanqing.action;
 2 
 3 import java.io.File;
 4 import java.util.Map;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 
 8 import org.apache.struts2.ServletActionContext;
 9 
10 import com.opensymphony.xwork2.ActionSupport;
11 
12 public class ShowPhoto  extends ActionSupport{
13 
14     public String execute() throws Exception {
15         //获得username
16         Map session = ServletActionContext.getContext().getSession();
17         String username = (String) session.get("username");
18         
19         //设置文件目录
20         String photoPath = 
21             ServletActionContext.getServletContext().getRealPath("/user/photo/" + username);
22         //使用File封装
23         File fphotoPath = new File(photoPath);
24         //获得相册目录的所有图片文件
25         String[] photoList = fphotoPath.list();
26         //将图片列表设置到request范围
27         HttpServletRequest request = ServletActionContext.getRequest();
28         request.setAttribute("photoList", photoList);
29         return super.execute();
30     }
31 
32 }

 

13

 1 package com.sanqing.action;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.struts2.ServletActionContext;
 6 
 7 import com.opensymphony.xwork2.ActionSupport;
 8 import com.sanqing.fenye.Page;
 9 import com.sanqing.fenye.Result;
10 import com.sanqing.po.Article;
11 import com.sanqing.rss.CreateRss;
12 import com.sanqing.service.ArticleService;
13 
14 public class ShowRSS extends ActionSupport {
15     private ArticleService articleService;
16     public ArticleService getArticleService() {
17         return articleService;
18     }
19     public void setArticleService(ArticleService articleService) {
20         this.articleService = articleService;
21     }
22     public String execute() throws Exception {
23         Page page = new Page();        //分页信息
24         page.setCurrentPage(0);        //设置当前页为第一页
25         page.setEveryPage(10);        //每页显示10条记录
26         Result result = articleService.
27             showArticleByPage(page);//通过调用业务逻辑组件来完成查询
28         page = result.getPage();
29         List<Article> all = result.getList();//获得文章结果集
30         String filePath = ServletActionContext.
31             getServletContext().getRealPath("/rss.xml");//设置订阅文件地址
32         CreateRss.publishRss(all, filePath);//写入订阅文件
33         return this.SUCCESS;
34     }
35 }

 

14.

 1 package com.sanqing.action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Map;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 
 9 import org.apache.struts2.ServletActionContext;
10 
11 import com.opensymphony.xwork2.ActionContext;
12 import com.opensymphony.xwork2.ActionSupport;
13 import com.sanqing.fenye.Page;
14 import com.sanqing.fenye.Result;
15 import com.sanqing.po.Article;
16 import com.sanqing.service.ArticleService;
17 
18 public class ShowUserAllArticle extends ActionSupport{
19     private ArticleService articleService;
20     private int currentPage;
21     private String username;
22 
23     public String getUsername() {
24         return username;
25     }
26     public void setUsername(String username) {
27         this.username = username;
28     }
29     public int getCurrentPage() {
30         return currentPage;
31     }
32     public void setCurrentPage(int currentPage) {
33         this.currentPage = currentPage;
34     }
35     public ArticleService getArticleService() {
36         return articleService;
37     }
38     public void setArticleService(ArticleService articleService) {
39         this.articleService = articleService;
40     }
41     
42     public String execute() throws Exception {
43         if(username == null || "".equals(username)) {
44             //首先要获得session
45             Map session = ActionContext.getContext().getSession();
46             //获得username
47             username = (String) session.get("username");
48         }
49         
50         //通过调用业务逻辑组件来完成查询
51         Page page = new Page();
52         page.setCurrentPage(this.getCurrentPage());
53         page.setEveryPage(5);
54         
55         Result result = articleService.showUserArticleByPage(username, page);
56         page = result.getPage();
57         List<Article> all = result.getList();
58         List critiqueCounts = new ArrayList();
59         
60         for(Article article : all) {
61             critiqueCounts.add(articleService.getCritiqueCount(article.getId()));
62         }
63         
64         //把查询到的结果保存在一个范围,request
65         HttpServletRequest request = ServletActionContext.getRequest();
66         request.setAttribute("all", all);
67         request.setAttribute("page", page);
68         request.setAttribute("critiqueCounts",critiqueCounts);
69         return this.SUCCESS;
70     }
71     
72 }

 

JavaWeb项目开发案例精粹-第4章博客网站系统-005action层

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5323379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!