嗨我正在使用spring MVC..i wan在从控制器传递一些值时给用户一些警告。
这是我的控制员:
@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:/";
}
这是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);
}
});
}
我想知道如何从地图获取数据到jsp
答案 0 :(得分:1)
由于您想要在成功删除时重定向,而不是在不成功删除时重定向,您是否可以处理AJAX响应中的重定向。
例如,建立马克的答案:
@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;
}
然后询问JQuery中的响应以确定是否重定向:
$.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);
}
});
您可能需要在Javascript中使用重定向网址,因为它可能需要webapp名称 - 例如。 window.location.replace("/yourWebApp/");
答案 1 :(得分:0)
修复请求中的数据:
$.ajax({
type: "POST",
url: "/controller/deleteTeam",
data: { 'teamID' : team_ID }
success: function(response){
.............
}
});
和控制器方法:
@RequestMapping(value="/deleteTeam", method=RequestMethod.POST)
public @ResponseBody String editDeleteRecodes(HttpServletRequest request) {
request.getParameterMap(); // your parameter teamID will be here, extract it
........
}
或
public @ResponseBody String editDeleteRecodes(@RequestParam("teamID") String teamID) {