The TaskLogHandler
class processes the task log actions and is mapped to the /taskLog/
URL pattern:
package com.gieman.tttracker.web; import com.gieman.tttracker.domain.*; import com.gieman.tttracker.service.TaskLogService; import com.gieman.tttracker.vo.Result; import static com.gieman.tttracker.web.SecurityHelper.getSessionUser; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import javax.json.JsonObject; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/taskLog") public class TaskLogHandler extends AbstractHandler { static final SimpleDateFormat DATE_FORMAT_yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); @Autowired protected TaskLogService taskLogService; @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor(DATE_FORMAT_yyyyMMdd, true)); } @RequestMapping(value="/find", method = RequestMethod.GET, produces = {"application/json"}) @ResponseBody public String find( @RequestParam(value = "idTaskLog", required = true) Integer idTaskLog, HttpServletRequest request) { User sessionUser = getSessionUser(request); Result<TaskLog> ar = taskLogService.find(idTaskLog, sessionUser.getUsername()); if (ar.isSuccess()) { return getJsonSuccessData(ar.getData()); } else { return getJsonErrorMsg(ar.getMsg()); } } @RequestMapping(value = "/store", method = RequestMethod.POST, produces = {"application/json"}) @ResponseBody public String store( @RequestParam(value = "data", required = true) String jsonData, HttpServletRequest request) throws ParseException { User sessionUser = getSessionUser(request); JsonObject jsonObj = parseJsonObject(jsonData); String dateVal = jsonObj.getString("taskLogDate"); Result<TaskLog> ar = taskLogService.store( getIntegerValue(jsonObj.get("idTaskLog")), getIntegerValue(jsonObj.get("idTask")), jsonObj.getString("username"), jsonObj.getString("taskDescription"), DATE_FORMAT_yyyyMMdd.parse(dateVal), jsonObj.getInt("taskMinutes"), sessionUser.getUsername()); if (ar.isSuccess()) { return getJsonSuccessData(ar.getData()); } else { return getJsonErrorMsg(ar.getMsg()); } } @RequestMapping(value = "/remove", method = RequestMethod.POST, produces = {"application/json"}) @ResponseBody public String remove( @RequestParam(value = "data", required = true) String jsonData, HttpServletRequest request) { User sessionUser = getSessionUser(request); JsonObject jsonObj = parseJsonObject(jsonData); Result<TaskLog> ar = taskLogService.remove( getIntegerValue(jsonObj.get("idTaskLog")), sessionUser.getUsername()); if (ar.isSuccess()) { return getJsonSuccessMsg(ar.getMsg()); } else { return getJsonErrorMsg(ar.getMsg()); } } @RequestMapping(value = "/findByUser", method = RequestMethod.GET, produces = {"application/json"}) @ResponseBody public String findByUser( @RequestParam(value = "username", required = true) String username, @RequestParam(value = "startDate", required = true) Date startDate, @RequestParam(value = "endDate", required = true) Date endDate, HttpServletRequest request) { User sessionUser = getSessionUser(request); Result<List<TaskLog>> ar = taskLogService.findByUser( username, startDate, endDate, sessionUser.getUsername()); if (ar.isSuccess()) { return getJsonSuccessData(ar.getData()); } else { return getJsonErrorMsg(ar.getMsg()); } } }
The TaskLogHandler
class will, hence, be mapped to the following URLs:
/taskLog/find
will map to the find
method using a GET
request/taskLog/store
will map to the store
method using a POST
request/taskLog/findByUser
will map to the findByUser
method using a GET
request/taskLog/remove
will map to the remove
method using a POST
requestWe have also introduced a new annotation: the @InitBinder
annotation.
The @InitBinder
annotation is used to mark a method as "data binding aware". The method initializes the WebDataBinder
object with editors that are used to transform String parameters into their Java equivalent. The most common need for this transformation is in the case of dates.
A date can be represented in many different ways. All of the following dates are equivalent:
Sending date representations via HTTP requests can be confusing to say the least! We all understand what most of these dates represent, but how can we convert these dates into a java.util.Date
object? This is where the @InitBinder
method is used. The code to specify the required date format involves registering a CustomDateEditor
constructor for the Date
class:
binder.registerCustomEditor(Date.class, new CustomDateEditor(DATE_FORMAT_yyyyMMdd, true));
This will allow Spring to use the DATE_FORMAT_yyyyMMdd
instance to parse the dates sent by clients in the yyyyMMdd
format. The following URL will now be transformed correctly for the arguments required in the findByUser
method:
/taskLog/findByUser?username=bjones&startDate=20130719&endDate=20130812
The true
argument in the CustomDateEditor
constructor ensures that empty dates are given the value null
.
3.135.200.14