jsf-2 interview questions
Top jsf-2 frequently asked interview questions
When should I use an <h:outputLink>
instead of an <h:commandLink>
?
I understand that a commandLink
generates an HTTP post; I'm guessing that outputLink
will generate HTTP gets. That said, most of the JSF tutorial material I've read uses commandLink
(almost?) exclusively.
Context: I am implementing a wee little demo project that shows a header link to a user page, much like Stack Overflow's...
...and I am not sure if commandLink
(perhaps using ?faces-redirect=true
for bookmarkability) or outputLink
is the right choice.
Source: (StackOverflow)
I'm using Primefaces in a JSF 2 application. I have a <p:dataTable>
, and instead of selecting rows, I want the user to be able to directly execute various actions on individual rows. For that, I have several <p:commandLink>
s in the last column.
My problem: how can I pass a row ID to the action started by the command link so that I know which row to act on? I tried using an <f:attribute>
:
<p:dataTable value="#{bean.items}" var="item">
...
<p:column>
<p:commandLink actionListener="#{bean.insert}" value="insert">
<f:attribute name="id" value="#{item.id}" />
</p:commandLink>
</p:column>
</p:dataTable>
But it always yields 0 - apparently the row variable f
is not available when the attribute is rendered (it works when I use a fixed value).
Anyone has an alternative solution?
Source: (StackOverflow)
The JSF <h:outputStylesheet>
, <h:outputScript>
and <h:graphicImage>
components have a library
attribute. What is this and how should this be used? There are a lot of examples on the web which use it as follows with the common content/file type css
, js
and img
(or image
) as library name depending on the tag used:
<h:outputStylesheet library="css" name="style.css" />
<h:outputScript library="js" name="script.js" />
<h:graphicImage library="img" name="logo.png" />
How is it useful? The library
value in those examples seems to be just repeating whatever is already been represented by the tag name. For a <h:outputStylesheet>
it's based on the tag name already obvious that it represents a "CSS library". What's the difference with the following which also just works the same way?
<h:outputStylesheet name="css/style.css" />
<h:outputScript name="js/script.js" />
<h:graphicImage name="img/logo.png" />
Also, the generated HTML output is a bit different. Given a context path of /contextname
and FacesServlet
mapping on an URL pattern of *.xhtml
, the former generates the following HTML with the library name as request parameter:
<link rel="stylesheet" type="text/css" rel='nofollow' href="/contextname/javax.faces.resource/style.css.xhtml?ln=css" />
<script type="text/javascript" src="/contextname/javax.faces.resource/script.js.xhtml?ln=js"></script>
<img src="/contextname/javax.faces.resource/logo.png.xhtml?ln=img" alt="" />
While the latter generates the following HTML with the library name just in the path of the URI:
<link rel="stylesheet" type="text/css" rel='nofollow' href="/contextname/javax.faces.resource/css/style.css.xhtml" />
<script type="text/javascript" src="/contextname/javax.faces.resource/js/script.js.xhtml"></script>
<img src="/contextname/javax.faces.resource/img/logo.png.xhtml" alt="" />
The latter approach makes in hindsight also more sense than the former approach. How exactly is the library
attribute then useful?
Source: (StackOverflow)
Yesterday I saw a presentation on Java Server Faces 2.0 which looked truly impressive, even though I am currently a happy ASP.NET MVC / jQuery developer. What I liked most about JSF was the huge amount of AJAX-Enabled UI components which seem to make development much faster than with ASP.NET MVC, especially on AJAX-heavy sites. Integration testing looked very nice too.
Since the presentation only emphasized the advantages of JSF, I'd like to hear about the other side as well.
So my questions are:
- What are the main disadvantages of Java Server Faces 2.0?
- What might make a JSF developer consider using ASP.NET MVC instead of JSF?
Source: (StackOverflow)
Can anyone clarify how we can use in general, or a in real world example, this snippet?
<f:metadata>
<f:viewParam id="id" value="#{bean.id}" />
<f:viewAction action="#{bean.init}" />
</f:metadata>
Source: (StackOverflow)
I am working with a rather large app written in JSF 1.2.
JSF 1.2 is around 6 years old now. I need to upgrade to JSF 2.0. How painful will this be? I noticed that some attributes in custom tags have been changed etc.
Source: (StackOverflow)
What is the most correct way to include another XHTML page in an XHTML page? I have been trying different ways, none of them are working.
Source: (StackOverflow)
I noticed that there are different bean scopes like:
@RequestScoped
@ViewScoped
@FlowScoped
@SessionScoped
@ApplicationScoped
What is the purpose of each? How do I choose a proper scope for my bean?
Source: (StackOverflow)
I found a problem when using the <h:commandLink>
or <h:commandButton>
in an include page: the action
and actionListener
associated with the UICommand
component are simply not being invoked. What are the possible causes and solutions for this?
Source: (StackOverflow)
What is the difference between action
and actionListener
, and when should I use action
versus actionListener
?
Source: (StackOverflow)
I would like to output a bit of Facelets code conditionally.
For that purpose, the JSTL tags seem to work fine:
<c:if test="${lpc.verbose}">
...
</c:if>
However, I'm not sure if this is a best practice? Is there another way to achieve my goal?
Source: (StackOverflow)
I started using JSF 2.0 with Facelets recently and got puzzled by new composite components knowing existing <ui:include>
and other templating techniques offered by Facelets 1.x.
What is the difference between those approaches? Functionally they seem to offer about the same: <ui:param>
vs <cc:attribute>
, <ui:insert>
+<ui:define>
vs tag files, reuse of the existing templates. Is there anything besides syntax and clear interface specification in case of composite components? Could performance differ?
Source: (StackOverflow)
I'm trying to write a custom servlet (for AJAX/JSON) in which I would like to reference my @ManagedBeans
by name. I'm hoping to map:
http://host/app/myBean/myProperty
to:
@ManagedBean(name="myBean")
public class MyBean {
public String getMyProperty();
}
Is it possible to load a bean by name from a regular servlet? Is there a JSF servlet or helper I could use for it?
I seem to be spoilt by Spring in which all this is too obvious.
Source: (StackOverflow)
I have this structure:
WebContent
resources
components
top.xhtml
company
about_us.xhtml
index.xhtml
top.xhtml
is a component, that is used in index.xthml
and about_us.xhtml
too.
top.xhtml
<ul>
<li><a rel='nofollow' href="index.xhtml">Home</a></li>
<li><a rel='nofollow' href="company/about_us.xhtml">About us</a></li>
...
</ul>
So my problem is, when the current page is index.xhtml
the component generates URLs correctly, but when the current page is about_us.xhtml
, it generates wrong URLs. I cannot use relative path because it's going to generate the wrong URL too. I think it is because the component is based on the current path of the *.xhtml
page.
The only solution I could found out is:
<ul>
<li><a rel='nofollow' href="${pageContext.request.contextPath}/webname/index.xhtml">Home</a></li>
<li><a rel='nofollow' href="${pageContext.request.contextPath}/webname/about_us.xhtml">About us</a></li>
...
</ul>
But I think is not 'elegant' at all. Any ideas?
Source: (StackOverflow)
What is the best possible way to invalidate session within a JSF 2.0 application? I know JSF itself does not handle session. So far I could find
private void reset() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(false);
session.invalidate();
}
- Is this method correct? Is there a way without touching the
ServletAPI?
- Consider a scenario wherein a
@SessionScoped
UserBean handles the
login-logout of a user. I have this method in the same bean. Now
when I call the reset()
method after I'm done with necessary DB
updates, what will happen to my current session scoped bean? since
even the bean itself is stored in HttpSession
?
Source: (StackOverflow)