标签:
Handler的开发方式在springmvc中有多种,下面我们主要讲解三种实现方式:实现Controller接口、实现HttpRequestHandler接口、使用注解。
ItemsController1.java
/* * Handler开发方式1: 实现HttpRequestHandler接口 */ public class ItemsController1 implements HttpRequestHandler{ @Override public void handleRequest(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ //模拟查询商品列表数据 List<Items> list = new ArrayList<Items>(); list.add(new Items("笔记本",6000f,new Date(),"联想笔记本值得信赖!!!")); list.add(new Items("手机",3000f,new Date(),"支持国产手机!!!")); list.add(new Items("背包",1000f,new Date(),"登山旅行必备!!!")); //将商品放置到request域中 req.setAttribute("itemsList",list); //指定指定的视图 req.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(req,resp); } }
配置handler
在springmvc.xml配置文件中配置。
<!-- 配置实现HttpRequestHandler接口的处理器 --> <bean name="/queryItems.do" class="com.kiwi.controller.ItemsController1" />
jsp页面
<c:forEach items="${itemsList }" var="item"> <tr> <td>${item.name }</td> <td>${item.price }</td> <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td> <td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/editItems.do?id=${item.id}">修改</a></td> </tr> </c:forEach>
结果:
/* * Handler开发方式2: 实现Controller接口 */ public class ItemsController2 implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception{ ModelAndView mv = new ModelAndView(); // 模拟查询商品列表数据 List<Items> list = new ArrayList<Items>(); list.add(new Items("笔记本2",6000f,new Date(),"联想笔记本值得信赖!!!")); list.add(new Items("手机2",3000f,new Date(),"支持国产手机!!!")); list.add(new Items("背包2",1000f,new Date(),"登山旅行必备!!!")); mv.addObject("itemsList",list); mv.setViewName("/items/itemsList"); return mv; } }
配置handler
在springmvc.xml配置文件中配置。
<!-- 配置视图解析器: 如何把handler方法返回值解析作为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置实现Controller接口的处理器 --> <bean name="/queryItems2.do" class="com.kiwi.controller.ItemsController2" />
/* * Handler开发方式3: 使用注解开发 */ @Controller public class ItemsController3{ @RequestMapping("/queryItems3") public String showList(Model model){ // 模拟查询商品列表数据 List<Items> list = new ArrayList<Items>(); list.add(new Items("笔记本3",6003f,new Date(),"联想笔记本值得信赖!!!")); list.add(new Items("手机3",3003f,new Date(),"支持国产手机!!!")); list.add(new Items("背包3",1003f,new Date(),"登山旅行必备!!!")); model.addAttribute("itemsList",list); return "/items/itemsList"; } }
配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.kiwi" /> <!-- 配置视图解析器: 如何把handler方法返回值解析作为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp" /> <property name="suffix" value=".jsp" /> </bean> </beans>
标签:
原文地址:http://www.cnblogs.com/yangang2013/p/5801079.html