views:

272

answers:

5

I have a requirement to update the properties file based upon the input provided by the user in the jsf page. After updating the file, user session should be cleared and alert box should say 'setting has been updated' and the page should navigate to the login page.

I don't know how to call a alert box from java code. I found this to be possible in asp.net. I searched google, but didn't get any possible solution

How can I make this possible?

All possible solutions are welcome. Thanks in advance.

UPDATE @balusc thanks for your pointer. I am using JSF RI 1.2 and Richfaces 3.3.2

A: 

Something like this

<%

ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
HttpSession session = (HttpSession)ectx.getSession(false);
session.invalidate(); 
%>
<script type="text/javascript">
alert('Setting has been updated');
location.replace('/index.jsp');
</script>
mplungjan
Thanks for the quick reply. Is it possible to implement this without the use of scriptlet?
mvg
What do you mean? If you wish to ALERT the user, you need to use javascript. If you just want to put a box on the page and redirect, you can just show html and use a meta tag. Perhaps a better solution is to redirect to the homepage and have the homepage detect the reason and show a message. Here is a discussion on redirect http://forums.sun.com/thread.jspa?threadID=530461
mplungjan
Sorry, if my question did not sound clear. Clearing session is not a problem. I want to know is there any possiblity in JSF similar to this http://stackoverflow.com/questions/1679218/how-to-call-javascript-function-from-code-behind. Sorry again if my question is not clear
mvg
@mplugjan: Scriptlet is not the same as Javascript. Scriptlets are those old fashioned `<% %>` things whose use is been discouraged since over a decade. Mvg just aksed if it was possible without them (and it is).
BalusC
Ah, I see - never used the name scriplet before. I put the <% %> there to show where the javascript would go and where the java.
mplungjan
+1  A: 

You can use PrimeFaces dialog.

PrimeFaces is an open source of components that enhance GUI and development.

The dialog has action listener on buttons, so when the user approves reading the dialog, you can redirect him to any page the you need.

Odelya
Thanks for the reply. I will look into it and get back to you.
mvg
@Odelya plz correct me, if I am wrong. after completing the server side process, I need to show the dialog box and when the user approves it, I need to naviagate to the other page using javascript correct?
mvg
@mvg - not using javascript. use server side code to navigate:
Odelya
A: 

For displaying alert-box you can use below code in your bean :

JavascriptContext.addJavascriptCall(facesContext, "alert('Setting has been updated');");

Clearing the session :

HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
session.invalidate();
Nayan Wadekar
If I am not mistaken, this is IceFaces specific. You should mention as such.
BalusC
Thanks for the answer. Is JavascriptContext available only at IceFaces?
mvg
@Balusc I expected the same.
mvg
Sorry it was IceFaces specific, but <rich:modalPanel> used to show modal alert box might help you.
Nayan Wadekar
<rich:modalPanel> cannot be called from managed bean to my knowledge. Plz correct me if I am wrong
mvg
Nayan Wadekar
@Nayan Wadekar I expected the answer similar to the one you posted JavascriptContext.addJavascriptCall. Sad it is available only at IceFaces
mvg
Nayan Wadekar
+1  A: 

Use the rich:modalPanel with the showWhenRendered attribute set to a boolean property of your managed bean. Then just reRender the parent of the modal panel after your call.

List of attributes for modal panel

Eg:

<a4j:outputPanel id="modalParent">
   <rich:modalPanel showWhenRendered="#{bean.someBooleanValue}">

   </rich:modalPanel>
</a4j:outputPanel>

And your button/link would have reRender="modalParent"

Damo
Thanks for the reply. I will work with it and get back to you.
mvg
I tried showwhenrendered as suggested by you with no success. If showwhenrendered is "true" value for this attribute makes a modal panel opened as default when the page loads. It doesn't open after the server side process completed. Plz correct me if I am wrong
mvg
You need to put the modalPanel inside another component and reRender the outer component.
Damo
A: 

I managed to solve this problem by using 'oncomplete' attribute in a4j commandbutton. 'oncomplete' attribute is used to call javascript function after the server side process is completed.

After completing the server side process, I called a javascript function which called an alert box and using

document.location="../login.jsf";

I navigated to the login page.

Any suggestions regarding this solution is welcome.

Thanks every one for the answers and comments.

mvg