Tutorials > Program model > Create new business logic

< Previous | Next >


Create a new view

In this step, we will create a new view and its corresponding JSP page.

A view represents what data will display to the user. In this tutorial, you register the view to a JSP page. When you type the name of the view in a Web browser, the corresponding JSP page loads. In this tutorial this new view is called MyNewView and the JSP page is called MyNewJSPTemplate.jsp.

In this step, we will learn the following information:

In general, creating a new view includes the following steps:


Procedure

  1. Name the view and map it to a JSP page in the struts-config-ext.xml file.

    To map the new view to the new JSP page, we will insert an entry into the struts-config-ext.xml file. Before inserting the new entry into the struts-config-ext.xml file, determine the unique identifier for the store.

    To determine the unique identifier:

    1. Start the test environment.

    2. Open a browser and type the following URL: http://localhost/webapp/wcs/admin/servlet/db.jsp.

    3. In the input box, enter the following SQL statement:

      select STOREENT_ID from STOREENT where IDENTIFIER = 'ConsumerDirect_name';
      

      Where ConsumerDirect_name is the name of the store. Make note of the value here:___________________________


    Map MyNewView to MyNewJSPTemplate.jsp

    Add your new view and JSP page to struts-config-ext.xml file:

    1. Open WebSphere Commerce Developer.

    2. Navigate to Stores > WebContent > WEB-INF.

    3. Right-click the struts-config-ext.xml file; then click Open With > Struts Configuration File Editor.

    4. In the Action Mappings tab:

      1. In the Actions Mappings section, click Add. Delete the default path value /action1 and enter /MyNewView.

      2. In the Action Mapping attributes section, in the Type field, enter com.ibm.commerce.struts.BaseAction.

      The editor should look like the following screen capture:

    5. Enter values for the new view:

      1. In the Global Forwards tab, click Add. Delete the default name success and enter MyNewView.

      2. In the Forward Attributes section, in the Path field, enter /MyNewJSPTemplate.jsp.

      3. In the Forward Mapping Extensions section, in the Class Name field, enter com.ibm.commerce.struts.ECActionForward.

      The editor should look like the following screen capture:

    6. Save the file and leave it open. You will edit the file again later in this tutorial.

    7. Update the registry component named Struts Configuration so the server detects the changes:

      1. Open the Administration Console and select Site on the Administration Console Site/Store Selection page.

      2. Click Configuration > Registry. A list of registry components for the site displays.

      3. Select the check box for the Struts Configuration registry component and click Update. The Registry window reloads listing the status for the selected components as Update... .

      4. Click Refresh to reload the Registry window. When updating is complete, the status column reads Updated.

  2. Create a new properties file in which translatable text for JSP pages are stored.

    In this section, we will create a new properties file to hold any translatable text that is used in the JSP page. Separating translatable text from the JSP page itself makes the task of translation much simpler and is also key to a globalized Web site.

    You will create a properties file for the locale en_US. You can create properties files for each locale you support. For example, if you view the store using the locale fr_FR, you can also create a properties file for the fr_FR you use, containing text in French. You will also create a default properties file, with no locale in the file name. If a customer uses a locale that does not have a corresponding properties file, the text from the default properties file displays.

    The Tutorial_All_en_US.properties file is provided as the code source for the purpose of the tutorial and copying is encouraged to reduce the effort of typing and minimize typing errors.

    To create the properties file:

    1. In the Project Navigator view, navigate to the Stores > Java Resources : src > ConsumerDirect_name directory.

    2. Locate the Tutorial_All_en_US.properties file.

    3. Right-click the Tutorial_All_en_US.properties and select Copy

    4. Right-click again and select Paste.

    5. In the Name Conflict window, type TutorialNLS_en_US.properties. The contents of the file follows:

      # -- SECTION 1 -- # 
      WebSphereCommerceInformation=WebSphere Commerce Developer Tutorials
      Tutorial=Tutorial: Creating new business logic
      ParametersFromCmd= List of parameter-value pairs sent from the controller command
      CalledByControllerCmd=MyNewView was called by a controller command
      CalledByWhichControllerCmd=MyNewView was called by the controller command which is -
      ControllerParm1=ControllerParm1=
      ControllerParm2=ControllerParm2= 
      Example=This is an example of using the
      <if> tag from JSP Standard Tag Library (JSTL)
      UserName=UserName=
      Points=Points=
      Greeting=Greeting=
      UserId=UserId=
      FirstInput=Your first input parameter
      RegisteredUser=is a registered user
      ReferenceNumber=The member reference number of this user is
      NotRegisteredUser=is not a registered user
      BonusAdmin=Bonus Administration
      PointBeforeUpdate=The bonus point before update is
      PointAfterUpdate=The bonus point after update is
      EnterPoint=Please enter the points, then submit it to the controller command
      # -- END OF SECTION 1 -- # 
      

    6. Close the TutorialNLS_en_US.properties and Tutorial_All_en_US.properties files.

    7. Create the default properties file that is used when a locale-specific properties file is not found:

      1. Right-click the TutorialNLS_en_US.properties file, and select Copy.

      2. Right-click again and select Paste.

      3. In the Name Conflict window, enter the file name TutorialNLS.properties.

      4. Click OK.

    When Rational Application Developer builds the Stores project, it will copy the new properties files to the Stores project in the Web Content\WEB-INF\classes\ConsumerDirect_name directory and will be used as a resource bundle by the new JSP page. A resource bundle contains the text that displays in the JSP page through a Web browser and will be retrieved from this file.

  3. Create a new JSP page for the new view.

    In this section, we will create the MyNewJSPTemplate.jsp file that is used with MyNewView. When creating this page, create a new blank JSP page and then copy and paste appropriate sections from the MyNewJSPTemplate_All.jsp source code file.

    MyNewJSPTemplate_All.jsp is provided as the source code for the purpose of the tutorial and copying is encouraged to reduce the effort of typing and minimize typing errors.

    To create the new JSP page:

    1. In the Enterprise Explorer view, navigate to the Stores > WebContent > ConsumerDirect_name directory.

    2. Double-click the MyNewJSPTemplate_All.jsp file to open it for editing.

    3. From the ConsumerDirect_name folder's pop-up menu, select New > File to create the new JSP page in this folder.

    4. Specify values for the new file,...

      1. In the File name field, enter MyNewJSPTemplate.jsp.

      2. Click Finish.

      3. The MyNewJSPTemplate.jsp file opens. Click the Design, Source, and Preview tabs for different views of the file.

      4. Click the Source tab of MyNewJSPTemplate_All.jsp then copy the text between <?xml version="1.0" and content="IBM Software Development Platform" />.

      5. Click the Source tab of MyNewJSPTemplate.jsp then replace the entire generated code by pasting the copied text onto this page.

    5. Copy a preparation section from the MyNewJSPTemplate_All.jsp file into the new MyNewJSPTemplate.jsp file. This preparation section sets the place holders for updates we will make to the file.

      1. Click the Source tab of MyNewJSPTemplate_All.jsp then copy the text between the <%--PREPARATION SECTION and END OF PREPARATION SECTION --%> markers, but not the markers themselves.

      2. Paste the copied text to the end of the new JSP page.

    6. Copy sections 1A and 2 from MyNewJSPTemplate_All.jsp file into your new MyNewJSPTemplate.jsp file. Place the new text between the <!-- SECTION 1A -->, <!-- END OF SECTION 1A -->, <!-- SECTION 2 -->, and <!-- END OF SECTION 2 --> markers. These sections introduce the following text into the MyNewJSPTemplate.jsp file:

      <!-- SECTION 1A -->  
       
         
      <%@ include file="include/JSTLEnvironmentSetup.jspf"%>  
      
      <!-- END OF SECTION 1A -->
      
      <!-- SECTION 2 -->
      
      <fmt:setLocale value="${CommandContext.locale}" />
      <fmt:setBundle basename="${sdb.directory}/TutorialNLS" var="tutorial" />
      
      <!-- END OF SECTION 2-->
      

      Section 1A includes the JSTLEnvironmentSetup.jspf file that sets up environment variables. Section 2 creates the resource bundle object that retrieves information from the properties file and it sets the locale. TutorialNLS refers to the name of the properties file that you customized in the previous step. The .properties file extension for TutorialNLS.properties is not included because the Java Standard Tag Library (JSTL) syntax requires that the file extension is not included in the setBundle tag.

    7. Add text to the JSP page by copying text from the MyNewJSPTemplate_All.jsp file into the MyNewJSPTemplate.jsp file. Copy section 3 from MyNewJSPTemplate_All.jsp file into the MyNewJSPTemplate.jsp file. This section introduces the following text into the JSP page:

      <!-- SECTION 3 -->
        
      <h2><fmt:message key="WebSphereCommerceInformation" bundle="${tutorial}" />
      </h2>
       
      <h3><fmt:message key="Tutorial" bundle="${tutorial}" />
      </h3>
      
      <!-- END OF SECTION 3 -->
      

      Section 3 introduces an image that is located in the store-specific image sub-folder, in the Stores project, in the Web Content\ConsumerDirect_name\images directory. This section also retrieves text from the properties file.

    8. Save the changes you made to the MyNewJSPTemplate.jsp file.

  4. Create and load access control policies for the view.

    Command-level access control must be specified for the new view. In this case, the command-level access control policy specifies that all users are allowed to execute the view. Note that this type of access control policy is acceptable for the development environment, but it may not be suitable for other circumstances.

    The access control policy is defined by the MyNewViewACPolicy.xml file, which you placed into the following directory, as part of the preparatory steps: WC_EAR\xml\policies\xml. Before loading the new policy, open the MyNewViewACPolicy.xml file and examine the content to see how this level of access control is structured. Reviewing this section will help you understand how to update the level of access control when needed.

    To load the new policy:

    1. If it is started, stop the test environment.

    2. At a command prompt, navigate to the following directory: WCDE_INSTALL/\bin

    3. Run the acpload command. The acpload command has the following form:

      acpload db_host_name db_name db_user db_password inputXMLFile schema_name
      

      where

      • db_host_name is the hostname of machine on which the development database runs. This parameter is required for remote databases only.

      • db_name is the name of the development database.

      • db_user is the name of the database user.

      • db_password is the password for the database user.

      • inputXMLFile is the XML file containing the access control policy specification. In this case, specify MyNewViewACPolicy.xml.

      • schema_name is:

        • the database user who created the database and owns the schema

        • the user who owns the tables

        • Do not specify this parameter. This parameter is not applicable.

      The following is an example of the command, with variables specified:

      acpload Demo_Dev db2user db2userpassword MyNewViewACPolicy.xml DB2USER
      

    4. Run the acpload command with the access control policy XML file as the only parameter:

      acpload MyNewViewACPolicy.xml
      

    5. Navigate to the WCDE_INSTALL/\logs directory. Inspect the acpload.log and messages.txt files to ensure that the access control policy loaded successfully. The messages.txt file might not exist if the load completed successfully. Also check if policy files were created successfully in the WC_EAR\xml\policies\xml directory: MyNewViewACPolicy_idres.xml and MyNewViewACPolicy_xmltrans.xml. These two files are created as part of a successful idresgen utility process. If any other error files are generated in this directory, this indicates that there was a problem.

  5. Test MyNewView

    The final step of creating a new view is to test the new view in the WebSphere test environment. When testing the new view, and later when testing the new commands, first launch the home page of the store. Since the new view is registered specifically to the store, launching the home page establishes an activity in the context of the store, allowing the view to display. Similarly, the controller command you create later is also registered specifically to the store and requires the use of the appropriate activity.

    To test the new view:

    1. Start the test environment.

    2. Navigate to the Stores > WebContent > ConsumerDirect_name directory.

    3. Select the index.jsp file; then click Run > Run on Server. The store home page displays in the Web browser.

    4. A message might display asking you to select a server. Select Choose an existing server and click Finish.

    5. In the Web browser enter the following URL:

      http://localhost/webapp/wcs/stores/servlet/MyNewView
      

      The new JSP page displays as shown in the following screen capture:

< Previous | Next >


+

Search Tips   |   Advanced Search