1)默认支持的类型:Request,Response,Session,Model 2)基本数据类型(包含String) 3)Pojo类型 4)Vo类型 5)Converter自定义转换器 6)数组 7)ListitemList.jsp varStatus属性常用参数总结下:
${status.index} 输出行号,从0开始。
${status.count} 输出行号,从1开始。
${status.current} 当前这次迭代的(集合中的)项
${status.first} 判断当前项是否为集合中的第一项,返回值为true或false
${status.last} 判断当前项是否为集合中的最后一项,返回值为true或false
begin、end、step分别表示:起始序号,结束序号,跳跃步伐。
public class QueryVo {
//商品对象
private Items items;
//订单对象...
//用户对象....
//批量删除使用
private Integer[] ids;
//批量修改使用
private List itemsList;
ItemsController.java QueryVo.getIds();
@RequestMapping("/updateAll")
//public String updateAll(String[] ids) throws Exception{
public String updateAll(QueryVo vo) throws Exception{
System.out.println(vo.getIds());
return "";
}
@RequestMapping("/updateAll")
public String updateAll(QueryVo vo) throws Exception{
System.out.println(vo.getItemsList());
return "";
}
窄化请求映射
@Controller
//窄化请求映射:为防止你和你的队友在conroller方法起名的时候重名,所以相当于在url中多加了一层目录,防止重名
//例如:当前list的访问路径 localhost:8081/ssm0523-1/items/list.action
@RequestMapping("/items")
public class ItemsController {
@Autowired
private ItemsService itmesService;
请求方法限定
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.POST)☆2.controller方法返回值(指定返回到哪个页面, 指定返回到页面的数据) 1)ModelAndView
modelAndView.addObject("itemList", list); 指定返回页面的数据
modelAndView.setViewName("itemList"); 指定返回的页面
2)String(推荐使用)
返回普通字符串,就是页面去掉扩展名的名称, 返回给页面数据通过Model来完成
//指定逻辑视图名,经过视图解析器解析为jsp物理路径:/WEB-INF/jsp/item/editItem.jsp return "item/editItem";返回的字符串以forward:开头为请求转发
//结果转发到editItem.action,request可以带过去 //request.getRequestDispatcher().forward(request,response) return "forward:editItem.action";返回的字符串以redirect:开头为重定向
//重定向到queryItem.action地址,request无法带过去 //response.sendRedirect() return "redirect:queryItem.action";3)返回void(使用它破坏了springMvc的结构,所以不建议使用)
可以使用request.setAttribut 来给页面返回数据 可以使用request.getRquestDispatcher().forward()来指定返回的页面 如果controller返回值为void则不走springMvc的组件,所以要写页面的完整路径名称相对路径:相对于当前目录,也就是在当前类的目录下,这时候可以使用相对路径跳转 绝对路径:从项目名后开始.
在springMvc中不管是forward还是redirect后面凡是以/开头的为绝对路径,不以/开头的为相对路径 例如:forward:/items/itemEdit.action 为绝对路径 forward:itemEdit.action为相对路径3.架构级别异常处理:
主要为了防止项目上线后给用户抛500等异常信息,所以需要在架构级别上整体处理.hold住异常 首先自定义全局异常处理器实现HandlerExceptionResolver接口 在spirngMvc.xml中配置生效
系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。
系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:
//自定义异常类,用来处理自定义异常
public class CustomException extends Exception{
//保存异常信息
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
自定义异常处理器 CustomGlobalExceptionResolver.java
//自定义全局异常处理
public class CustomGlobalExceptionResolver implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object arg2, Exception exc) {
//保存异常信息
String msg = "";
//判断异常类型
if(exc instanceof CustomException){
//处理业务级别异常
msg = ((CustomException)exc).getMessage();
} else {
//处理运行时异常
msg = "系统异常, 亲,对不起, 请及时联系管理员哦!";
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", msg);
modelAndView.setViewName("error");
return modelAndView;
}
}
错误页面 error.jsp
异常处理器配置SpringMvc.xml查询商品列表 ${msg}
异常测试
@RequestMapping("/list")
public ModelAndView itemsList() throws Exception{
//测试运行时异常
//int i= 0/0;
//测试自定义异常
// if(true){
// CustomException customException = new CustomException();
// customException.setMessage("对不起哦, 您已经抢购过, 不要太贪心哦!");
// throw customException;
// }
List list = itmesService.list();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", list);
modelAndView.setViewName("itemList");
return modelAndView;
}
4.上传图片:
1)在tomcat中配置虚拟图片服务器.
在tomcat上配置图片虚拟目录,在tomcat下conf/server.xml中添加:2)导入fileupload的jar包访问http://localhost:8080/pic即可访问F:developuploadtemp下的图片。 也可以通过eclipse配置:
CommonsMultipartResolver
4)在页面上编写上传域,更改form标签的类型5242880


