需要引入接口
ServletResponseAware
1 public class Login extends ActionSupport implements SessionAware,ServletResponseAware 2 { 3 4 private HttpServletResponse response = null; 5 .... 6 public void setServletResponse(HttpServletResponse response) { 7 // TODO Auto-generated method stub 8 this.response = response; 9 }10 11 public String execute()12 {13 Person person = new Person();14 PersonDAO personDAO = new PersonDAO();15 if(personDAO.findById(id)==null)16 {17 System.out.println("用户不存在");18 try {19 response.setContentType("text/html; charset=UTF-8");20 response.setCharacterEncoding("UTF-8"); //防止乱码;21 PrintWriter out = response.getWriter();22 out.print("");23 out.flush();24 out.close();25 }26 catch (IOException e) {27 // TODO Auto-generated catch block28 e.printStackTrace();29 }30 }31 ....32 } 33 }34
不过很多网页,像淘宝之类的,他们的页面出错不是这么处理的,这种方法弹出提示框以后不能再返回页面