使用形参传递参数,用的是多线程实现。不能使用成员变量接受参数,因为当controller是单例模式的时候,无论多少个请求都是公用一个类,所以不能用成员变量接受参数。
1、默认支持的参数类型
(1)HttpServletRequest
(2)HttpServletResponse
(3)HttpSession
@RequestMapping(value="itemEdit.action")
public ModelAndView selectItemsById(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
String id = request.getParameter("id");
Items item = ItemService.selectItemsById(Integer.parseInt(id));
ModelAndView mav = new ModelAndView();
mav.setViewName("editItem");
mav.addObject("item",item);
return mav;
}
(4)Model/ModelMap
Model和ModelMap可以代替ModelAndView,使用Model/ModelMap后方法返回值可以使用String,表示View,Model/ModelMap可以传递数据。
Model是接口,ModelMap是Model的实现类。使用Model,SpringMVC会实例化ModelMap。
Model、ModelMap、ModelAndView本质都是request传递参数到jsp。
1)Model使用:
@RequestMapping(value="itemEdit.action")
public String selectItemsById(Integer id, Model model) {
Items item = ItemService.selectItemsById(id);
model.addAttribute("item",item);
return "editItem";
}
2)ModelMap使用:
@RequestMapping(value="itemEdit.action")
public String selectItemsById(Integer id, ModelMap modelMap) {
Items item = ItemService.selectItemsById(id);
modelMap.addAttribute("item",item);
return "editItem";
}
2、绑定简单类型
当处理器形参和请求中的参数名相同时,SpringMVC会自动把请求参数和形参绑定
(1)支持的数据类型
整形:Integer、int
字符串:String
单精度:Float、float
双精度:Double、double
布尔型:Boolean、boolean
(2)@RequestParam
当请求参数名和形参名不一样的时候,可以使用@RequestParam进行绑定
value:请求参数名
required:参数是否必须有,默认true,没有就会报错:
TTP Status 400 - Required Integer parameter 'XXXX' is not present
defaultValue:当required为false时,defaultValue表示默认值
@RequestMapping(value="itemEdit.action")
public String selectItemsById(@RequestParam(value="id",required=false, defaultValue=1) Integer ThisId, ModelMap modelMap) {
Items item = ItemService.selectItemsById(ThisId);
modelMap.addAttribute("item",item);
return "editItem";
}
3、绑定pojo类型
当请求参数和pojo属性名一致时,SpringMVC会自动把请求参数和pojo属性进行绑定。
4、绑定包装pojo
包装类:
package club.ityuchao.pojo;
public class QueryVo {
private Items item;
public Items getItem() {
return item;
}
public void setItem(Items item) {
this.item = item;
}
}
页面:name=item.属性
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post">
<input type="hidden" name="item.id" value="${item.id }" /> 修改商品信息:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td><input type="text" name="item.name" value="${item.name }" /></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="item.price" value="${item.price }" /></td>
</tr>
<tr>
<td>商品简介</td>
<td><textarea rows="3" cols="30" name="item.detail">${item.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
5、自定义参数绑定
由于日期格式有很多中,SpringMVC没有帮我们自动转换,所以SpringMVC提供了转换器供我们手动转换
Springmvc.xml文件配置:
<!-- 注解驱动:处理器映射器+处理器适配器 -->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
<!-- 配置converter -->
<bean name="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="club.ityuchao.converter.DateConverter"/>
</list>
</property>
</bean>
DataConversion:
package club.ityuchao.converter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
/**
*
* @date 2019年7月10日 下午9:48:23
* @Description 字符串转换为日期
* String:转换前的类型
* Date:转换后的类型
*
*/
public class DateConverter implements Converter<String, Date> {
public Date convert(String source) {
//如果抛异常,则返回null
try {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(source);
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
}
方式二(了解):该方式是单独配置转换适配器时用(未用<mvc:annotation-driven/>)
<!--注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder"></property>
</bean>
<!-- 自定义webBinder -->
<bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
</bean>
<!-- 转换器配置 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="cn.itcast.springmvc.convert.DateConverter" />
</set>
</property>
</bean>