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

SpringMVC实例分析

时间:2015-01-16 06:30:52      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

Spring的MVC模块

Spring提供了自己的MVC框架实现,相比Struts、WebWork等MVC模块,Spring的MVC模块显得小巧而灵活。Spring的MVC使用Controller处理用户请求,此处的Controller类似于Struts1.x中的Action。SpringMVC作为Spring框架的一部分,在进行框架整合时不需要像Struts1&2那样特意的去融合到Spring里面,其本身就在Spring里面。

先定义如下各层:

域模型层实体类Cat:

 1 package com.spring.mvc;
 2 
 3 import java.util.Date;
 4 
 5 import javax.persistence.Entity;
 6 import javax.persistence.GeneratedValue;
 7 import javax.persistence.GenerationType;
 8 import javax.persistence.Id;
 9 import javax.persistence.Table;
10 import javax.persistence.Temporal;
11 import javax.persistence.TemporalType;
12 
13 @Entity
14 @Table(name = "tb_cat")
15 public class Cat {
16 
17     @Id
18     @GeneratedValue(strategy = GenerationType.AUTO)
19     private Integer id;
20     private String name;
21     @Temporal(value = TemporalType.TIMESTAMP)
22     private Date createdDate;
23     public Date getCreatedDate() {
24         return createdDate;
25     }
26 
27     public void setCreatedDate(Date createdDate) {
28         this.createdDate = createdDate;
29     }
30 
31     public Integer getId() {
32         return id;
33     }
34 
35     public void setId(Integer id) {
36         this.id = id;
37     }
38 
39     public String getName() {
40         return name;
41     }
42 
43     public void setName(String name) {
44         this.name = name;
45     }
46 }


业务逻辑层接口ICatService:

1 package com.spring.mvc;
2 
3 import java.util.List;
4 
5 public interface ICatService {
6     public void createCat(Cat cat);
7     public List<Cat> listCats();
8     public int getCatsCount();
9 }

业务逻辑层接口的实现类CatServiceImpl:

 1 package com.spring.mvc;
 2 
 3 import java.util.List;
 4 
 5 public class CatServiceImpl implements ICatService {
 6     private ICatDao catDao;
 7     public ICatDao getCatDao() {
 8         return catDao;
 9     }
10 
11     public void setCatDao(ICatDao catDao) {
12         this.catDao = catDao;
13     }
14 
15     public void createCat(Cat cat) {
16         if (catDao.findCatByName(cat.getName()) != null){
17             throw new RuntimeException("猫" + cat.getName() + "已经存在。" );
18         }
19         catDao.createCat(cat);
20     }
21 
22     public int getCatsCount() {
23         return catDao.getCatsCount();
24     }
25 
26     public List<Cat> listCats() {
27         return catDao.listCats();
28     }
29 }

数据库持久层接口ICatDao:

 1 package com.spring.mvc;
 2 
 3 import java.util.List;
 4 
 5 public interface ICatDao {
 6     public void createCat(Cat cat);
 7     public Cat findCatByName(String name);
 8     public List<Cat> listCats();
 9     public int getCatsCount();
10 }

数据库持久层实现类CatDaoImpl:

 1 package com.spring.mvc;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 6 
 7 public class CatDaoImpl extends HibernateDaoSupport implements ICatDao {
 8 
 9     public void createCat(Cat cat) {
10         this.getHibernateTemplate().persist(cat);
11     }
12 
13     public Cat findCatByName(String name) {
14         List<Cat> catList = this.getHibernateTemplate().find(" select c from Cat c where c.name = ? ", name);
15         if (catList.size() > 0)
16             return catList.get(0);
17         return null;
18     }
19 
20     public int getCatsCount() {
21         return (Integer) this.getSession(true).createQuery(" select count(c) from Cat c ").uniqueResult();
22     }
23 
24     public List<Cat> listCats() {
25         return this.getHibernateTemplate().find(" select c from Cat c ");
26     }
27 }

SpringMVC的控制层和视图层

SpringMVC的控制层是Controller。Controller是个接口,一般直接继承AbstractController抽象类,并实现handleRequestInternal方法,此方法类似于Struts1.x中的execute()方法。

SpringMVC的视图层使用的是ModelAndView对象。handleRequestInternal方法返回的即时此对象,ModelAndView相当于Struts1.x中的ActionForward
ModelAndView可以方便的传递参数,例如
return new ModelAndView("cal/listCat","cat",cat);
等价于
request.setAttribute("cat",cat);
return new ModelAndView("cal/listCat");

当传递多个参数时,可以使用Map,例如:
Map map = new HashMap();
map.put("cat",cat);
map.put("catList",catList);
return new ModelAndView("cat/listCat",map);

 1 package com.spring.mvc;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 import org.springframework.web.servlet.ModelAndView;
10 import org.springframework.web.servlet.mvc.AbstractController;
11 
12 public class CatController extends AbstractController {
13 
14     private ICatService catService;
15 
16     @Override
17     protected ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response) throws Exception {
18         String action = request.getParameter("action");
19         if ("add".equals(action)) {
20             return this.add(request, response);
21         }
22         return this.list(request, response);
23     }
24 
25     protected ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception {
26         List<Cat> catList = catService.listCats();
27         request.setAttribute("catList", catList);
28         return new ModelAndView("cat/listCat");
29     }
30 
31     protected ModelAndView add(HttpServletRequest request,HttpServletResponse response) throws Exception {
32         Cat cat = new Cat();
33         cat.setName(request.getParameter("name"));
34         cat.setCreatedDate(new Date());
35         catService.createCat(cat);
36         return new ModelAndView("cat/listCat", "cat", cat);
37     }
38 
39     public ICatService getCatService() {
40         return catService;
41     }
42 
43     public void setCatService(ICatService catService) {
44         this.catService = catService;
45     }
46 }

 多业务分发器

如果一个Controller需要处理多种业务逻辑,可以使用MultiActionController。MultiActionController就是一个分发器,相当于Struts1.x中的DispatchAction分发器,能根据某参数值将不同的请求分发到不同的方法上,比如可以设置分发器参数为method,则URL地址访问catMulti.mvc?method=add时将会执行add方法。CatMultiController不需要继承父类的任何方法,只需要定义形如public ModelAndView xxx(HttpServletRequest request,HttpServletResponse response)的方法,当地址栏参数method为xxx时,Spring会通过反射调用xxx()方法。

 1 package com.spring.mvc;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 import org.springframework.web.servlet.ModelAndView;
10 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
11 
12 public class CatMultiController extends MultiActionController {
13 
14     private ICatService catService;
15 
16     public ICatService getCatService() {
17         return catService;
18     }
19 
20     public void setCatService(ICatService catService) {
21         this.catService = catService;
22     }
23 
24     @SuppressWarnings("unchecked")
25     public ModelAndView add(HttpServletRequest request,HttpServletResponse response) {
26         Cat cat = new Cat();
27         cat.setName(request.getParameter("name"));
28         cat.setCreatedDate(new Date());
29         catService.createCat(cat);
30         return this.list(request, response);
31     }
32 
33     @SuppressWarnings("unchecked")
34     public ModelAndView list(HttpServletRequest request,HttpServletResponse response) {
35         List<Cat> catList = catService.listCats();
36         request.setAttribute("catList", catList);
37         return new ModelAndView("cat/listCat");
38     }
39 
40 }

 

SpringMVC实例分析

标签:

原文地址:http://www.cnblogs.com/cnjavahome/p/4227567.html

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