public class CustomDateConverter implements Converter<String,Date>{
@override
public Date convert(String source){
try{
SimpleDateFormat simpleDateFormat
= new SimpleDateFormat("yyy-MM-dd HH-mm-ss");
return simpleDateFormat.parse(source);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>
<!-- conversionService -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<list>
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>
@Component
public class JsonUtil {
public static final ObjectMapper mapper;
static {
SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.YYYY_MM_DD_HH_MM_SS);
mapper =new ObjectMapper();
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
TimeZone.setDefault(tz);
mapper.setTimeZone(tz);
mapper.setDateFormat(dateFormat);
}
public static String toJson(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException("转换json字符失败!");
}
}
public <T extends Object> T toObject(String json, Class<T> clazz) {
try {
return mapper.readValue(json, clazz);
} catch (IOException e) {
throw new RuntimeException("将json字符转换为对象时失败!");
}catch (Exception e) {
throw new RuntimeException("转换json字符失败!");
<!-- 使用缓存mvc注解配置 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
页面定义如下:
页面选中多个checkbox向controller方法传递
<input type="checkbox" name="item_id" value="001"/>
<input type="checkbox" name="item_id" value="002"/>
<input type="checkbox" name="item_id" value="002"/>
传递到controller方法中的格式是:001,002,003
Controller方法中可以用String[]接收,定义如下:
public String deleteitem(String[] item_id)throws Exception{
System.out.println(item_id);
}
}
List中存放对象,并将定义的List放在包装类中,action使用包装对象接收。
List中对象:
成绩对象
Public class QueryVo {
Private List<Items> itemList;//订单明细
//get/set方法..
}
包装类中定义List对象,并添加get/set方法如下:
页面:
<tr>
<td>
<input type="text" name=" itemList[0].id" value="${item.id}"/>
</td>
<td>
<input type="text" name=" itemList[0].name" value="${item.name }"/>
</td>
<td>
<input type="text" name=" itemList[0].price" value="${item.price}"/>
</td>
</tr>
<tr>
<td>
<input type="text" name=" itemList[1].id" value="${item.id}"/>
</td>
<td>
<input type="text" name=" itemList[1].name" value="${item.name }"/>
</td>
<td>
<input type="text" name=" itemList[1].price" value="${item.price}"/>
</td>
</tr>
Contrller方法定义如下:
public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getItemList());
}
在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。
包装类中定义Map对象如下:
Public class QueryVo {
private Map<String, Object> itemInfo = new HashMap<String, Object>();
//get/set方法..
}
页面定义如下:
<tr>
<td>学生信息:</td>
<td>
姓名:<inputtype="text"name="itemInfo['name']"/>
年龄:<inputtype="text"name="itemInfo['price']"/>
.. .. ..
</td>
</tr>
Contrller方法定义如下:
public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getStudentinfo());
}
<bean
id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>800000000</value>
</property>
</bean>
}