作者:大叔丶你好哇塞 | 来源:互联网 | 2023-09-02 11:41
商品订单数据模型
![](https://img.php1.cn/3cd4a/1eebe/cd5/3e641eb5f5c8cd90.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIxNDY3MTEz,size_16,color_FFFFFF,t_70)
商品列表查询
1.环境准备:需要spring3.2所有jar(一定包括spring-webmvc-3.2.0.RELEASE.jar)
![](https://img.php1.cn/3cd4a/1eebe/cd5/bff2716168d1ed7b.webp)
2.在web.xml中配置前端控制器
springmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:springmvc.xmlspringmvc*.action
3.在classpath下的springmvc.xml中配置处理器适配器
此适配器能执行实现 Controller接口的Handler。
4.开发Handler
publicclass ItemsController1 implements Controller {@Overridepublic ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {//调用service查找 数据库,查询商品列表,这里使用静态数据模拟List itemsList = new ArrayList();//向list中填充静态数据Items items_1 = new Items();items_1.setName("联想笔记本");items_1.setPrice(6000f);items_1.setDetail("ThinkPad T430 联想笔记本电脑!");Items items_2 = new Items();items_2.setName("苹果手机");items_2.setPrice(5000f);items_2.setDetail("iphone6苹果手机!");itemsList.add(items_1);itemsList.add(items_2);//返回ModelAndViewModelAndView modelAndView = new ModelAndView();//相当 于request的setAttribut,在jsp页面中通过itemsList取数据modelAndView.addObject("itemsList", itemsList);//指定视图modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");return modelAndView;}}
需要实现 controller接口,才能由org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter适配器执行。
5.视图编写
<%&#64; page language&#61;"java" contentType&#61;"text/html; charset&#61;UTF-8"pageEncoding&#61;"UTF-8"%>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/core" prefix&#61;"c" %>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/fmt" prefix&#61;"fmt"%>
查询条件&#xff1a;
商品列表&#xff1a;
商品名称 | 商品价格 | 生产日期 | 商品描述 | 操作 |
${item.name } | ${item.price } | | ${item.detail } | 修改 |
6.将编写Handler在spring容器加载。
7.在classpath下的springmvc.xml中配置处理器映射器
8.配置解析jsp的视图解析器。
9.部署调试
访问地址&#xff1a;http://localhost:8080/springmvcfirst/queryItems_test.action