Summary on 20080716: close the current page through backend bean method

来源:互联网 发布:哈喽聊天软件 编辑:程序博客网 时间:2024/05/21 00:49

There is such process that: in home page, click some link/button to open one pop up child window, in the window, input/edit/add some content and click  'submit' button to submit the change, if the change is saved successfully, close the child window and refresh the home page if necessary.

For this case, in JSP page, it can be handled by ajax easily, however, in normal jsf(with out ajax), it's quite one tricky and hard-to-resolve problem. The dificulty is that: save is done by backend bean, while close and refresh is done by JavaScript, backend bean can't close/refresh page directly.

To achive this, the only way is that: in backend bean, return some state/value to frontend. And fortunately that we can do this by FacesContext. Refer the following code:

//Get the current context;FacesContext context = FacesContext.getCurrentInstance();//Get servlet response;HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();PrintWriter writer = response.getWriter();writer.write("");the first script code will refresh the home pagethe second script code will close the child page;

The related link:
1. web page for summary of js code that commonly used:
http://www.utf.com.cn/article/s1024
2. one simple sample for the usage:
http://www.itzhe.cn/article/20080116/55203.html

原创粉丝点击