A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 我是色色 于 2018-2-5 15:02 编辑

1 课程计划
1、高级参数绑定
a) 数组类型的参数绑定
b) List类型的绑定
2、@RequestMapping注解的使用
3、Controller方法返回值
4、Springmvc中异常处理
5、图片上传处理
6、Json数据交互
7、Springmvc实现Restful
8、拦截器
2 高级参数绑定2.1 绑定数组2.1.1 需求
在商品列表页面选中多个商品,然后删除。
2.1.2 需求分析
此功能要求商品列表页面中的每个商品前有一个checkbook,选中多个商品后点击删除按钮把商品id传递给Controller,根据商品id删除商品信息。
2.1.3 Jsp中实现:
[AppleScript] 纯文本查看 复制代码
<c:forEach items="${itemList }" var="item">
<tr>
<td><input name="ids" value="${item.id}" type="checkbox"></td>
<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 }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
生成html代码如下:
页面选中多个checkbox向controller方法传递
[AppleScript] 纯文本查看 复制代码
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<tr>
<td><input name="ids" value="1" type="checkbox"></td>
<td>台式机</td>
<td>3000.0</td>
<td>2016-02-03 13:22:53</td>
<td></td>
<td><a href="/springmvc-web/itemEdit.action?id=1">修改</a></td>
</tr>
<tr>
<td><input name="ids" value="2" type="checkbox"></td>
<td>笔记本</td>
<td>6000.0</td>
<td>2015-02-09 13:22:57</td>
<td></td>
<td><a href="/springmvc-web/itemEdit.action?id=2">修改</a></td>
</tr>
<tr>
<td><input name="ids" value="3" type="checkbox"></td>
<td>背包</td>
<td>200.0</td>
<td>2015-02-06 13:23:02</td>
<td></td>
<td><a href="/springmvc-web/itemEdit.action?id=3">修改</a></td>
</tr>
</table>
2.1.4 Controller
Controller方法中可以用String[]接收,或者pojo的String[]属性接收。两种方式任选其一即可。
定义如下:
[AppleScript] 纯文本查看 复制代码
@RequestMapping("/queryitem")
public String queryItem(QueryVo queryVo, String[] ids) {
System.out.println(queryVo.getItems().getName());
System.out.println(queryVo.getItems().getPrice());
System.out.println(ids.toString());
return null;
}
或者:
查看结果:
2.2 将表单的数据绑定到List2.2.1 需求
实现商品数据的批量修改。
2.2.2 需求分析
要想实现商品数据的批量修改,需要在商品列表中可以对商品信息进行修改,并且可以批量提交修改后的商品数据。
2.2.3 接收商品列表的pojo
List中存放对象,并将定义的List放在包装类中,使用包装pojo对象接收。
2.2.4 Jsp改造
页面定义如下:
[AppleScript] 纯文本查看 复制代码
<tr>
<td>
<input type="text" name=" itemsList[0].id" value="${item.id}"/>
</td>
<td>
<input type="text" name=" itemsList[0].name" value="${item.name }"/>
</td>
<td>
<input type="text" name=" itemsList[0].price" value="${item.price}"/>
</td>
</tr>
<tr>
<td>
<input type="text" name=" itemsList[1].id" value="${item.id}"/>
</td>
<td>
<input type="text" name=" itemsList[1].name" value="${item.name }"/>
</td>
<td>
<input type="text" name=" itemsList[1].price" value="${item.price}"/>
</td>
</tr>
 
Name属性必须是包装pojo的list属性+下标+元素属性。Jsp做如下改造:
[AppleScript] 纯文本查看 复制代码
<c:forEach items="${itemList }" var="item">
<tr>
<td><input name="ids" value="${item.id}" type="checkbox"></td>
<td>
<input name="id" value="${item.id}" type="hidden">
<input name="name" value="${item.name }" type="text">
</td>
<td><input name="name" value="${item.price }" type="text"></td>
<td><input name="name" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" type="text"></td>
<td><input name="name" value="${item.detail }" type="text"></td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
varStatus属性常用参数总结下:
${status.index}      输出行号,从0开始。
${status.count}      输出行号,从1开始。
${status.current}   当前这次迭代的(集合中的)项
${status.first}  判断当前项是否为集合中的第一项,返回值为true或false
${status.last}   判断当前项是否为集合中的最后一项,返回值为true或false
begin、end、step分别表示:起始序号,结束序号,跳跃步伐。
2.2.5 Contrller
[AppleScript] 纯文本查看 复制代码
@RequestMapping("/queryitem")
public String queryItem(QueryVo queryVo, String[] ids) {
System.out.println(queryVo.getItems().getName());
System.out.println(queryVo.getItems().getPrice());
System.out.println(ids.toString());
return null;
}
注意:接收List类型的数据必须是pojo的属性,方法的形参为List类型无法正确接收到数据。
3 @RequestMapping
通过RequestMapping注解可以定义不同的处理器映射规则。
3.1 URL路径映射
@RequestMapping(value="/item")或@RequestMapping("/item)
value的值是数组,可以将多个url映射到同一个方法
3.2 窄化请求映射
在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理。
如下:
[AppleScript] 纯文本查看 复制代码
@RequestMapping放在类名上边,设置请求前缀 
@Controller
@RequestMapping("/item")
 
方法名上边设置请求映射url:
[AppleScript] 纯文本查看 复制代码
@RequestMapping放在方法名上边,如下:
@RequestMapping("/queryItem ")
 
访问地址为:/item/queryItem
3.3 请求方法限定
[AppleScript] 纯文本查看 复制代码
u 限定GET方法
@RequestMapping(method = RequestMethod.GET)
 
如果通过Post访问则报错:
HTTP Status 405 - Request method 'POST' not supported
 
例如:
@RequestMapping(value="/editItem",method=RequestMethod.GET)
u 限定POST方法
 
@RequestMapping(method = RequestMethod.POST)
 
如果通过Post访问则报错:
HTTP Status 405 - Request method 'GET' not supported
 
u GET和POST都可以
@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})
 
4 controller方法返回值4.1 返回ModelAndView
controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。
4.2 返回void
        在controller方法形参上可以定义request和response,使用request或response指定响应结果:
1、使用request转向页面,如下:
[AppleScript] 纯文本查看 复制代码
request.getRequestDispatcher("页面路径").forward(request, response);
2、也可以通过response页面重定向:
[AppleScript] 纯文本查看 复制代码
response.sendRedirect("url")
 
3、也可以通过response指定响应结果,例如响应json数据如下:
[AppleScript] 纯文本查看 复制代码
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
 
4.3 返回字符串4.3.1 逻辑视图名
controller方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。
[AppleScript] 纯文本查看 复制代码
//指定逻辑视图名,经过视图解析器解析为jsp物理路径:/WEB-INF/jsp/item/editItem.jsp
return "item/editItem";
4.3.2 Redirect重定向
Contrller方法返回结果重定向到一个url地址,如下商品修改提交后重定向到商品查询方法,参数无法带到商品查询方法中。
[AppleScript] 纯文本查看 复制代码
//重定向到queryItem.action地址,request无法带过去
return "redirect:queryItem.action";
 
redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。
由于新发起一个request原来的参数在转发时就不能传递到下一个url,如果要传参数可以/item/queryItem.action后边加参数,如下:
/item/queryItem?...&…..
4.3.3 forward转发
controller方法执行后继续执行另一个controller方法,如下商品修改提交后转向到商品修改页面,修改商品的id参数可以带到商品修改方法中。
[AppleScript] 纯文本查看 复制代码
//结果转发到editItem.action,request可以带过去
return "forward:editItem.action";
forward方式相当于“request.getRequestDispatcher().forward(request,response)”,转发后浏览器地址栏还是原来的地址。转发并没有执行新的request和response,而是和转发前的请求共用一个request和response。所以转发前请求的参数在转发后仍然可以读取到。

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马