1. 默认支持类型HttpServletRequest、HttpServletResponse、HttpSession、Model、ModelMap
测试路径:%项目根%/getAllEmp.action?empno=7788
1 @RequestMapping(value="/getAllEmp") 2 public String getAllEmp(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model,ModelMap modelMap) 3 { 4 System.out.println("request:" + request.getParameter("empno")); 5 6 System.out.println("response:" + response.getLocale().getCountry() +"-" + response.getLocale().getLanguage()); 7 8 System.out.println("session:" + session.getId()); 9 10 List<Emp> emps = empService.getEmp(); 11 12 model.addAttribute("emps", emps); 13 14 modelMap.addAttribute("message", "查询雇员信息成功"); 15 16 return "empQuery";//实际解析名称: 前缀"/" + main + 后缀".jsp" /main.jsp 17 }
2. 简单类型:整型,字符串,单精度/双精度,boolean类型[注意:这种方式只能用于get请求方式,如果为post请求,必须用request获取到参数]
注意:1. 默认情况下,实际参数名称与形式参数名称一致
2. 基本数据类型,建议使用其包装类型Integer、Boolean、Double、Float等
测试路径:%项目根%/getEmpByEmpno.action?index=${status.index+1}
1 @RequestMapping("/getEmpByEmpno") 2 public String getEmpByEmpno(Model model,Integer index) 3 { 4 System.out.println("getEmpByEmpno:index参数值---" + index); 5 6 Emp emp = empService.getEmpByEmpno(index); 7 8 model.addAttribute("emp", emp); 9 10 return "empUpdate"; 11 }
2.1 简单类型:整型,字符串,单精度/双精度,boolean类型
注意: 1. 默认情况下,实际参数名称与形式参数名称一致
2. 基本数据类型,建议使用其包装类型Integer、Boolean、Double、Float等
3. 当实际参数名称与形式参数名称不一致时,使用注解@RequestParam标明实际参数名称
value:指定的实际参数名称
required:true 参数必须带
defaultValue:参数默认值
测试路径:
%项目根%/getEmpByEmpno_2.action?index=${status.index+1}
%项目根%/getEmpByEmpno_2.action
1 @RequestMapping("/getEmpByEmpno_2") 2 public String getEmpByEmpno_2(Model model,@RequestParam(value="index",required=true,defaultValue="0")Integer index2) 3 { 4 System.out.println("getEmpByEmpno:index2参数值---" + index2); 5 6 Emp emp = empService.getEmpByEmpno(index2); 7 8 model.addAttribute("emp", emp); 9 10 return "empUpdate"; 11 }
3.简单的POJO(plain old java projects)对象
注意:提交的参数名称必须与javaBean对象的属性名称一致
例如:<input type="text" name="empno" value="${emp.empno}">中的name属性值必须与参数emp对象中的empno属性名一致
4.包装的POJO对象: dept.dname dept.deptno
5.自定义参数绑定
(1) 定义类,并实现 org.springframework.core.convert.converter.Converter接口,同时重写convert()方法
1 public class CustomerDateConverter implements Converter<String, Date> { 2 3 @Override 4 public Date convert(String param) { 5 6 //将字符串类型转换为日期类型 7 8 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 9 10 try { 11 12 return sdf.parse(param); 13 14 } catch (ParseException e) { 15 e.printStackTrace(); 16 } 17 18 return null; 19 }
注意:泛型中,第一个为传入参数类型,第二个为传出的指定格式的参数类型
(2) 配置自定义参数转换器
<!-- 自定参数绑定组件 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 配置转换器 -->
<property name="converters">
<list>
<!-- 日期类型转换器 -->
<bean class="com.neuedu.converter.CustomerDateConverter"></bean>
</list>
</property>
</bean>
(3)将自定义参数转换器注入到适配器
<!-- 处理器适配器、处理器映射器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
6. 集合类 List
注意:形式参数类型为引用类型Emp
实际参数类型(List)传入时,name属性值subEmps[${status.index}].empno必须与Emp对象中的属性值subEmps一致
<input type="text" name="subEmps[${status.index}].empno" value="${emp.empno}">
1 @RequestMapping("/getSubEmps") 2 public String getSubEmps(Model model,Integer index) 3 { 4 Emp emp = empService.getEmpByEmpno(index); 5 6 List<Emp> subEmpList = emp.getSubEmps(); 7 8 model.addAttribute("subEmpList", subEmpList); 9 10 return "subEmpQuery"; 11 } 12 @RequestMapping("/updateSubEmp") 13 public String updateSubEmp(Emp emp) 14 { 15 System.out.println("updateSubEmp:"+emp.getSubEmps()); 16 17 return "success"; 18 19 }
6. 集合类 数组
1 @RequestMapping("/deleteEmps") 2 public String deleteEmps(Integer[] indexs) 3 { 4 //delete from emp where empno in(1, 2, 3, 5); 5 System.out.println("deleteEmps:" +Arrays.toString(indexs));//[1, 2, 3, 5] 6 7 return "success"; 8 }
1 <c:forEach items="${emps}" var="emp" varStatus="status"> 2 <tr> 3 <td><input type="checkbox" name="indexs" value="${status.index+1}">${status.index+1}</td> 4 <td>${emp.empno}</td> 5 <td>${emp.ename}</td> 6 <td>${emp.job}</td> 7 <td><fmt:formatDate value="${emp.hiredate}" pattern="yyyy-MM-dd"/></td> 8 <td>${emp.salary}</td> 9 <td>${emp.comm}</td> 10 <td>${emp.dept.dname}</td> 11 <td> 12 <a href="getEmpByEmpno.action?index=${status.index+1}">修改</a> 13 <a href="getEmpByEmpno_2.action?index=${status.index+1}">修改_2</a> 14 <a href="#?index=${status.index+1}">删除</a> 15 <a href="getSubEmps.action?index=${status.index+1}">查看下属</a> 16 </td> 17 </tr> 18 </c:forEach>