spring mvc:基于MVC思想的应用框架的快速入门( 四 )

至此spring+mybatis就在web中整合完毕了 , 还剩下spring.mvc , 它不需要整合 , 只需要将springmvc.xml拿过来 , 然后再web.xml中配置好前端控制器就好了
springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml springmvc 然后在applicationContext中配置事务
一套完整的流程
浏览器访问 , 处理器进行处理 , 去servic层 , service层去dao层 , dao层查询数据库 , 获取数据之后 , 返回service , 然后返回控制层 , 最终到jsp页面 , 这就是基本的web开发逻辑
首先application.xml,springmvc.xml,sqlMapConfig.xml、web.xml的四个文件的配置
application.xml
springmvc.xml
配置这个扫描之后ssm包以及子包中的所有子类都会被spring实例化
sqlMapConfig.xml
web.xml
contextConfigLocation 【spring mvc:基于MVC思想的应用框架的快速入门】classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml springmvc *.action 控制层
package com.huanfeng.ssm.controller; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.HttpRequestHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.huanfeng.ssm.pojo.Items; import com.huanfeng.ssm.pojo.QueryVo; import com.huanfeng.ssm.service.ItemService; @Controller public class ItemController { @Autowired private ItemService itemService; @RequestMapping(valuehttp://news.hoteastday.com/a/= ''/item/itemlist.action'') public ModelAndView itemList(){ //从Mysql中查询 List list = itemService.selectItemsList(); ModelAndView mav = new ModelAndView(); //数据 mav.addObject(''itemList'', list); mav.setViewName(''itemList''); return mav; } }因为控制层需要service的对象 , 所以我们使用@Autowired的方式完成itemService的配置
Service层
package com.huanfeng.ssm.service; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.huanfeng.ssm.dao.ItemsMapper; import com.huanfeng.ssm.pojo.Items; @Service public class ItemServiceImpl implements ItemService { @Autowired private ItemsMapper itemsMapper; //查询商品列表 public List