0

Hi i am using spring MVC..i wan to give some alert to user when pass some value from controleer.

this is my part of controller:

@RequestMapping(value="/deleteTeam", method=RequestMethod.POST)
public String editDeleteRecodes(@ModelAttribute NewTeams newTeams, Map<String, Object> map){        
    Teams teams = new Teams();
    teams.setTeamID(newTeams.getTeamID());
    try{
        teamService.delete(teams);
    }catch (DataIntegrityViolationException ex){
        map.put("error", "x");
        //System.out.println("aaaaa");
    }

    return "redirect:/";


}

this is front END in jsp

<script type="text/javascript" >
 function doAjaxPostTeamDelete (team_ID){
     //get the values               
     //var team_ID = $('#teamID').val();         

     $.ajax({
        type: "POST",
        url: "/controller/deleteTeam",
        data: "teamID=" + team_ID,          
        success: function(response){  
              // we have the response  
            if(x="${error}"){
                alert("You Cant Delete");   
                         }      

            }, 
            error: function(e){  
              alert('Error: ' + e);  
            }    
     });     
 }   

just i want to how get data from from map to jsp

Zcon
  • 153
  • 6
  • 18
  • I guess this post is useful for you, http://stackoverflow.com/questions/16232833/how-to-respond-with-http-400-error-in-a-spring-mvc-responsebody-method-returnin – ali4j Nov 19 '13 at 07:17
  • map.put("error", "x");tell me how to catch tis data in jsp ( // we have the response if(x="${error}"){ alert("You Cant Delete"); } },) – Zcon Nov 19 '13 at 07:44

1 Answers1

1

Since you want to redirect on successful deletion but not on unsuccessful deleteion, could you handle the redirect in the AJAX response.

For example, building on Mark's answer:

@RequestMapping(value="/deleteTeam", method=RequestMethod.POST)
public @ResponseBody String editDeleteRecodes(@RequestParam("teamID") String teamID) {     
    Teams teams = new Teams();
    teams.setTeamID(newTeams.getTeamID());

    String result;
    try {
        teamService.delete(teams);
        result = "{deleted: true}";
    } catch (DataIntegrityViolationException ex){
        result = "{deleted: false}";
    }

    return result;        
}

And then interrogate the response in the JQuery to determine whether to redirect or not:

$.ajax({
    type: "POST",
    url: "/controller/deleteTeam",
    data: "teamID=" + team_ID,          
    success: function(response){  
        // we have the response  
        if (response.deleted)
            window.location.replace("/"); // Do the redirect
        } else {
            alert("You can't delete");
        }}, 
    error: function(e){  
          alert('Error: ' + e);  
        }    
 });   

You may need to play around with the redirect URL in the Javascript as it may require the webapp name - eg. window.location.replace("/yourWebApp/");

Will Keeling
  • 22,055
  • 4
  • 51
  • 61