高级参数绑定
1、数组类型的参数绑定
基于批量删除操作完成参数传递
@RequestMapping("queryItem")
public String queryItem(QueryVo vo, Integer[] ids) {
System.out.println(vo);
//输出传入的数组
if (ids != null && ids.length > 0) {
for (Integer id : ids) {
System.out.println("传入的商品列表分别为:" + id);
}
}
return "itemList";
2、List类型的绑定
基于批量修改操作完成参数传递(控制器修改)
@RequestMapping("queryItem")
public String queryItem(QueryVo vo,Integer[] ids){
System.out.println(vo);
//输出传入的数组
if(ids != null && ids.length > 0){
for(Integer id : ids){
System.out.println("传入的参数为"+id);
}
}
}
(POJO修改)
public class QueryVo{
private Item item;
//演示List参数传递
private List<Item> items;
public List<Item> getItems(){
return items
}
}
编写一个DateConvert类
public class DateConvert implements Converter<String,Date>{
@Override
public Date convert(String source){
Date result = null;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
result = sdf.parse(source);
}catch(ParseException e){
e.printStackTrace();
}
return result;
}
}
在springmvc.xml配置日期转换器
<!-- 使用自定义转换器 -->
<mvc:annotation-driven conversion-service="MyConvert"
<!-- 定义转换器 -->
<bean id="MyConvert" class="org.springframework.format.support.FormattingConversionServiceFactory" >
<property name="converters">
<set>
<bean class="com.java.springmvc.util.DateConvert"/>
</set>
</property>
</bean>
@RequestMapping注解的使用
分目录管理(随便找个例子)
@RequestMapping("Item")
public class Controller{
@RequestMapping("queryItem")
public String queryItem(QueryVo vo,Integer[] ids){
System.out.println(vo);
//输出传入的数组
if(ids != null && ids.length > 0){
for(Integer id : ids){
System.out.println("传入的参数为"+id);
}
}
}
}
这个时候在浏览器输入地址就不是localhost:8080/queryItem了,还要在queryItem前面加个Item,如localhost:8080/Item/queryItem
限定请求方法
@RequestMapping(value="queryItem",method=(RequestMethod.POST,method=(RequestMethod.GET))
Controller方法返回值
1、返回 ModelAndView
@RequestMapping("itemEdit")
public ModelAndView itemEdit(@RequestParam(value="id",required=true,defaultValue="1")Integer ids){
ModelAndView mav = new ModelAndView();
//查询商品信息
Item item = itemServices.getItemById(ids);
//设置商品数据返回页面
mav.addObject("item", item);
//设置视图名称
mav.setViewName("itemEdit");
return mav;
}
2、返回void( request 与 response )
request
@RequestMapping("queryVoid")
public void queryVoid(HttpServletRequest request,HttpServletResponse response) throws Exception{
//request响应客户请求
request.setAttribute("msg","这个是request响应的消息");
request.getRequestDispatcher("/WEB-INF/jsp/demo.jsp").forward(request,response);
}
response
@RequestMapping("queryVoid")
public void queryVoid(HttpServletRequest request,HttpServletResponse response) throws Exception{
//response响应用户请求
response.sendRedirect("/hello.action");
//设施响应的字符编码
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter printWriter = response.getWriter();
printWriter.println("这是response的打印消息");
}
3、返回String( 返回视图名字,参考第一天内容 )
redirect与forward
@RequestMapping("demo")
public String name(String msg,Model model) throws Exception{
model.addAttribute("msg", msg);
return "redirect:hello.action";
//return "forword:hello.action";//相当于request的请求转发和response的请求重定向
}