!--a11y-->
Adding QuizData Node Elements in Context
Nodes 
In the following diagram, you can see the different context structures of the quiz example application, which were defined in the individual controllers (component controller and view controller).
Note that the diagram illustrates a design time abstraction of the context structures that are actually available at runtime. The main difference between the runtime perspective and design time perspective of the context is that the value nodes represent collections (for example, lists) of concrete node elements (with predefined possible cardinalities) at runtime. At design time, the type of these node elements is defined by the context structure, which is contained in the value node (value attributes or additional child nodes).
You must now instantiate these node elements of the type QuizData in the generic event handler wdDoInit() of the Quiz component controller and store them in the QuizData value node.
The top level value attributes, which are directly below the root node, are automatically created during the Web Dynpro runtime and can therefore be directly initiated by the application development.
Before you implement the wdDoInit() method in the Quiz component controller, you must store the question-answer texts, which are displayed in the example application, in two resource bundles.
1. Add a new directory resources in the src/packages/com/sap/tc/webdynpro/tutorials/quiz directory.

In the Package Explorer, select the node WebDynpro_Quiz ® src/packages ® com.sap.tc.webdynpro.tutorials.quiz. Choose the entry New ® Other in the context menu. A dialog box appears. Select the entry Java (on the left) – Package (on the right). Choose Next and enter the package name com.sap.tc.webdynpro.tutorials.quiz.resources in the following dialog box. Choose Finish.
2. Save the files Questions.properties and Answer.properties.

In the Package Explorer, select the node WebDynpro_Quiz ® src/packages ® com.sap.tc.webdynpro.tutorials.quiz.resources. Choose the entry New ® Other in the context menu. A dialog box appears. Select the entry Simple (on the left) – File (on the right). Choose Next and enter a file name in the following dialog box - for example, Questions.properties. Choose Finish.
|
Questions.properties: Resource bundle for the quiz questions (in the directory src/packages/com/sap/tc/webdynpro/tutorials/quiz/resources) |
|
############################################################## ## ## Question resources for Web Dynpro Tutorial WebDynpro_Quiz ## ##############################################################
Q0 = What is a view? Q1 = What is a view set? Q2 = What is a view area? Q3 = What is a window? Q4 = What is a view composition? Q5 = How do you embed views into other views? Q6 = What is a component interface view? SIZE = 7 |
|
Answers.properties: Resource bundle for the quiz answers (in the directory src/packages/com/sap/tc/webdynpro/tutorials/quiz/resources) |
|
############################################################## ## ## Answer resources for Web Dynpro Tutorial WebDynpro_Quiz ## ##############################################################
A0 = A view is the smallest visual UI entity to be used inside view areas. \ It is a screen area representing controls that logically belong \ together. A1 = A view set provides view containers for embedding views or other \ view sets and layouts them in its own visible area. A2 = Rectangular spaces inside a Web Dynpro view set for the layout of a\ Web Dynpro view. A3 = A browser window displays a Web Dynpro window. Web Dynpro windows are \ made up of views, for example, by defining a view set. A4 = In the development environment, the potential view assemblies are \ specified at design time along with the navigation links, which specify \ the possible changes to the view assembly. The defined set of all view \ assemblies is called the view composition. A5 = A special UI element is the view container UI Element. Views can be \ embedded in other views using this UI element. A6 = A component interface view is an external visual representation of a \ Web Dynpro component to be used for integrating its corresponding window \ (internal visual representation of a component) into a view composition. |
There are three steps for implementing the wdDoInit()method in the controller of the Quiz component:
...
1. Import the question-answer pairs from the resource bundles
2. Create node elements of the type QuizData for individual question-answer pairs
3. Bind node elements of the type QuizData to the QuizData value node
|
Implementation in the Quiz Component Controller |
|
... //@@begin imports import java.util.*; import com.sap.tc.webdynpro.services.sal.localization.api.*; import com.sap.tc.webdynpro.tutorials.quiz.wdp.IPrivateQuiz; import com.sap.tc.webdynpro.tutorials.quiz.wdp.IPublicQuiz; //@@end ...
/** Hook method called to initialize controller. */ public void wdDoInit() { //@@begin wdDoInit()
//===== STEP 1: Load resource bundles for questions and answers ==========
// Get the questions-answer resource handlers for the current session locale // using the factory class WDResourceHandler. IWDResourceHandler resourceHandlerForQuestions = WDResourceHandler.createResourceHandlerForCurrentSession(); IWDResourceHandler resourceHandlerForAnswers = WDResourceHandler.createResourceHandlerForCurrentSession();
// Load the resource bundle "Questions.properties" located // in the package "com.sap.tc.webdynpro.tutorials.quiz.resources.Questions" // for locale set in the resource handler. resourceHandlerForQuestions.loadResourceBundle( "com.sap.tc.webdynpro.tutorials.quiz.resources.Questions" , this.getClass().getClassLoader()); resourceHandlerForAnswers.loadResourceBundle( "com.sap.tc.webdynpro.tutorials.quiz.resources.Answers" , this.getClass().getClassLoader());
// get the number of question-answer-pairs contained in the resource bundle int size = Integer .valueOf(resourceHandlerForQuestions.getString( "SIZE" )).intValue();
//===== STEP 2: Create context node elements of the type QuizData =========
// begin populating context node QuizData Collection questions = new ArrayList(); for (int i = 0; i < size; i++) { // instantiate the new context node element of type QuizData IPublicQuiz.IQuizDataElement quizDataElement = wdContext.createQuizDataElement(); // set contained context value attributes quizDataElement.setQuestion(resourceHandlerForQuestions.getString("Q"+i)); quizDataElement.setAnswer(resourceHandlerForAnswers.getString( "A" +i)); quizDataElement.setCounter(String.valueOf(i+1)+"/"+String.valueOf(size)); // add new context node element of the type QuizeData to ArrayList questions.add(quizDataElement); }
//===== STEP 3: Bind collection of Q&A pairs to context value node QuizData ====
// Bind collection of context node elements to context value node QuizData wdContext.nodeQuizData().bind(questions); //@@end } |
After executing the source text, which is implemented in the wdDoInit() method, the context of the Quiz component controller has the following structure at runtime – note that the diagram shows only three out of the seven node elements of the type QuizData.

The
four top level value attributes within the controller context are initialized
in the next
step.
