Announcement Announcement Module
Collapse
No announcement yet.
Object attributes becoming NULL after form submission Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Object attributes becoming NULL after form submission

    Hey all,

    I have a form which is used to edit an object. The form does not contain very attribute in the object. My problem is that when I submit my form, any attribute of my object that does not have a corresponding request parameter gets set to NULL when the object is saved?

    Is there a way around this? I do not want to have to fill my form with hidden input fields for those object attributes which I don't want the user to be able to edit simply to prevent them from being set to NULL.

    My annotated controller:

    Code:
    @Controller
    @RequestMapping("/task/taskEdit.htm")
    public class TaskEditController {
    	
    	private TaskService taskService;
    	private UserService userService;
    	private TaskValidator validator;
    	
    	@Autowired
    	public TaskEditController(TaskService taskService, TaskValidator validator) {
    		this.taskService = taskService;
    		this.validator = validator;
    	}
    	
    	@InitBinder
    	public void initBinder(WebDataBinder dataBinder) {
    		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy HH:mm");
    		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    	}
    	
    	@RequestMapping(method = RequestMethod.GET)
    	public String setupForm(@RequestParam(required = true, value = "id") String id, ModelMap model) {
    		AssignmentTask task = (AssignmentTask) taskService.findById(Long.parseLong(id));
    		model.addAttribute("task", task);
    		return "/ticket/taskEdit";
    	}
    	
    	@RequestMapping(method = RequestMethod.POST)
    	public String processSubmit(@ModelAttribute("task") AssignmentTask task, BindingResult result, HttpServletRequest request) {
    		validator.validate(task, result);
    		if(result.hasErrors()) {
    			return "/ticket/taskEdit";
    		} else {
    			task.setUpdateUser(userService.getCurrentUser());
    			task.setUpdatedTime(new Date());
    			taskService.save(task);
    		}
    		return "status/success";
    	}
    }
    Thanks in advance

  • #2
    It seems adding @SessionAttributes("task") annotation and adding the arg SessionStatus status to my processSubmit method and setting status.setComplete() has solved this problem.

    Comment

    Working...
    X