04. Java EE 6: DI (JSR 330) and CDI (JSR 299)


Lab Exercises


For the sake of the simplicity of the lab, most exercises are provided in the form of "ready-to-open-and-run" NetBeans projects.  (Many of them are borrowed from "glassfish-samples" and "Java EE 6 tutorial".)  Please feel free to create them from scratch if you want to. 

It is strongly encouraged, leveraging what is provided in this lab, you do your own experimentation meaning creating/adding your own code as much as you can. 

If you have written some code that might help everyone else, please feel free to share them on this codecamp email alias or directly send them to the instructors.  Your name will be recognized in this lab if your sample code is chosen to be included.  For the tasks that need to be done, please see the "Things to be done" section above.)
  Before you start this lab, you have to get sample applications from "glassfish-samples" and "Java EE tutorial" as described in LAB-4530: Java EE 6 - Examples.

Exercise 1: @Inject simple usages

더보기


Exercise 2: @Inject usage with @Qualifier

더보기


Exercise 3: Scope


더보기


Exercise 4: Producer Methods

더보기

Exercise 5: Managed Bean with Lifecycle methods

더보기

Exercise 6: Build and run "weld-servlet" sample application

더보기


Exercise 7: Build and run "weld-guess" sample application

더보기

Exercise 8: Event

더보기


 

Homework Exercise


1. The first part of homework is to modify inject_qualifier project you worked on in Exercise 2 above as following.  (You might want to create a new project by copying inject_qualifier project.  You can name the newly copied project in any way you want.  Here it is referred to as my_inject_qualifier.)
  • Goal: Exercising qualifier
    • Define another qualifier called @SemiFormal.
    • Define SemiFormalGreeting class with @SemiFormal annotation
    • Modify HelloServlet class to inject @SemiFormal greeting object.
2. The second part of homework is to modify inject_scope_conversation project you worked on in Exercise 3 above as following.  (You might want to create a new project by copying inject_scope_conversation project.  You can name the newly copied project in any way you want.  Here it is referred to as my_inject_scope_conversation.)  - If you already submitted homework or would use to use a single project as a base as in the original homework, that is perfectly fine as well.
  • Goal: Exercising scope
    • Create a bean called my_application_scope_bean with application scope, which has a field called my_application_scope_counter.  Use the my_application_scope_counter to keep track of how many times the Say Hello button was clicked.
    • Display the value of my_application_scope_counter.
    • Create another bean called my_conversation_scope_bean with conversation scope, which has a field called my_conversation_scope_counter.  Now every time my_application_scope_counter hits multiple of 5, for example, 5, 10, 15, ..., start a conversation.  Every time my_application_scope_counter hits multiple of 5 plus 4, for example, 9, 14, 19, ..., stop the conversation.
    • The my_conversation_scope_counter gets incremented each time the Say Hello button is clicked but starting from a new random number whenever a new coversation scope gets started.
    • Display the value of my_conversation_scope_counter.
  • Goal: Exercising event handling
    • Every time my_application_scope_bean hits the prime numbers up to 20, fire a event.  The event handler should display a message "Prime number x is hit!". This message can be displayed either HTML response message or system log.
Hint: Given that we have not covered how to use JSF/Facelets yet, the following hint shows how to display things in the index.xhtml file - make sure you copy resources directory and template.xhtml as well as seen in inject_scope_conversation project.  The "Prime number x is hit!" can be printed out on system log through sysem.out.println(...);

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">
    <ui:composition template="/template.xhtml">

        <ui:define name="title">Simple Greeting</ui:define>
        <ui:define name="head">Simple Greeting</ui:define>
        <ui:define name="content">
            <h:form id="greetme">
                <p><h:outputLabel value="Enter your name: " for="name"/>
                    <h:inputText id="name" value="#{printer.name}"/></p>
                <p><h:commandButton value="Say Hello" action="#{printer.createSalutation}"/></p>
                <p><h:commandButton value="Start conversation" action="#{printer.startConversation}"/></p>
                <p><h:commandButton value="End conversation" action="#{printer.endConversation}"/></p>
                <p><h:outputText value="printer.salutation = #{printer.salutation}"/> </p>
                <p><h:outputText value="printer.old_salutation = #{printer.old_salutation}"/> </p>
                <p><h:outputText value="myConversationStatus.conversationSattus = #{myConversationStatus.conversationSattus}"/> </p>
                <p><h:outputText value="my_application_scope_bean.my_application_scope_counter = #{my_application_scope_bean.my_application_scope_counter}"/> </p>
                <p><h:outputText value="my_conversation_scope_bean.my_conversation_scope_counter = #{my_conversation_scope_bean.my_conversation_scope_counter}"/> </p>
            </h:form>
        </ui:define>

    </ui:composition>
</html>


2. Send the following files to javaee6-homeworks@sun.com with Subject as homework_javaee6_injection.
  • Zip files of the the my_inject_qualifier and my_inject_scope_conversation NetBeans projects.  (Someone else should be able to open and run them as NetBeans projects.)  You can use your favorite zip utility or you can use "jar" utility that comes with JDK as following.
    • cd <parent directory that contains my_inject_qualifier directory> (assuming you named your project as my_inject_qualifier)
    • jar cvf my_inject_qualifier.zip my_inject_qualifier (my_inject_qualifier should contain nbproject directory)
  • Captured output screens  - name it as homework_javaee6_injection1.gif orhomework_javaee6_injection1.jpg (or homework_javaee6_injection1.<whatver graphics format>) for the first part and homework_javaee6_injection2.gif orhomework_javaee6_injection2.jpg (or homework_javaee6_injection2.<whatver graphics format>)
    • Any screen capture that shows that your program is working is good enough.

 



 

저작자 표시
Trackback 0 Comment 0