关于spring中时间类型转换的举例

initBinder是spring自带的,默认使用“/”格式,如“1999/10/10”;当使用“1999-10-10”或“1999.10.10”则要在获取封装参数的类中重写initBinder方法。

package cn.liu.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import cn.liu.domain.User;

//从页面接收参数,封装Javabean的User对象
public class CommandController extends AbstractCommandController {
	
	//指定把参数封装到User对象
	public CommandController() {
		 setCommandClass(User.class);
	}

	@Override
	protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command,
			BindException errors) throws Exception {
		//值被封装命令对象
		User user = (User) command;
		//使用ModelAndView回显
		ModelAndView mav = new ModelAndView();
		//设置model数据,值可以是任意的pojo
		mav.addObject("user", user);
		//指定返回视图页面
		mav.setViewName("index");
		
		return mav;
	}

	@Override
	//获取birthday参数,并使用它的格式
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		String birthday = request.getParameter("birthday");
		if (birthday.contains(".")) {
			binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy.mm.dd"), true));
		}
		else if(birthday.contains("/")) {
			binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/mm/dd"), true));
		}else {
			binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-mm-dd"), true));
		}
	
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC,JSON类型的数据交互通常使用Spring提供的Jackson库实现,具体实现步骤如下: 1. 返回实体对象 首先需要在Controller方法上添加@ResponseBody注解,表示将返回的数据转换成JSON格式。例如: ``` @RequestMapping("/getUser") @ResponseBody public User getUser(int id){ User user = userService.getUserById(id); return user; } ``` 这样就可以将User对象转换成JSON格式返回给前端。 2. ArrayList集合 如果要返回ArrayList集合,则只需要将集合作为Controller方法的返回值即可。例如: ``` @RequestMapping("/getAllUsers") @ResponseBody public List<User> getAllUsers(){ List<User> userList = userService.getAllUsers(); return userList; } ``` 这样就可以将List<User>对象转换成JSON格式返回给前端。 3. Map<String,Object>集合 如果要返回Map<String,Object>集合,则同样将集合作为Controller方法的返回值即可。例如: ``` @RequestMapping("/getUserInfo") @ResponseBody public Map<String,Object> getUserInfo(int id){ User user = userService.getUserById(id); Map<String,Object> userInfo = new HashMap<>(); userInfo.put("id",user.getId()); userInfo.put("name",user.getName()); userInfo.put("age",user.getAge()); return userInfo; } ``` 这样就可以将Map<String,Object>对象转换成JSON格式返回给前端。 4. List<Map<String,Object>>集合 如果要返回List<Map<String,Object>>集合,则同样将集合作为Controller方法的返回值即可。例如: ``` @RequestMapping("/getAllUserInfo") @ResponseBody public List<Map<String,Object>> getAllUserInfo(){ List<User> userList = userService.getAllUsers(); List<Map<String,Object>> userInfoList = new ArrayList<>(); for(User user : userList){ Map<String,Object> userInfo = new HashMap<>(); userInfo.put("id",user.getId()); userInfo.put("name",user.getName()); userInfo.put("age",user.getAge()); userInfoList.add(userInfo); } return userInfoList; } ``` 这样就可以将List<Map<String,Object>>对象转换成JSON格式返回给前端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值