Results 1 to 1 of 1

Thread: BindingResult question

  1. #1
    Join Date
    Nov 2011
    Location
    Kansas City, MO
    Posts
    4

    Default 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:
    Code:
    		@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()) {
    	        	logger.info(result.getAllErrors());
    	        	String message_txt= "";
    	        	
    	        	List<SurveyTeam> Teams = surveyTeamService.getTeamById(teamId);
    	        	model.addAttribute("teams", Teams);
    	        	message.addAttribute("error",message_txt);
    	            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:
    Code:
    <form:form method="POST" action="AddActivity">
    			<spring:hasBindErrors name="*">
    	        <h2>Errors</h2>
    	            <c:forEach items="${result.allErrors}">
    	                <c:out value="${result.defaultMessage}" />
    	                <c:out value="${status.errorMessage}"/>
    	            </c:forEach>
    	    	</spring:hasBindErrors>
    		
    				<table>
    					<thead>
    						<tr>
    							<th width="50px">Activity Type</th>
    							<th width="50px">Original Need</th>
    						</tr>
    					</thead>
    					
    					<tr>
    						<td>
    							<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>
    								</c:forEach>
    							</select>
    						</td>
    						<td id="oneedList">
    						</td>
    					</tr>
    					
    					<tr><td><input type="Submit" value="Add ActivityType" /></td>
    					<c:forEach items="${teams}" var="teams">
    					<td>
    						<input type="hidden" name="teamid" id="teamid" value="<c:out value="${teams.id}"/>"/>
    					</td>
    					</c:forEach>
    					</tr>
    					
    				</table>
    			
    			
    			</form:form>
    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 at 11:07 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •