2 baihaiqi baihaiqi 于 2015.07.21 10:20 提问

java执行完方法后怎么跳转到jsp
function submitForm1() {
/*  var id = document.getElementById("userId").value;
    if(id == ''){
        alert("id不允许为空!");
        return;
    } */

    $.post("/mission/abc", $('#newForm').serialize(), function(data) {//"/user/save"
        /*alert("您还没有登录");*/
        if(data.state == 'noLogin') {

            alert("皇上,您还没有登录,点击确定跳转到登录界面");
            window.location.href = "../user/login.jsp";//"/user/logintext.jsp";
        }

        if(data.state == 'success'){
            alert("添加成功");
            location = "../user/login.jsp";//"userManager.jsp";
            document.location.reload();
        }
    }, "json");
}

@Before(AjaxLoginInterceptor.class)
public void abc(){

    String type = getPara("type");
    String sex = getPara("sex");
    String num = getPara("num");
    String money = getPara("money");
    String content = getPara("content");
    /*String hours = getPara("hours");
    String min = getPara("min");*/


    /*createTime = hours + ":" +min;*/
    title = type + createTime + num + money;


    setAttr("title", title);
    setAttr("type", type);
    setAttr("personNum", num);
    setAttr("content", content);
    setAttr("sex", sex);
    /*setAttr("createTime", createTime);*/
    setAttr("money", money);
    //renderJsp("newMission.jsp");
    System.out.println("abc");


    Mission mission = getModel(Mission.class, "mission");

    if(mission.get("id") == null) {
        mission.set("id", IDGeneUtil.getRandNum(6));
        mission.set("status", "发布");
        mission.set("title",title);
        mission.set("type",type);
        mission.set("content",content);
        mission.set("personNum",num);
        mission.set("sex",sex);
        /*mission.set("createTime",createTime);*/
        mission.set("money",money);
        mission.set("userId", getSessionAttr("loginUser"));
        mission.save();
    } else if(mission.getInt("id") > 0) {
        mission.update();
    }
    /*renderJsp("../mission/mission.jsp");*/
    forwardAction("/mission");//("/mission");

}

public class AjaxLoginInterceptor implements Interceptor {

    public Map res = new HashMap();//用来存放返回的数据(json)

    public void intercept(ActionInvocation ai) {
        Controller ctl=ai.getController();
        String loginUser = ctl.getSessionAttr("loginUser");

        // 判断是否登录过
        if(loginUser == null || loginUser.equalsIgnoreCase("")) {
            res.put("state", "noLogin");
            //返回保存结果
            ctl.renderJson(res);
            return;
        }

        System.out.println("Before ajaxlogin action invoking");
        ai.invoke();//对action的调用
        System.out.println("After ajaxlogin action invoking");
    }
}

现在拦截器好使了-但是点完发布页面不跳转了-但是数据库能存上-不知道怎么回事-新手-求解答

3个回答

yuke198907
yuke198907   2015.07.21 11:17

你的submitForm1方法是ajax方法,他是根据返回值进行跳转的;而forwardAction("/mission")是直接跳转的;这两个是不应该在一起使用的。

qq806142724
qq806142724   2015.07.21 11:27

比如说你想调用ajax 后 走完流程 如果想动态的跳转 这样写:
String url = "";
url = "地址/xxxxx.jsp?flag=true";
打印url
==================前台================
function(data){
location.href=data;
}

strutce
strutce   Ds   Rxr 2015.07.21 11:28

设置data.state 值返回到ajax方法中在跳转到页面

Csdn user default icon
上传中...
上传图片
插入图片