Modifying MyNewTaskCmd
In this lesson, you modify MyNewTaskCmd to determine
if the user name included in the URL is a registered user. You learn
how to modify MyNewDataBean to handle fields from the task command,
for example, the user name. Correspondingly, you modify MyNewJSPTemplate
to display whether the user is registered or not.
About this task
In this section of the tutorial, you learn how to use URL parameters and access current WebSphere Commerce information from within your own customized code.
Modifying the new task command for user name validation
Modify the new task command to validate that the user name passed in through the URL is that of a registered user:
Procedure
- In the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.commands.
- Open the MyNewTaskCmd.java file for editing
- Locate and uncomment Section 3 to add the following code
into the interface:
/// Section 3 //////////////////////////////////////////////// public void setFoundUserId(java.lang.String inputUserId); public java.lang.String getFoundUserId(); public void setUserRegistryAccessBean(UserRegistryAccessBean rrb); /// End of section 3//////////////////////////////////////////
- Save your changes.
- Open the MyNewTaskCmdImpl.java file for editing.
- Locate and uncomment Import section 1 to add the following
code to the class:
/// Import section 1 /////////////////////////////////////// import com.ibm.commerce.user.objects.UserRegistryAccessBean /// End of Import section 1 ///////////////////////////////
- Locate and uncomment sections 2A and 2B to add the following
code. This code create the fields and getter and setter methods that
correspond to the methods added into the interface:
//// Section 2A ////////////////////////////////////////////// private java.lang.String foundUserId = null; private UserRegistryAccessBean rrb = null; ////End of Section 2A ///////////////////////////////////////// //// Section 2B ////////////////////////////////////////////// public void setUserRegistryAccessBean(UserRegistryAccessBean newRRB) { rrb = newRRB; } public void setFoundUserId(java.lang.String newFoundUserId) { foundUserId = newFoundUserId; } public java.lang.String getFoundUserId() { return foundUserId; } /// End of section 2B ///////////////////////////////////////////
- In the Outline view, select the validateParameters method.
- Locate and uncomment section 1, to introduce the following
code into the method:
// section 1 /////////////////////////////////////////////////// // use UserRegistryAccessBean to check user Id try { if (rrb!=null){ setFoundUserId(rrb.getUserId()); } else { rrb =new UserRegistryAccessBean(); rrb=rrb.findByUserLogonId(getInputUserName()); setFoundUserId(rrb.getUserId()); } } catch (javax.ejb.FinderException e) { return; } catch (java.rmi.RemoteException e) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, this.getClass().getName(), "validateParameters"); } catch (javax.naming.NamingException e) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, this.getClass().getName(), "validateParameters"); } catch (javax.ejb.CreateException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, this.getClass().getName(), "validateParameters"); } // end of section 1 ///////////////////////////////////////////////
- Save your changes.
- Modifying the MyNewControllerCmdImpl file to create a reference
which creates an object in the task command. This modification creates
a UserRegistryAccessBean object instance variable. This object is
also available to the task command. By taking this approach, the task
command does not need to create a separate instance of the object.
Later in this step, you use the UserRegistryAccessBean objects in
the task command to determine if the user name is that of a registered
user
- In the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsLogic > src > com.ibm.commerce.sample.commands.
- Open the MyNewControllerCmdImpl.java file for editing
- Locate and uncomment section 2 to add the following
code to the class:
/// Section 2 //////////////////////////////////////////////// /// create a user registry accessbean resource instance variable private UserRegistryAccessBean rrb = null; /// End of Section 2 /////////////////////////////////////////
- In the Outline view, select the performExecute method.
- Locate and uncomment sections 4D and 4F to add the following
code. This code passes the instance variable to the task command and
then make the returned user ID available in the response properties
object.
// Section 4D //////////////////////////////////// /// pass rrb instance variable to the task command cmd.setUserRegistryAccessBean(rrb); // End of section 4D ///////////////////////////// // Section 4F /////////////////////////////////////// ///using access bean to get information from database if (cmd.getFoundUserId() != null) { rspProp.put("taskOutputUserId", cmd.getFoundUserId()); } // End of section 4F /////////////////////////////
- Right-click the MyNewControllerCmdImpl.java file; select Source > Organize Imports. The remaining two compilation errors are now resolved.
- Save your changes.
- Modify the MyNewJSPTemplate.jsp file to display the user
name validation information:
- In the Enterprise Explorer view, expand Stores > WebContent > Madisons.
- Open the MyNewJSPTemplate_All.jsp and MyNewJSPTemplate.jsp files for editing
- In the MyNewJSPTemplate_All.jsp file, locate and copy the code from section 8.
- In the MyNewJSPTemplate.jsp file,
paste the copied code into section 8. The following code is added
to the file:
<!-- SECTION 8 --> <c:if test="${!empty taskOutputUserId}"> <fmt:message key="UserId" bundle="${tutorial}" /> <c:out value="${taskOutputUserId}"/> <br /> <fmt:message key="FirstInput" bundle="${tutorial}" /> <b><c:out value="${userName}"/></b> <fmt:message key="RegisteredUser" bundle="${tutorial}" /> <br /> <fmt:message key="ReferenceNumber" bundle="${tutorial}" /> <b><c:out value="${taskOutputUserId}"/></b> <br /> <br /> </c:if> <c:if test="${empty taskOutputUserId}"> <fmt:message key="FirstInput" bundle="${tutorial}" /> <b><c:out value="${userName}"/></b> <fmt:message key="NotRegisteredUser" bundle="${tutorial}" /> <br /> </c:if> <!-- END OF SECTION 8 -->
- Save your changes.
- Test the user name validation
- Start or restart the WebSphere Commerce test server
- In the Enterprise Explorer view, expand Stores > WebContent > Madisons.
- Right-click the index.jsp file;
click Run As > Run on
Server. Your storefront page displays in
the web browser.Note: If prompted to select a server, select Choose an existing server and click Finish.
- Click Sign in and login to your store. If you do not have a store login Id and password, create a new user.
- In the web browser, enter the following URL where new_logon_Id is
a valid user login Id:
http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=new_logon_Id&input2=1000
Note: If global security (LDAP) is enabled, instead of the logon_id, you must use the name registered in the LDAP server. This name must be properly URL encoded. For example, if the name registered to the LDAP server isuid=myName,cn=users,dc=ibm,dc=com
, the URL encoded LDAP string is:uid%3DmyName%2Ccn%3Dusers%2Cdc%3Dibm%2Cdc%3Dcom
.The MyNewJSPTemplate displays in the web browser: