在用@requestbody接受数据后,我想返回消息或实现网页跳转

时间:2015-03-25 01:48:28

标签: ajax json spring-mvc

控制器:

@RequestMapping(value="receive", method=RequestMethod.POST, consumes="application/json")
@ResponseBody
public  RegInfo receiveData(@RequestBody RegInfo info){
    String reg_check = regInfoService.checkRegInfo(info);     
     ......
}

RegInfo:

public class RegInfo {

private String account;

private String passwords;

private String realname;

private String phonenumber;

private String sex;

private String mailname;
.......}

register.jsp:

    $("#sub").click(function(){
    var m = {
            "account": $("#_account").val(),
            "passwords": $("#_pass").val(),
            "realname": $("#real_name").val(),
            "phonenumber": $("#phone_number").val(),
            "sex": $("input:checked").val(),
            "mailname": $("#mail_name").val()
        };
    $.ajax({
        type:"POST",
        async : false,
        url:"/demo/user/receive",
        dataType:"json",
        contentType:"application/json; charset=utf-8",
        data:JSON.stringify(m),
        success:function(data){
            alert("ok");
            alert(data.realname);
        },
        erroe:function(data){
            alert("保存失败 ")
        }
    })
});

现在我想检查控制器中的RegInfo。如果检查结果合法,我想跳转到login.jsp之类的其他网页,如果它是非法的,我想返回一些消息并在register.jsp中显示消息。我怎么才能意识到这一点? 完成控制器:

    @Controller
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private UserService userService;    
    @Autowired
    private RegInfoService regInfoService;

    @RequestMapping("/login")
    public String homePage(){
        return "user/login";
    }

    @RequestMapping("/loginin")
    public String toLogin(@ModelAttribute("user")User user){
        String u = userService.loginCheck(user);
        System.out.println(u);
        if(u == "success"){
            return "user/success";
        }
        else{
            return "user/login";
        }
    }

    @RequestMapping("/register")
    public String toRegister(){
        return "user/register";
    }

    @RequestMapping("/success")
    public String toSuccess(){
        return "user/success";
    }
    @RequestMapping(value="receive", method=RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public RegInfo receiveData(@RequestBody RegInfo info){

        String reg_check = regInfoService.checkRegInfo(info);
        System.out.println(reg_check);
        System.out.println(info);
        System.out.println(info.getRealname());
        return info;
    }
}

2 个答案:

答案 0 :(得分:0)

  

我想跳转到login.jsp之类的其他网页,如果它是非法的,我   想要返回一些消息并在register.jsp中显示消息。怎么样   我可以意识到吗?

简单,在您的AJAX成功处理程序中使用以下URL参数进行重定向:

$.ajax({
        type:"POST",
        async : false,
        url:"/demo/user/receive",
        dataType:"json",
        contentType:"application/json; charset=utf-8",
        data:JSON.stringify(m),
        success:function(data){
            alert("ok");
            alert(data.realname);
            if (data.realname != undefined || data.realname!= null) {
              window.location = '/login?realname=' + data.realname;
            }
        },
        erroe:function(data){
            alert("保存失败 ")
        }
    });

编辑: 根据您的评论,控制器方法将如下所示:

@Controller
@RequestMapping("/user")
public class LoginController {

    @RequestMapping("/login")
    public String homePage(Mode model, @RequestParam(value="realname",required=false)String realName){
        if(realName!=null && (!realName.trim().isEmpty())){
           model.addAttribute("regSuccessUser",realName);
        }
        return "user/login";
    }
}

和redirect语句如下所示:

window.location = '/user/login?realname=' + data.realname;

注意:URL以contextPath开头以避免404问题

答案 1 :(得分:0)

我测试如下:

<input type="button" value="test" onClick="newpage()">
function newpage(){
window.location="/demo/user/login";}

然后它成功跳转到login.jsp。在ajax中,它已经运行到“window.location”但没有跳。它看起来很奇怪。

最终解决方案: 错误:

<input type="submit" id="sub" value="save" >

真:

<input type="button" id="sub" value="save" >

表格提交两次。