Q: How to terminate the session?
A: In order to terminate the session you can
use session invalidate method.
This is an example how to terminate the session
from the action method of a backing bean:
public String logout() {
FacesContext fc =
FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
session.invalidate();
return "login_page";
}
The following code snippet allows to terminate
the session from the jsp page:
<% session.invalidate(); %> <c:redirect
url="loginPage.jsf" />
Q: How to implement "Please, Wait..."
page?
A: The client-side solution might be very simple.
You can wrap the jsp page (or part of it you
want to hide) into the DIV, then you can add
one more DIV that appears when user clicks the
submit button. This DIV can contain the animated
gif you speak about or any other content.
Scenario: when user clicks the button, the JavaScript
function is called. This function hides the
page and shows the "Wait" DIV. You
can customize the look-n-fill with CSS if you
like.
This is a working example:
<%@ taglib uri="http://java.sun.com/jsf/html"
prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core"
prefix="f" %>
<f:loadBundle basename="demo.bundle.Messages"
var="Message"/>
<html>
<head>
<title>Input Name Page</title>
<script>
function gowait() {
document.getElementById("main").style.visibility="hidden";
document.getElementById("wait").style.visibility="visible";
}
</script>
</head>
<body bgcolor="white">
<f:view>
<div id="main">
<h1><h:outputText value="#{Message.inputname_header}"/></h1>
<h:messages style="color: red"/>
<h:form id="helloForm">
<h:outputText value="#{Message.prompt}"/>
<h:inputText id="userName" value="#{GetNameBean.userName}"
required="true">
<f:validateLength minimum="2" maximum="20"/>
</h:inputText>
<h:commandButton onclick="gowait()"
id="submit"
action="#{GetNameBean.action}" value="Say
Hello" />
</h:form>
</div>
<div id="wait" style="visibility:hidden;
position: absolute; top: 0; left: 0">
<table width="100%" height ="300px">
<tr>
<td align="center" valign="middle">
<h2>Please, wait...</h2>
</td>
</tr>
</table>
</div>
</f:view>
</body>
</html>
If you want to have an animated gif of the "Wait"
Page, the gif should be reloaded after the form
is just submitted. So, assign the id for your
image and then add reload code that will be
called after some short delay. For the example
above, it might be:
<script>
function gowait() {
document.getElementById("main").style.visibility="hidden";
document.getElementById("wait").style.visibility="visible";
window.setTimeout('showProgress()', 500);
}
function showProgress(){
var wg = document.getElementById("waitgif");
wg.src=wg.src;
}
</script>
....
....
....
<img id="waitgif" src="animated.gif">
Q: How to reload
the page after ValueChangeListener is invoked?
A: At the end of the ValueChangeListener, call
FacesContext.getCurrentInstance().renderResponse()
Q: How to download PDF file with JSF?
A: This is an code example how it can be done
with action listener of the backing bean.
Add the following method to the backing bean:
public void viewPdf(ActionEvent event) {
String filename = "filename.pdf";
// use your own method
that reads file to the byte array
byte[] pdf = getTheContentOfTheFile(filename);
FacesContext faces
= FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)
faces.getExternalContext().getResponse();
response.setContentType("application/pdf");
response.setContentLength(pdf.length);
response.setHeader( "Content-disposition",
"inline; filename=\""+fileName+"\"");
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(pdf);
} catch (IOException e) {
e.printStackTrace();
}
faces.responseComplete();
}
This is a jsp file snippet:
<h:commandButton immediate="true"
actionListener="#{backingBean.viewPdf}"
value="Read PDF" />
Q: What is JSF?
Ans. JSF stands for Java Server
Faces. JSF has set of pre-assembled User Interface
(UI). By this it means complex components are
pre-coded and can be used with ease. It is event-driven
programming model. By that it means that JSF
has all necessary code for event handling and
component organization. Application programmers
can concentrate on application logic rather
sending effort on these issues. It has component
model that enables third-party components to
be added like AJAX.
Q: What is required for JSF to get started?
Ans. Following things required
for JSF:
• JDK (Java SE Development Kit)
• JSF 1.2
• Application Server (Tomcat or any standard
application server)
• Integrated Development Environment (IDE)
Ex. Netbeans 5.5, Eclipse 3.2.x, etc.
Once JDK and Application Server is downloaded
and configured, one can copy the JSF jar files
to JSF project and could just start coding.
:-)
If IDE is used, it will make things very smooth
and will save your time.