日期:2014-05-20  浏览次数:20766 次

菜鸟问题:在JAVA后台ACTION中输出JS对话框这哪里错了呢??
我的ACTION方法是这样,当执行插入的时候页面就变的一片空白了也不跳转,
    我这句<script language= 'javascript '> alert('删除成功!'); location.href=login.jsp</script> 哪里错了吗,怎么改呢????
public ActionForward insertDept(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DeptForm deptForm = (DeptForm) form;
DeptVO deptVO = new DeptVO();
deptVO.setDepartmentCode(deptForm.getDepartmentCode());
deptVO.setDepartmentID(deptForm.getDepartmentID());
deptVO.setDepartmentName(deptForm.getDepartmentName());
try {
deptDao.insertDept(deptVO);
} catch (Exception e) {
e.printStackTrace();
}
try {
PrintWriter out = response.getWriter();
out.println( " <script language= 'javascript '> alert('删除成功!'); location.href=login.jsp</script> ");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
request.getSession().setAttribute("Record", "N");
return queryDept(mapping,form,request,response);

}
------解决方案--------------------
你把
return改成return null;

public class LoginAction extends Action {


public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

    String user = null;

// Default target to success
    String target = new String("success");

    // Use the LoginForm to get the request parameters
  String username = ((LoginForm)form).getUsername();
  String password = ((LoginForm)form).getPassword();

    // Set the target to failure
    if ( user == null ) {
      ((LoginForm)form).setUsername("username");
      ((LoginForm)form).setPassword("password");
      target = new String("failure");
    }
    else {

      request.setAttribute("USER", user);
    }
    PrintWriter out = response.getWriter();
    out.write("<script language=\"javascript\">alert(\"hello test\"); location.href=\"testhello.jsp\";</script>");
  // Forward to the appropriate View
  return null;

}
}

用action这个代码我也测了没问题。
你的js最终是要到页面的。所以js在哪里测都行的。