springmvc4开发学习(第四讲)

数据的回显:

提交后,如果出现错误,数据回到刚才提交的页面中。

pojo数据回显方法

1.springmvc默认对pojo数据进行回显。

   pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)

   使用@ModelAttribute指定pojo回显到页面在request中的key

2.@ModelAttribute还可以将方法的返回值传到页面

   在商品查询列表页面,通过商品类型查询商品信息。

   在controller中定义商品类型查询方法,最终将商品类型传到页面。


在页面中,能够有数据回显作用是因为有相同的ItemsCustom


如果其中数据不相同,


则响应的编码:



@ModelAttribute进行数据的回显

在controller中定义:

@ModelAttribute("itemtypes")
public Map<String,String> getItemType(){
Map<String,String> itemTypes = new HashMap<String,String>();
itemTypes.put("101", "数码");
itemTypes.put("102", "母婴");
return itemTypes;
}

在页面中定义:

商品类型:
<select name="itemtype">
<c:forEach items="${itemtypes }" var="itemtypett">
<option value="${itemtypett.key }">${itemtypett.value }</option>
</c:forEach>
</select>


最简单的方法是定义一个model,不用使用@ModelAttribute


对于简单类型的数据回显,只能使用model。

model.addAttribute("id",id);

异常处理

系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。

系统的daoservicecontroller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:


全局异常处理器

思路:

系统遇到异常,在程序中手动抛出,dao抛给serviceservicecontrollercontroller抛给前端控制器,前端控制器调用全局异常处理器。

全局异常处理器处理思路:

解析出异常类型

如果该 异常类型是系统自定义的异常,直接取出异常信息,在错误页面展示

如果该 异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)


1.创建一个异常类(继承Exception)

public class CustomException extends Exception{
public String message;
public CustomException(String message) {
super(message);
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

2.创建一个全局异常类(继承HandlerExceptionResolver)

@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,Exception ex) {

/* String message=null;
if(ex instanceof CustomException){
message = ((CustomException)ex).getMessage();
}
else {
message="未知错误";
}*/
CustomException customException = null;
if(ex instanceof CustomException){
customException = (CustomException)ex;

}
else {
customException = new CustomException("未知错误");
}
String message = customException.getMessage();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", message);
modelAndView.setViewName("error");
return modelAndView;
}

3.在springmvc中添加

<!-- 全局异常处理器 -->
<bean class="cn.itcast.ssm.exception.CustomExceptionResolver"></bean>

上传图片

springmvc中对多部件类型解析

springmvc中对多部件类型解析

页面form中提交enctype="multipart/form-data"的数据时,需要springmvcmultipart类型的数据进行解析。

 

springmvc.xml中配置multipart类型解析器。

<!-- 文件上传的解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>

加入上传图片的jar


在controller中:

public String editItemsSubmit(Model model,Integer id,
@ModelAttribute("items") @Validated(value={ValidatorGroup1.class,ValidatorGroup2.class})ItemsCustom itemsCustom,BindingResult bindingResult, 
MultipartFile items_pic) throws Exception{

if(items_pic!=null){
//存储图片的物理路径
String pic_path = "E:\\java\\pic\\";
//原始名称
String originalFilename = items_pic.getOriginalFilename();
//新图片的名称
String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
//新图片
File newFile = new File(pic_path+newFileName);
//将内存中的数据写入磁盘中
items_pic.transferTo(newFile);
//将新图片名称写到itemsCustom中
itemsCustom.setPic(newFileName);
}

代码页面:

<tr>
<td>商品图片</td>
<td>
<c:if test="${items.pic !=null}">
<img src="/pic/${items.pic}" width=100 height=100/>
<br/>
</c:if>
<input type="file"  name="
items_pic"/> 
</td>
</tr>

其中标红的参数必须一致。即controller的形参和input的name。














































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值