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(); %>
Ques 17
How to reload the page after ValueChangeListener is invoked?
At the end of the ValueChangeListener, call FacesContext.getCurrentInstance().renderResponse()
Ques 18
How to download PDF file with JSF?
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" />
Ques 19
How to show Confirmation Dialog when user Click the Command Link?
ah:commandLink assign the onclick attribute for internal use. So, you cannot use it to write your own code. This problem will fixed in the JSF 1.2. For the current JSF version you can use onmousedown event that occurs before onclick.
<script language="javascript">
function ConfirmDelete(link) {
var delete = confirm('Do you want to Delete?');
if (delete == true) {
link.onclick();
}
}
</script>
. . . . <h:commandLink action="delete" onmousedown="return ConfirmDelete(this);">
<h:outputText value="delete it"/> </h:commandLink>
Ques 20
What is the different between getRequestParameterMap() and getRequestParameterValuesMap()
getRequestParameterValuesMap() similar to getRequestParameterMap(), but contains multiple values for for the parameters with the same name. It is important if you one of the components such as <h:selectMany>.
Ques 21
Is it possible to have more than one Faces Configuration file?
Yes. You can define the list of the configuration files in the web.xml.
This is an example:
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config-navigation.xml,/WEB-INF/faces-beans.xml</param-value>
</context-param>
Note: Do not register /WEB-INF/faces-config.xml file in the web.xml . Otherwise, the JSF implementation will process it twice.
Hi there, I guess the Note: column should have been meant or intended for "faces-config.xml" file as thats the default configuration file for JSF (which is similar to struts-config.xml for Struts!!). faces-context.xml file sounds like the user defined config file similar to the aforementioned two xml files.
Ques 22
How to print out html markup with h:outputText?
The h:outputText has attribute escape that allows to escape the html markup. By default, it equals to "true". It means all the special symbols will be replaced with '&' codes. If you set it to "false", the text will be printed out without ecsaping.
For example, <h:outputText value="<b>This is a text</b>"/>
will be printed out like:
<b>This is a text</b>
In case of <h:outputText escape="false" value="<b>This is a text</b>"/>
you will get:
This is a text
Ques 23
h:inputSecret field becomes empty when page is reloaded. How to fix this?
Set redisplay=true, it is false by default.
Ques 24
Discuss life cycle in JSF.
The life cycle of a JavaServer Faces page is similar to that of a JSP page: The client makes an HTTP request for the page, and the server responds with the page translated to HTML. However, because of the extra features that JavaServer Faces technology offers, the life cycle provides some additional services to process a page.
JSF application lifecycle consist of six phases which are as follows
Restore view phase
Apply request values phase; process events
Process validations phase; process events
Update model values phase; process events
Invoke application phase; process events
Render response phase
Restore View Phase:
When a request for a JavaServer Faces page is made, such as when a link or a button is clicked, the JavaServer Faces implementation begins the restore view phase.
Apply Request Values Phase:
After the component tree is restored, each component in the tree extracts its new value from the request parameters by using its decode method. The value is then stored locally on the component. If the conversion of the value fails, an error message associated with the component is generated and queued on FacesContext. This message will be displayed during the render response phase, along with any validation errors resulting from the process validations phase.
Process Validations Phase:
During this phase, the JavaServer Faces implementation processes all validators registered on the components in the tree. It examines the component attributes that specify the rules for the validation and compares these rules to the local value stored for the component.
Update Model Values Phase:
After the JavaServer Faces implementation determines that the data is valid, it can walk the component tree and set the corresponding server-side object properties to the components' local values. The JavaServer Faces implementation will update only the bean properties pointed at by an input component's value attribute. If the local data cannot be converted to the types specified by the bean properties, the life cycle advances directly to the render response phase so that the page is rerendered with errors displayed. This is similar to what happens with validation errors.
Invoke Application Phase:
During this phase, the JavaServer Faces implementation handles any application-level events, such as submitting a form or linking to another page.
Render Response Phase:
During this phase, the JavaServer Faces implementation delegates authority for rendering the page to the JSP container if the application is using JSP pages. If this is an initial request, the components represented on the page will be added to the component tree as the JSP container executes the page. If this is not an initial request, the components are already added to the tree so they needn't be added again. In either case, the components will render themselves as the JSP container traverses the tags in the page.