spring mvc 前台到后台 时间类型是怎样处理的
spring mvc 本身并不提供日期类型的解析器,需要手工绑定, 否则会出现非法参数异常.
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.Date]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException
解决方法很简单,只需要在controller 里面注册一个类型解析器:
@InitBinder
public void InitBinder(HttpServletRequest request,
ServletRequestDataBinder binder) {
// 不要删除下行注释!!! 将来"yyyy-MM-dd"将配置到properties文件中
// SimpleDateFormat dateFormat = new
// SimpleDateFormat(getText("date.format", request.getLocale()));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(
dateFormat, true));
}
这样就ok了,
demo 如下:
controller
@RequestMapping("bakComment")
@Controller
public class BakCommentController {
Logger logger = Logger.getLogger(getClass());
@Resource
private CommentService commentService;
//@InitBinder
public void InitBinder(HttpServletRequest request,
ServletRequestDataBinder binder) {
// 不要删除下行注释!!! 将来"yyyy-MM-dd"将配置到properties文件中
// SimpleDateFormat dateFormat = new
// SimpleDateFormat(getText("date.format", request.getLocale()));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(
dateFormat, true));
}
@RequestMapping("verifyComment")
public void verifyComment(@ModelAttribute("comment") TSdComment comment,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
logger.info(comment);
String str = "success";
response.setCharacterEncoding("utf-8");
response.getWriter().print(str);
}
}
html:
<form:form modelAttribute="comment" class="form" method="post">
<fieldset>
<legend> 账单信息 </legend>
<table class="table" style="width: 100%;">
<tr>
<th>ID</th>
<td><input name="commentId" value="<%=uid%>"
readonly="readonly" /></td>
<th>用户ID</th>
<td><input name="restaurantId" class="easyui-validatebox"
data-options="required:true" readonly="readonly" /></td>
</tr>
<tr>
<th>商家名称</th>
<td><input name="restaurantName" readonly="readonly" /></td>
<th>创建时间</th>
<td><input name="createTime" readonly="readonly" /></td>
</tr>
<tr>
<th>消费金额</th>
<td><input name="commentSpending" /></td>
<th>抵金卷面额</th>
<td><input name="voucherDenomination" /></td>
</tr>
<tr>
<th>审核状态</th>
<td><select>
<option value="2">待检测</option>
<option value="3">已检查(通过)</option>
<option value="4">已检查(不通过)</option>
</select></td>
<th>原因</th>
<td><input name="commentRemark" /></td>
</tr>
<tr>
<th>账单</th>
<td colspan="3"><div style="width: 600px" id="commentPic"></div>
</td>
</tr>
</table>
</fieldset>
</form:form>
控制台如下:
2014-05-23 13:32:09,514 [http-bio-8080-exec-1] INFO [com.sd.microMsg.controller.BakCommentController] - TSdComment [commentId=11, userId=null, restaurantId=1, voucherId=null, restaurantName=笑嘻嘻, commentMlb=null, commentContext=null, commentPersonNo=null, voucherDenomination=30.0, commentSpending=300.0, commentPictureId=null, commentPraiseCount=null, commentNegativeCount=null, commentSelectCount=null, commentSubCommentCount=null, commentReContext=null, commentRemark=mohu, commentCheckedStatus=null, commentBusinessStatus=null, createType=null, createTime=Sat May 17 19:05:52 GMT+08:00 2014, lastActiveTime=null, checkedTime=null, commentTempConsumptionTopNum=null, commentTempPerCapitaConsumptionTopNum=null, commentType=null, commentMlzs=null, commentPlusMlzs=null, commentMark=null, commentSubMark=null, version=null, dtype=null]