public class CustomDateConverter implements Converter<String,Date>{java
@overrideweb
public Date convert(String source){spring
try{json
SimpleDateFormat simpleDateFormat 數組
= new SimpleDateFormat("yyy-MM-dd HH-mm-ss");緩存
return simpleDateFormat.parse(source);mvc
}catch(Exception e){app
e.printStackTrace();ide
}spa
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中對象:
成績對象
Private List<Items> itemList;//訂單明細
//get/set方法..
}
包裝類中定義List對象,並添加get/set方法以下:
頁面:
<tr>
<td>
<input type="text" name=" itemList[0].id" value="${item.id}"/>
</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>
}