Start webapp (EJB3+JSF2) with method from bean instead of page - jsf

I'm pretty new to EJB + JSF and I'd like that my webapp's entry point would be a method of a bean instead of a view.
I mean, when someone types www.mypage.com I'd like it to go to a method of a bean and then be redirected to a view rather than go to any xhtml... how can I do this?
EDIT:
I need to load some data from the database to show it in the view that the user sees when he enters the app. It's not authentication, it's just that I need some data to be loaded before the user sees the website.

Annotating the method that loads the info with #PostConstruct gets the work done :D Thanks to Luiggi Mendoza http://stackoverflow.com/users/1065197/luiggi-mendoza

Related

jsf displaying alert outside managed bean

In the existing application there is a requirement to put some filter when any service is being called from the managed bean.
Filter is across the service call and will do some configured validation either using some config file or Rules Engine and based on it will popup a message that need user input and accordingly will proceed.
Problem is that ones the request goes out from managed bean i am not getting the way to popup a message with user input.
Any help or suggestions are welcome.
it's a bit difficult without code to see but you can try calling FacesContext and create FacesMessage object into a java filter.

Unable to get my custom JSF Login Portlet working

I am using webSphere portal 6.1. I wrote my own custom login portlet and placed in the Login page from the portal server. The page loads fine the problem is when I try to login. It takes 2 attempts to login. The first time when I try to login, the page refreshes (I don’t see any exception on the consol). If I enter the user id/password for the second time, the login works perfectly
After extensive debugging this is what I found,
The first time when I try to login, the process action get called but it skips the managed bean and call the doView. The second time, the process action get called and the managed bean get called (login is successful) and then the doView get called. So I decided to take a look at the html output, sure enough the action url is different. The url for initil load is
action="/wps/portal/esb/!ut/p/c5/04_SB8K8xLLM9MSSzPy8xBz9CP0os3h355AAV0dnEwMLPycTA8_AUINQL1cLAwNXU_1wkA6zeAMcwNFA388jPzdVvyA7rxwAtTMgag!!/dl3/d3/L0lDU0lKSkpDZ3BSQ1FvS1VRa2chL29Gb2dBRUlRaGpGRUlBQWpPTTRSa0JTVkpRcEdnZ0d0QUEhIS80QzFiOVdfTnIwUkprQWxJTVJKa1FsTUlpUi1BLzdfR0NUUEVBQzQwOEdQRDBJUTFGUVQ2MzMwRzUvaWJtLmludi8xNDc3ODc5NDM0MDAvamF2YXguc2VydmxldC5pbmNsdWRlLnBhdGhfaW5mby8lMGpzcHMlMEVTQkxvZ2luUG9ydGxldFZpZXcuanNw/”
and the url after the first attempt to log in is,
action="/wps/portal/esb/!ut/p/c5/dVBHsoPIAjvLHIAy2bCEJhswqaFh4yKZYBNMhtPP-7u_GWmhhUpSlW7J7Y99ujVVujRDn35v6JawLxUEjiwAGudskcZ1F-LQkDkcl5lbdEM4_fJbbrTOBZkX2IL2cnErKCnLzP80PixbcorQg6IgXFALwr9M8r9W_D8g4DdbG7ryFt-S-_9tq470t00obsBSFK4yt-CGxtAzP6zOKQ6-zFgbhmonj9NYXMDQBZUQSxfpYzX7SkSCADgOujiRZCdG-60JFN3PGnR5ybH8ZXU5DtZ3H1rY88xwaBE5IwQ6x78oXEfRuiOKe9WXyAopGRCQT_jDM42lgrK_TGHPyk-g1J1S-d9w_CXXFcO_Nz49ZsTpSe_VZ45OkgeOdG8eFNi-pdxYCUTWZOMSJkG2DCBGqB1QdVeiqf5pqJEu9gleebgMHtuTNt37IR4IXqU4HbNYNN40KyO1W3lh7NR3sOewBJKX1KXWEFelg2lciJqklUX068fRNUzJuNUulevvXrnRbAaWZEhBWddE9qM1_gMrffrp9fjouTXpPMRTRGu-uMfxDn-9fE7ePjiTZ6ceGqnCfHVPzh7ucwV7sx8rwNEGm1JCVPQaa715RygFAiwcr72mgQ726FrWMWPchGbrp5Th3ZY9nDL5mEXIYSaA67MPaUAs9eiMlnFalHBRYnruS0asZU_YDo2Ugbl7KJplfRRitT0nMPCzqfICQExjlJjOPT4OPIRPo3EGYarhN50qHnihCJTZIFgURycbf8IiG5wSsvS4MYITZHUqYzDcNVC7cxvIen-_msergaAj6XtBo9GuWwADk8xSfb5jCslG0O8uem8wILCTobn3VGKkMHQ3xR9Xw5y9MoGHeKEaG0I5_JB2kmOW2eWTiJY4i9o6bLxzPwM5nwGSAif8fqCuffi36iX0Pu1OgcsF_95QIhOKKYUv9prKxO_6xbf3WmOSaJCDLfrRpPaK3CYnHFkt8oxodC8uG2fvJ2F4aLYyMd6vrb1IbL2fgESn2LPEf4ZSkH6PeaKONvL9mm5Prguu8ZvwMPzk7z6b1_OLI5iox6ivHBoZb32Y0far1f7atWl0jpaUYilQd9xIClj36LL3oTjZNbLmTiKaQcH8dcea7YFayk8bk3S2dC_8cn9afvJQ6ZAzOd-_cInXqhfj7eWb5Jj4nbXKK4k9mmJw6358T99Yvx1Rqpf6fj_FnJiKsQlPn7g7x1tjrwMTxvI3GbTALcHCI3fl-zw2rT_bYvONm7glRu93peJk7zG3sdtGzhb--RcqYJzK/dl3/d3/L0lDU0lKSkthWWtLQ2xFWm1ZQSEhL29Pb2dBRUlRaGpCS0VRQUFBRVpDZ0dRNEtRcGNFb2lBWUFEUkVBd0FCaUlCZ0FMRVFEQUFBQSEhLzRDMWI5V19OcnhRREVTWklKUkNBLzdfR0NUUEVBQzQwOEdQRDBJUTFGUVQ2MzMwRzUvMTQ3Nzg3OTY4NTYzL2phdmF4LnNlcnZsZXQuaW5jbHVkZS5wYXRoX2luZm8vJTBqc3BzJTBFU0JMb2dpblBvcnRsZXRWaWV3LmpzcA!!/"
Can some please explain me what going on here?
Thanks
i think i found a solution for my problem. it seems that JSF is trying to save it's viewstate in the session, but public session is not enabled by default for anonymous user. So changing the state saving method in web.xml from server to client seem to do the trick

jsf - intercept when user departs page

I have the following 3 simple pages in a JSF app.
index.html
start.html
confirmSuccess.thml
start.html is a simple form, with a "GO!" button and a html link back to index.html. When the user clicks GO! it kicks of a transaction in the backing bean. If this fails, I want start.html form redisplayed, but with an error explaining failure. So, I have an error flag set in my backing bean, and the error display is managed using rendered="" attribute.
Simple, right?
So, how do I handle the case where the user then clicks the html link to index.html, and from index.html clicks the link back to start.html, or simply clicks back. That is, how do I clear intercept that user has departed the start page, and clear the error flag so I know second time around that I don't need to display the error.
Is this possible without getting deep into the bowels of JSF lifecycle management?
Note The 3 pages listed is a gross oversimplification of my webapp, in reality, there multiple pages a user can go to from start.html, so I don't want to have a solution where I need to track the page someone arrives at to clear flags for start.
Sounds like you're transferring request scoped data around in a session scoped bean. Make the bean which holds request scoped data request scoped and instruct the browser to not cache the dynamic pages so that it is forced to re-request the page from the webserver instead of from its history.
That said, I'd rather use a combination of FacesContext#addMessage() and <h:messages/> to display errors. You can use an clientId of null to denote global message. They are in turn already request based so that you don't need to hassle with a flag to render/unrender an error display.
E.g.
public void submit() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error!"));
}
with
<h:messages globalOnly="true" />

page redirection problem via Bean in Js1.1

I have a requested scoped managed bean, called AuthenticationBean. I am developing a smal application with a login module, user activation and deactivation.
When I click on the activate or deactivate link, then the action is processed in AuthenticationBean. I want thereafter redirect to some page depending on the activate or deactivate link.
I have tried the following in the bean constructor:
FacesContext.getCurrentInstance().getExternalContext().redirect("/user/activate.jsp");
But this code is not working. Please help me.
have tried the following in the bean constructor:
Do not try to redirect in the bean constructor - do it in an action method.

JSF project wiring

I am writing a simple enough program using JSF, and I need some advice about how to go about it. I have a jsp which takes a unique ID and has to find out if the ID exists in 3 different databases. If it does it should display a message telling user, where it exists, otherwise it should give the user option to add the ID to a particular database.
I have the jsp page which has a text field for the input ID and I have a button called "Submit" which should trigger the process of querying the db to see where the ID exists. My question is, how to structure this project, in terms of front end, middle teir and db layer.
I have a JSP page, when the user clicks the Submit button, I have a listener in the managed bean which gets executed. I have also read up that the listeners can either be a managed bean or a separate class. Should I have a separate class which is the listener? If so, should it be a Servlet mapping in the web.xml file, so all request get forwarded to this class. Should there then be a separate DAO class where the actual query
gets executed. We are using hibernate as well.
I would jsut like to hear people comments about how many classes there should be and how a particular ID Check will flow through the program from JSP->Servlet(?)-> DAO and then back to the same jsp. There is only JSP , there will be no other navigation pages.
Any direction will be much appreciated.
I have used Spring in the past, and this would be a breeze fore me with Spring, using the Controllers to delegate the requests to the appropriate service, and then the service would call the DAO class. But here we are using JSF and it has to be a JSF web page. I have not used JSF before so I am unsure about the different components needed. I have the front end jsp and a DAO class with the actual query, just wondering about the middle tier, with business logic. How does the front end request after clicking the Submit button get to the middle tier, what wiring is required? Is it in the web.xml?
I would suggest you take a good long look at Spring framework. Here's a Spring MVC tutorial to get you started.

Resources