Announcement Announcement Module
No announcement yet.
BindingResult question Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • BindingResult question

    In my controller I catch attempts at adding duplicate records to the database to prevent a nasty database constraint error from being thrown displayed to the User. To do that, I add an error to BindingResult. That code is below:
    		@RequestMapping(value="Teams/AddActivity", method=RequestMethod.POST)
    		public String editTeam(@RequestParam(value="teamid", required=true) int teamId,
    							   @RequestParam(value="activityId", required=true) int activityId,
    							   TeamActivity teamActivity, BindingResult result,Model model, Model message) {
    	        if (result.hasErrors()) {
    	        	String message_txt= "";
    	        	List<SurveyTeam> Teams = surveyTeamService.getTeamById(teamId);
    	        	model.addAttribute("teams", Teams);
    	            return "redirect:ViewDetails?id="+teamId;
    	        System.out.println("POST addactivity method");
    	        List<TeamActivity> checkForDup = teamActivityService.checkDuplicate(teamId, activityId);
    	        System.out.println("size of checkForDup" +checkForDup.size());
    	        if(checkForDup.size() == 1 ){
    	        	result.addError(new ObjectError("duplicate", "Cannot insert duplicate activity type"));
    	        	return "redirect:AddActivity?teamid="+teamId;
    When this duplicate error occurs I return to the page the person was previous on for them to try again. I'd like to display an error message on that page. I'm not particularly tied to binding result but I would like to avoid having to use a scriptlet like <% request.getparam("error")... %>

    Here's my jsp section using spring:hasBindErrors tag:
    <form:form method="POST" action="AddActivity">
    			<spring:hasBindErrors name="*">
    	            <c:forEach items="${result.allErrors}">
    	                <c:out value="${result.defaultMessage}" />
    	                <c:out value="${status.errorMessage}"/>
    							<th width="50px">Activity Type</th>
    							<th width="50px">Original Need</th>
    							<select id="activityType" name="activityType">
    								<option value="null"></option>
    								<c:forEach items="${at}" var="at">
    									<option value="${at.activityType}"><c:out value="${at.activityType}"/></option>
    						<td id="oneedList">
    					<tr><td><input type="Submit" value="Add ActivityType" /></td>
    					<c:forEach items="${teams}" var="teams">
    						<input type="hidden" name="teamid" id="teamid" value="<c:out value="${}"/>"/>
    Currently, it seems that hasBindErrors is evaluating to false, but I'm not sure why. When I POST the form to the server, the server logs indicate there is an error does exist.

    Error in object 'duplicate': codes []; arguments []; default message [Cannot insert duplicate activity type]]
    Any help would be appreciated. Thanks
    Last edited by ronnelson929; Jan 19th, 2012, 11:07 AM.