Tutorials > Program model > Create new business logic
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:
- Where to place JSP pages and image files that apply to a specific store.
- How to use Rational Application Developer to create the JSP page.
- How to create the properties file that contains the text for the JSP page.
- How to update the struts-config-ext.xml file to map MyNewView view with MyNewJSPTemplate.jsp
- How to set up access control for the new view.
- How to use the WebSphere test environment to test the new view.
In general, creating a new view includes the following steps:
Procedure
- 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:
- Start the test environment.
- Open a browser and type the following URL: http://localhost/webapp/wcs/admin/servlet/db.jsp.
- 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.jspAdd your new view and JSP page to struts-config-ext.xml file:
- Open WebSphere Commerce Developer.
- Navigate to Stores > WebContent > WEB-INF.
- Right-click the struts-config-ext.xml file; then click Open With > Struts Configuration File Editor.
- In the Action Mappings tab:
- In the Actions Mappings section, click Add. Delete the default path value /action1 and enter /MyNewView.
- 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:
- Enter values for the new view:
- In the Global Forwards tab, click Add. Delete the default name success and enter MyNewView.
- In the Forward Attributes section, in the Path field, enter /MyNewJSPTemplate.jsp.
- 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:
- Save the file and leave it open. You will edit the file again later in this tutorial.
- Update the registry component named Struts Configuration so the server detects the changes:
- Open the Administration Console and select Site on the Administration Console Site/Store Selection page.
- Click Configuration > Registry. A list of registry components for the site displays.
- 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... .
- Click Refresh to reload the Registry window. When updating is complete, the status column reads Updated.
- 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:
- In the Project Navigator view, navigate to the Stores > Java Resources : src > ConsumerDirect_name directory.
- Locate the Tutorial_All_en_US.properties file.
- Right-click the Tutorial_All_en_US.properties and select Copy
- Right-click again and select Paste.
- 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 -- #
- Close the TutorialNLS_en_US.properties and Tutorial_All_en_US.properties files.
- Create the default properties file that is used when a locale-specific properties file is not found:
- Right-click the TutorialNLS_en_US.properties file, and select Copy.
- Right-click again and select Paste.
- In the Name Conflict window, enter the file name TutorialNLS.properties.
- 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.
- 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:
- In the Enterprise Explorer view, navigate to the Stores > WebContent > ConsumerDirect_name directory.
- Double-click the MyNewJSPTemplate_All.jsp file to open it for editing.
- From the ConsumerDirect_name folder's pop-up menu, select New > File to create the new JSP page in this folder.
- Specify values for the new file,...
- In the File name field, enter MyNewJSPTemplate.jsp.
- Click Finish.
- The MyNewJSPTemplate.jsp file opens. Click the Design, Source, and Preview tabs for different views of the file.
- Click the Source tab of MyNewJSPTemplate_All.jsp then copy the text between <?xml version="1.0" and content="IBM Software Development Platform" />.
- Click the Source tab of MyNewJSPTemplate.jsp then replace the entire generated code by pasting the copied text onto this page.
- 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.
- 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.
- Paste the copied text to the end of the new JSP page.
- 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.
- 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.
- Save the changes you made to the MyNewJSPTemplate.jsp file.
- 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:
- If it is started, stop the test environment.
- At a command prompt, navigate to the following directory: WCDE_INSTALL/\bin
- Run the acpload command. The acpload command has the following form:
acpload db_host_name db_name db_user db_password inputXMLFile schema_namewhere
- 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
- Run the acpload command with the access control policy XML file as the only parameter:
acpload MyNewViewACPolicy.xml
- 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.
- 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:
- Start the test environment.
- Navigate to the Stores > WebContent > ConsumerDirect_name directory.
- Select the index.jsp file; then click Run > Run on Server. The store home page displays in the Web browser.
- A message might display asking you to select a server. Select Choose an existing server and click Finish.
- In the Web browser enter the following URL:
http://localhost/webapp/wcs/stores/servlet/MyNewViewThe new JSP page displays as shown in the following screen capture: