巴巴运动网学习笔记(36-40)

1.创建品牌的业务bean,并单元测试

View Code
 1 package cnblogs.xiaoqiu.bean.product;
2
3 import java.io.Serializable;
4
5 import javax.persistence.Column;
6 import javax.persistence.Entity;
7 import javax.persistence.Id;
8 @Entity
9 public class Brand implements Serializable{
10 private static final long serialVersionUID = -79226975589304722L;
11 //品牌id
12 private String id;
13 //品牌名称
14 private String name;
15 //品牌logo图片路径(/images/brand/2008/12/12/xxxxxxx.jpg)
16 private String imagePath;
17 //品牌是否被删除的标识
18 private boolean isVisible = true;
19
20 public Brand(){}
21
22 public Brand(String name, String imagePath) {
23 this.name = name;
24 this.imagePath = imagePath;
25 }
26 //采用uuid的方式,自己手工注入ID
27 @Id @Column(length=36)
28 public String getId() {
29 return id;
30 }
31 public void setId(String id) {
32 this.id = id;
33 }
34 @Column(length=40,nullable=false)
35 public String getName() {
36 return name;
37 }
38 public void setName(String name) {
39 this.name = name;
40 }
41 @Column(length=80)
42 public String getImagePath() {
43 return imagePath;
44 }
45 public void setImagePath(String imagePath) {
46 this.imagePath = imagePath;
47 }
48 @Column(nullable=false)
49 public boolean isVisible() {
50 return isVisible;
51 }
52 public void setVisible(boolean isVisible) {
53 this.isVisible = isVisible;
54 }
55 @Override
56 public int hashCode() {
57 final int prime = 31;
58 int result = 1;
59 result = prime * result + ((id == null) ? 0 : id.hashCode());
60 return result;
61 }
62 @Override
63 public boolean equals(Object obj) {
64 if (this == obj)
65 return true;
66 if (obj == null)
67 return false;
68 if (getClass() != obj.getClass())
69 return false;
70 Brand other = (Brand) obj;
71 if (id == null) {
72 if (other.id != null)
73 return false;
74 } else if (!id.equals(other.id))
75 return false;
76 return true;
77 }
78 }

2.实现品牌分页显示的action与formbean(新建一个BrandAction来实现分页显示,新建一个formbean用来保存和分页相关的数据)

View Code
 1 package cnblogs.xiaoqiu.web.action.product;
2
3 import java.util.ArrayList;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import javax.annotation.Resource;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 import org.apache.struts.action.Action;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
13 import org.springframework.stereotype.Controller;
14
15 import cnblogs.xiaoqiu.bean.PagingBean;
16 import cnblogs.xiaoqiu.bean.ScrollResult;
17 import cnblogs.xiaoqiu.bean.product.Brand;
18 import cnblogs.xiaoqiu.service.product.BrandService;
19 import cnblogs.xiaoqiu.web.action.formbean.BrandForm;
20 @Controller("/control/product/brand/list")
21 public class BrandAction extends Action {
22 @Resource(name="brandServiceImpl")
23 BrandService brandService;
24 @Override
25 public ActionForward execute(ActionMapping mapping, ActionForm form,
26 HttpServletRequest request, HttpServletResponse response)
27 throws Exception {
28 BrandForm brandForm = (BrandForm)form;
29 int pageCounts = 10;
30 int currentPage = brandForm.getCurrentPage();
31 int begin = (currentPage-1)*pageCounts;
32 LinkedHashMap<String, String> orderHashMap = new LinkedHashMap<String, String>();
33 orderHashMap.put("id", "asc");
34 StringBuffer jpql = new StringBuffer(" p.visible=?1");
35 List<Object> params = new ArrayList<Object>();
36 params.add(true);
37 if(brandForm.getName()!=null&&!brandForm.equals("")){
38 jpql.append(" and p.name like ?"+(params.size()+1));
39 params.add("%"+brandForm.getName()+"%");
40 }
41 ScrollResult<Brand> scrollResult = brandService.getScrollResult(Brand.class, begin, pageCounts, orderHashMap, jpql.toString(), params.toArray());
42 PagingBean<Brand> pagingBean = new PagingBean<Brand>(currentPage,scrollResult,pageCounts);
43 request.setAttribute("pagingBean", pagingBean);
44 return mapping.findForward("list");
45 }
46 }

 

View Code
 1 package cnblogs.xiaoqiu.web.action.formbean;
2
3 import org.apache.struts.upload.FormFile;
4
5 public class BrandForm extends BaseForm {
6 private static final long serialVersionUID = -72250115273774274L;
7 private String id;
8 private FormFile image;
9 private String imagePath;
10
11
12 public String getImagePath() {
13 return imagePath;
14 }
15
16 public void setImagePath(String imagePath) {
17 this.imagePath = imagePath;
18 }
19
20 public String getId() {
21 return id;
22 }
23
24 public void setId(String id) {
25 this.id = id;
26 }
27
28 public FormFile getImage() {
29 return image;
30 }
31
32 public void setImage(FormFile image) {
33 this.image = image;
34 }
35 }


3.实现品牌分页显示的jsp页面,并单元测试


4.实现品牌添加(上传和显示logo图片),并单元测试

View Code
 1 /**
2 * 提供添加界面
3 * @param mapping
4 * @param form
5 * @param request
6 * @param response
7 * @return
8 * @throws Exception
9 */
10 public ActionForward addUI(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
11 throws Exception {
12 return mapping.findForward("add");
13 }
14 /**
15 * 提供添加功能
16 * @param mapping
17 * @param form
18 * @param request
19 * @param response
20 * @return
21 * @throws Exception
22 */
23 public ActionForward add(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
24 throws Exception {
25 // /images/brand/2008/12/12/03/xxxxxxxx.gif
26 BrandForm brandForm = (BrandForm)form;
27 Brand brand = new Brand();
28 String message="品牌添加成功";
29 brand.setName(brandForm.getName());
30 if(brandForm.getImage()!=null&&brandForm.getImage().getFileSize()>0){
31 if(brandForm.isImageTypeValidate(brandForm.getImage())){
32 StringBuffer imageDir = new StringBuffer("/images/brand/");
33 SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd/HH/");
34 imageDir.append(dateFormat.format(new Date()));
35 String imageRealDir = request.getSession().getServletContext().getRealPath(imageDir.toString());
36 File fileDir = new File(imageRealDir);
37 if(!fileDir.exists()) fileDir.mkdirs();
38 StringBuffer imageName = new StringBuffer(UUID.randomUUID().toString());
39 String imageFormat = brandForm.getImage().getFileName().substring(brandForm.getImage().getFileName().lastIndexOf('.'));
40 imageName.append(imageFormat);
41 FileOutputStream outputStream = new FileOutputStream(new File(imageRealDir,imageName.toString()));
42 outputStream.write(brandForm.getImage().getFileData());
43 outputStream.close();
44 brand.setImagePath(imageDir.append(imageName).toString());
45 }
46 else{
47 message = "品牌添加失败";
48 }
49 }
50 brandService.save(brand);
51 request.setAttribute("jump", PropertiesTool.getValue("control.product.brand.list"));
52 request.setAttribute("message", message);
53 return mapping.findForward("message");
54 }

5.实现品牌的修改和查询功能,并单元测试

View Code
 1 /**
2 * 提供修改界面
3 * @param mapping
4 * @param form
5 * @param request
6 * @param response
7 * @return
8 * @throws Exception
9 */
10 public ActionForward editUI(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
11 throws Exception {
12 BrandForm brandForm = (BrandForm)form;
13 Brand brand = brandService.find(Brand.class, brandForm.getId());
14 brandForm.setName(brand.getName());
15 brandForm.setImagePath(brand.getImagePath());
16 return mapping.findForward("edit");
17 }
18 /**
19 * 提供修改功能
20 * @param mapping
21 * @param form
22 * @param request
23 * @param response
24 * @return
25 * @throws Exception
26 */
27 public ActionForward edit(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
28 throws Exception {
29 String message = "品牌修改成功";
30 BrandForm brandForm = (BrandForm)form;
31 Brand brand = brandService.find(Brand.class, brandForm.getId());
32 brand.setName(brandForm.getName());
33 if(brandForm.getImage()!=null&&brandForm.getImage().getFileSize()>0){
34 if(brandForm.isImageTypeValidate(brandForm.getImage())){
35 StringBuffer imageDir = new StringBuffer("/images/brand/");
36 SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd/HH/");
37 imageDir.append(dateFormat.format(new Date()));
38 String imageRealDir = request.getSession().getServletContext().getRealPath(imageDir.toString());
39 File fileDir = new File(imageRealDir);
40 if(!fileDir.exists()) fileDir.mkdirs();
41 StringBuffer imageName = new StringBuffer(UUID.randomUUID().toString());
42 String imageFormat = brandForm.getImage().getFileName().substring(brandForm.getImage().getFileName().lastIndexOf('.'));
43 imageName.append(imageFormat);
44 FileOutputStream outputStream = new FileOutputStream(new File(imageRealDir,imageName.toString()));
45 outputStream.write(brandForm.getImage().getFileData());
46 outputStream.close();
47 brand.setImagePath(imageDir.append(imageName).toString());
48 }
49 else{
50 message = "品牌修改失败";
51 }
52 }
53 brandService.update(brand);
54 request.setAttribute("jump", PropertiesTool.getValue("control.product.brand.list"));
55 request.setAttribute("message", message);
56 return mapping.findForward("message");
57 }
58
59 /**
60 * 提供查询界面
61 * @param mapping
62 * @param form
63 * @param request
64 * @param response
65 * @return
66 * @throws Exception
67 */
68 public ActionForward findUI(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
69 throws Exception {
70 return mapping.findForward("find");
71 }

 

posted @ 2012-04-08 21:34  xiao秋  阅读(509)  评论(0编辑  收藏  举报