|
|
Subject: JSF PhaseListener Vs Servlet Filter - msg#00147
List: java.facelets.user
We use a Homegrown security application which on successful authentication returns the RequestHeader Variables i.e. employee Id etc. We use JSF Spring Hibernate Wondering whether i should JSF PhaseListener OR Servlet Filter to retrieve RequestHeader Variables. This is what i am doing currently not sure its best practice though. Any suggestions/pointers highly appreciated public class LoginPhaseListener implements PhaseListener { public void
afterPhase(PhaseEvent pe) { FacesContext facesContext = pe.getFacesContext(); String viewId = pe.getFacesContext().getViewRoot().getViewId(); if (viewId.endsWith(".xhtml")) { String managedBeanName = getManagedBeanNameFromView(viewId); Object object = facesContext.getApplication().createValueBinding("#{" + managedBeanName + "}").getValue(facesContext); if (object == null) logger.error("OnPageLoad
cannot be executed, no such managed bean:"+ managedBeanName); else { Login loginBean = (Login) object; loginBean.onPageLoad(); } } } public String getManagedBeanNameFromView(String viewId) { String pageName = viewId.substring(1, viewId.length() - 6); System. out.println("Name="+StringUtils. capitalize(pageName)+"Bean"); return pageName+"Bean"; } JSF Backing Bean: public class LoginBean implements Login { public void onPageLoad() { //System.out.println("***Inside onPageLoad******"); Map requestHeaderMap = FacesContext. getCurrentInstan
ce().getExternalContext().getRequestHeaderMap();
String empID = (String) requestHeaderMap.get("EmployeeID"); //Retrieve UserId and User roles from database and populate it with POJO UserInfo userInfo //pojo userInfo.setUserId(userId); userInfo.setUserRoles(userRoles); /* Store in Session so as to make it available to Spring Layer */ HttpSession userSession = (HttpSession) FacesContext. getCurrentInstance().getExternalContext() .getSession( true); userSession.setAttribute("userInfo", userInfo); } } I am also using Servlet Filter to retrieve Session object public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; // grab the current value from the session UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo"); // Update the user info bean holder for the current thread. UserInfoHolder. setUserInfo(userInfo); // ThreadLocal object // call the chain //System.out.println("In Security Filter"); chain.doFilter(req,resp); // threadLocal no longer needed (will be recreated next time through the filter) //UserInfoHolder.setUserInfo(null); } &n
bsp; Now i am thinking instead of using PhaseListener cant i just use the
Servelt Filter to retrieve the RequestHeader variables, make connection to database instead of using HttpSession Not sure if its best practice though & also how to retrieve RequestHeader variables in Servlet Filter Regards Bansi
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
Was this page helpful?
Thread at a glance:
Previous Message by Date:
click to view message preview
RE: RE: JSF, Facelets & Recursion
Hi,
Thank all 3 of you for
your quick and clear answers. And it could be true that this problem is caused
by the developer, because i’m a newbie
So here is my facelet:
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:d="http://www.donaldson.be/facelets">
<!--
PARAMETERS NEEDED:
- entity: the entity containing the collection used for
the data-table
- isOrderItem: boolean to indicate if it is a datatable
for order-items
-->
<ui:composition>
<t:dataTable
var="item"
value="#{ (isOrderItem==true) ?
entity.orderItems : entity.subWorkorders }"
preserveDataModel="false"
rowIndexVar="rowIndex"
style="width: 100%;">
<h:column>
<t:div style="#{ (isOrderItem==true) ? 'padding:0px
0px 0px 25px;' : '' }">
<t:div styleClass="#{ (rowIndex%2==0) ? 'evenRow' :
'oddRow' }" style="padding:3px 0px 3px 0px;" >
<input type="checkbox" name="selectedItems" value="#{item.nodeId}" />
<h:outputLink value="_javascript_:toggleLayer('div_#{item.nodeId}');"
rendered="#{item.orderItemsCount != 0}">
LINK
</h:outputLink>
<h:outputLabel rendered="#{item.orderItemsCount ==
0}">
NO LINK
</h:outputLabel>
</t:div>
<div id="div_#{item.nodeId}" style="display:none; padding:0px 0px
10px 0px;">
<d:workorder-tree entity="#{item}" isOrderItem="true" />
</div>
</t:div>
</h:column>
</t:dataTable>
</ui:composition>
</html>
And in another
xhtml-page I have
<d:workorder-tree entity="#{WorkorderBean.woEntity}" isOrderItem="false" />
Some more explanation:
-
the first call to this fragment displays the sub-workorders
-
each sub-workorder can contain a list of order-items
-
each order-item can contain a list of order-items
-
if an order-item contains an empty list, the div with id="div_#{item.nodeId}" must be
empty or not rendered
So could someone help to
determine what’s wrong with the use and reference my own el variables
which causes recursion not to work.
Any help greatly
appreciated
Many thanks
Kind Regards
Roel
Roel De Nijs
Consultant
Business Solutions
& Development
E-mail adres: Roel.DeNijs@xxxxxx
MSP n.v.
Veldkant 7
2550 Kontich
Tel : +32 (0)3 454 27 28
Fax :+32 (0)3 454 27 88
http://www.msp.be - info@msp. be
Ondernemingsnummer:
BTW-BE-0870.749.501
The information
contained in this transmission is proprietary and confidential and is intended
only for the use of the individual or entity named above. If the reader of this
message is not the intended recipient, the reader is notified that any
consideration, dissemination or duplication of this communication is strictly
prohibited.
From:
jacob@xxxxxxxxxx [mailto:jacob@xxxxxxxxxx]
Sent: maandag 21 mei 2007 19:49
To: users@xxxxxxxxxxxxxxxxxxxxx
Subject: RE: JSF, Facelets &
Recursion
Everytime this issue has
come up, it's been traced back to a developer issue with how they were
referencing their own EL variables, nothing to do with Facelets/EL/JSF itself.
Hello
all,
I'm working with JSF (MyFaces) and Facelets. I've created a custom facelet
(more precise: via a seperate xhtml-file), containing a tomahawk data-table
But one big problem: in this file there is a reference to itselfs, because i
have to represent a custom tree structure (with checkboxes to delete and
create new nodes/subtrees). so it's seems logical to solve this recursively,
but if i do so, i get a stack-overflow-error.
I'm searching and surfing the net for 2-3 days looking for a solution to this
problem, but apparently it doesn't exist (while in my opinion this is a very
straight forward problem).
and i tried everything i know of:
- using the rendered-attribute as the step-out-endless-loop thing but i've
read that the component tree contains all components and that afterwards the
rendered ones are really rendered and the others aren't, so that's not a
solution
- i also tried with some jstl and the ifhandler, but also learned that there
is a big difference in when the _expression_ is evaluated.
- also tried with some dynamic form of the jsfc-attribute, resulting in a
"ui : remove" if the component hadn't to be rendered, but also no
luck
- ...
So i was wondering: is there a solution for this or some other things i could
try, because it's really frustrating me :censored:
any tips, hints and/or suggestions are more than welcome
thanks in advance
Roel
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxxxxxxx
Next Message by Date:
click to view message preview
Re: JSF, Facelets & Recursion
<t:dataTable var="item"
value="#{ (isOrderItem==true) ? entity.orderItems :
entity.subWorkorders }"
> .....
<d:workorder-tree entity="#{item}" isOrderItem="true" />
.....
</t:dataTable>
You have a bad combination of build-time and render-time tags:
http://wiki.java.net/bin/view/Projects/FaceletsFAQ#Why_doesn_t_my_c_if_ui_repeat_ui
You really need a component which supports a hierarchical model, such as
Tomahawks t:tree2 or the Quipukit treetable:
http://www.teamdev.com/quipukit/component_index.jsf#treetable
Alternatively, if your recursion is going to have a maximum depth, you
could nest a fixed number of tables and used their rendered attribute to
display or hide them.
<h:dataTable var="item1">
<h:dataTable var="item1child" rendered="item1.hasChildren">
<h:dataTable var="item1childchild"
rendered="item1child.hasChildren">
....
You could probably take advantage of facelets' tag file or include
features to reuse some code here, but you have to be very careful when
mixing build and render-time expressions (as you've discovered).
Roger
And in another xhtml-page I have
<d:workorder-tree entity="#{WorkorderBean.woEntity}"
isOrderItem="false" />
Some more explanation:
- the first call to this fragment displays the sub-workorders
- each sub-workorder can contain a list of order-items
- each order-item can contain a list of order-items
- if an order-item contains an empty list, the div with
id="div_#{item.nodeId}" must be empty or not rendered
So could someone help to determine what’s wrong with the use and
reference my own el variables which causes recursion not to work.
Any help greatly appreciated
Many thanks
Kind Regards
Roel
Roel De Nijs
Consultant
Business Solutions & Development
E-mail adres: Roel.DeNijs@xxxxxx <mailto:Roel.DeNijs@xxxxxx>
MSP n.v.
Veldkant 7
2550 Kontich
Tel : +32 (0)3 454 27 28
Fax :+32 (0)3 454 27 88
http://www.msp.be <http://www.msp.be/> - info@msp. be <mailto:info@xxxxxx>
Ondernemingsnummer: BTW-BE-0870.749.501
The information contained in this transmission is proprietary and
confidential and is intended only for the use of the individual or
entity named above. If the reader of this message is not the intended
recipient, the reader is notified that any consideration, dissemination
or duplication of this communication is strictly prohibited.
------------------------------------------------------------------------
*From:* jacob@xxxxxxxxxx [mailto:jacob@xxxxxxxxxx]
*Sent:* maandag 21 mei 2007 19:49
*To:* users@xxxxxxxxxxxxxxxxxxxxx
*Subject:* RE: JSF, Facelets & Recursion
Everytime this issue has come up, it's been traced back to a developer
issue with how they were referencing their own EL variables, nothing to
do with Facelets/EL/JSF itself.
Hello all,
I'm working with JSF (MyFaces) and Facelets. I've created a custom
facelet (more precise: via a seperate xhtml-file), containing a tomahawk
data-table
But one big problem: in this file there is a reference to itselfs,
because i have to represent a custom tree structure (with checkboxes to
delete and create new nodes/subtrees). so it's seems logical to solve
this recursively, but if i do so, i get a stack-overflow-error.
I'm searching and surfing the net for 2-3 days looking for a solution to
this problem, but apparently it doesn't exist (while in my opinion this
is a very straight forward problem).
and i tried everything i know of:
- using the rendered-attribute as the step-out-endless-loop thing but
i've read that the component tree contains all components and that
afterwards the rendered ones are really rendered and the others aren't,
so that's not a solution
- i also tried with some jstl and the ifhandler, but also learned that
there is a big difference in when the expression is evaluated.
- also tried with some dynamic form of the jsfc-attribute, resulting in
a "ui : remove" if the component hadn't to be rendered, but also no luck
- ...
So i was wondering: is there a solution for this or some other things i
could try, because it's really frustrating me :censored:
any tips, hints and/or suggestions are more than welcome
thanks in advance
Roel
--------------------------------------------------------------------- To
unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxxxxxxx For
additional commands, e-mail: users-help@xxxxxxxxxxxxxxxxxxxxx
--
----------------------------------------
Ninth Avenue Software
p: +61 7 3137 1351 (UTC +10)
f: +61 7 3102 9141
w: http://www.ninthavenue.com.au
e: info@xxxxxxxxxxxxxxxxxx
----------------------------------------
Previous Message by Thread:
click to view message preview
When does Facelet's re-evaluate ui:fragment rendered expression?
Hi-
I'm using <ui:fragment rendered="#{myBean.rendered}">...
in my page. The business logic behind myBean.isRendered() is a fairly
expensive DB query. When I submit a form that includes that fragment,
the EL for the rendered attribute is being re-evaluated during the Apply
Request Values phase. Is this expected?
I assumed that the rendered attribute
would only be evaluated during the Render View phase, since this is the
only time you'd want to toggle this attribute.
I don't want to store myBean in session/conversation/view
scope. Is there another way that I can prevent my method being called
during the post-back?
Thanks for any tips.
Adam Brod
Product Development Team
Disclaimer: This electronic mail and any attachments are confidential and may be privileged. If you are not the intended recipient, please notify the sender immediately by replying to this email, and destroy all copies of this email and any attachments. Thank you.
Next Message by Thread:
click to view message preview
Re: JSF PhaseListener Vs Servlet Filter
Bansilal Haudakari wrote:
We use a Homegrown security application
which on successful authentication returns the RequestHeader Variables
i.e. employee Id etc.
We use JSF Spring Hibernate
Wondering whether i should JSF PhaseListener OR Servlet Filter to
retrieve RequestHeader Variables. This is what i am doing currently not
sure its best practice though. Any suggestions/pointers highly appreciated
Well, the FacesContext isn't available to a Servlet Filter, so unless
you want to do some refactoring of your code I'd stick with what you've got.
If you are listening on the RESTORE_VIEW phase, I don't suppose a phase
listener is much different from a Servlet Filter which has been mapped
to the Faces Servlet (except that the FacesContext is available).
A Servlet Filter can be useful for intercepting a request before the
Faces Servlet is invoked, for example doing URL mapping, sending HTTP
redirects, customizing ServletRequest/Response objects etc etc.
* *
*public* *class* LoginPhaseListener *implements* PhaseListener
{
**
* *
*public* *void* afterPhase(PhaseEvent pe)
{
FacesContext facesContext = pe.getFacesContext();
String viewId = pe.getFacesContext().getViewRoot().getViewId();
*if* (viewId.endsWith(".xhtml")) {
String managedBeanName = getManagedBeanNameFromView(viewId);
Object object = facesContext.getApplication().createValueBinding("#{" +
managedBeanName + "}").getValue(facesContext);
*if* (object == *null*)
/logger/.error("OnPageLoad cannot be executed, no such managed bean:"+
managedBeanName);
*else* {
Login loginBean = (Login) object;
loginBean.onPageLoad();
}
}
}
*public* String getManagedBeanNameFromView(String viewId) {
String pageName = viewId.substring(1, viewId.length() - 6);
System./out/.println("Name="+StringUtils./capitalize/(pageName)+"Bean");
*return* pageName+"Bean";
}
*_JSF Backing Bean:_*
* *
*public* *class* LoginBean *implements* Login {
**
* *
*public* *void* onPageLoad() {
//System.out.println("***Inside onPageLoad******");
Map requestHeaderMap =
FacesContext./getCurrentInstan
ce/().getExternalContext().getRequestHeaderMap();
String empID = (String) requestHeaderMap.get("EmployeeID");
//Retrieve UserId and User roles from database and populate it with POJO
UserInfo userInfo //pojo
userInfo.setUserId(userId);
userInfo.setUserRoles(userRoles);
/* Store in Session so as to make it available to Spring Layer */
HttpSession userSession =
(HttpSession) FacesContext./getCurrentInstance/().getExternalContext()
.getSession(*true*);
userSession.setAttribute("userInfo", userInfo);
}
}
I am also using Servlet Filter to retrieve Session object
* *
*public* *void* doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) *throws* IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
// grab the current value from the session
UserInfo userInfo = (UserInfo)
request.getSession().getAttribute("userInfo");
// Update the user info bean holder for the current thread.
UserInfoHolder./setUserInfo/(userInfo); // ThreadLocal object
// call the chain
//System.out.println("In Security Filter");
chain.doFilter(req,resp);
// threadLocal no longer needed (will be recreated next time through the
filter)
//UserInfoHolder.setUserInfo(null);
}
&n bsp;
Now i am thinking instead of using PhaseListener cant i just use the
Servelt Filter to retrieve the RequestHeader variables, make connection
to database instead of using HttpSession
Not sure if its best practice though & also how to retrieve
RequestHeader variables in Servlet Filter
Regards
Bansi
------------------------------------------------------------------------
Be a better Globetrotter. Get better travel answers
<http://us.rd.yahoo.com/evt=48254/*http://answers.yahoo.com/dir/_ylc=X3oDMTI5MGx2aThyBF9TAzIxMTU1MDAzNTIEX3MDMzk2NTQ1MTAzBHNlYwNCQUJwaWxsYXJfTklfMzYwBHNsawNQcm9kdWN0X3F1ZXN0aW9uX3BhZ2U-?link=list&sid=396545469>from
someone who knows.
Yahoo! Answers - Check it out.
--
----------------------------------------
Ninth Avenue Software
p: +61 7 3137 1351 (UTC +10)
f: +61 7 3102 9141
w: http://www.ninthavenue.com.au
e: info@xxxxxxxxxxxxxxxxxx
----------------------------------------
|
|