Skip to main content

[JSF] Changes of source code in JSF in Action

JSF in Action
I got this book 3 years years ago and I am reviewing these days. In this book, old version of JSF is used. (I can't find the exact version is using in the book)  During the time, something has changed. So, when I tried to execute sample Project Track project in the book, it caused errors. I was struggling the errors and searched API to soleve them. Finally I could see inbox page after I fixed some code.

Origianally, the source code in login() method in AuthenticationBean was like following:

Visit visit = new Visit();
visit.setUser(newUser);
visit.setAuthenticationBean(this);
setVisit(visit);

//Constants.VISIT_KEY_SCOPE => sessionScope.
//Constants.VISIT_KEY => visit
getApplication().createValueBinding("#{" + Constants.VISIT_KEY_SCOPE +
         Constants.VISIT_KEY + "}").setValue(facesContext, visit);


The createValueBinding() method comes from Java EE API and it is deprecated now. Refferring Java EE API,  it is recommended to call getExpressionFactory() method and then use createValueExpression() method. (see http://download.oracle.com/javaee/5/api/javax/el/ExpressionFactory.html#createValueExpression(javax.el.ELContext, java.lang.String, java.lang.Class)

Visit visit = new Visit();
visit.setUser(newUser);
visit.setAuthenticationBean(this);
setVisit(visit);


ValueExpression vExpr = getApplication().getExpressionFactory().createValueExpression(getFacesContext().getELContext(),
                    "#{" + Constants.VISIT_KEY_SCOPE + Constants.VISIT_KEY + "}",
                    Object.class);
vExpr.setValue(getFacesContext().getELContext(), visit);


The createMethodBinding() method in the old version also changed to createMethodBinding() method.
Through this code, JSF register Visit Class as a bean named visit into the bean management facility at a runtime. In the above, VISIT_KEY_SCOPE constant is sessionScope, so you can refer Visit instance with name 'visit' in any page.

Comments

Popular posts from this blog

Apply Kotlin DataBinding to Android Studio Generated Main Activity

I posted how to setup Kotlin and DataBinding in Android Stuido in the last blog (http://marksunghunpark.blogspot.com.au/2017/04/kotlin-binding-library-setup-in-android.html). In this post, I am going to how to use DataBiding in the MainActivity when you create a navigation drawer project from Android Studio templates. Layouts You will have four layouts in app/src/res/layout folder: app/src/main/res/layout/activity_main.xml app/src/main/res/layout/app_bar_main.xml app/src/main/res/layout/content_main.xml app/src/main/res/layout/nav_header_main.xml And activity_main.xml contains all other layout using include layout. You need to have tag in activity_main.xml , app_bar_main.xml and content_main.xml . If you don't have the tag, Binding library cannot recognise the sub layouts properly. Binding library doesn't support toolbar and navigation drawer yet, so you can use using BindingAdapter if you want to use binding library.(But I'm gong to skip this part for simplici

How to test AsyncTask in Android

In Android, test is not as easy as any other platform. Because Android test cannot be run without emulator. Particulary when it comes to AsyncTask or Service, it is difficult to test because they are different type of thread and hard to check their result. Then, how can we ensure the result of AsyncTask valid? AsyncTask is a thread and an asynchnorous as the name means. So, we need to wait for it finishes its job and need to capture the event. Then, when it happens in AsyncTask. It can be one of onBackground() and onPostExecute() methods. It doesn't matter you use onBackground() or onPostExecute() but I prefer onPostExecute(). Anyway, we can test an AsyncTask if we can hook it. Then, how can we hook it? For that, we can use callback pattern. But we need to delay main thread to wait for the AsyncTask's job done because we want to check the result. So the structure for the test would be like: 1. Create AsyncTask A 2. Injection a callback into A 3. Wait until A finish 4.

Let's start Lambda in Android

In Android programming, I think Java & J2EE programming are not that different, we developers put many boiler plate code particularly when we use anonymous instances. Example1. Simple example button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view){ //Do something } }); In the above example, what we really need is inside of onClick() method. Other code is actually decorations and we don't want it. How about we can pass just onClick method body as a parameter of setOnClickListener method like below? button1.setOnClickListener(view->{ //Do something }); That's what we have exactly wanted. We can use the code style in Java 8. I think you already might know about Java 8 Lambda but there is no official support for Java 8 in Android 8. It would be awesome we can use it in Android and there is a way to be able to use Lambda in Android as well. You can refer my previous blog to setup RetroLambda in Andro