WCS学习笔记(第六天) - 创建新的任务命令

本文详细介绍了如何在WebSphere Commerce Server中创建、实现和调用新任务命令,包括接口定义、方法实现及控制器集成,最终展示任务执行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 新建task command接口和实现:
  a.WebSphereCommerceServerExtensionsLogic >src >com.ibm.commerce.sample.commands修改MyNewTaskCmd.java(java类可在IBM或前面教程获取)

     指定默认实现接口类

/// Section 1 ///////////////////////////////////////////////

// set default command implement class

  static final String defaultCommandClassName=
    "com.ibm.commerce.sample.commands.MyNewTaskCmdImpl";
    

/// End of section 1//////////////////////////////////////////

    接口类get、set方法

/// Section 2 ////////////////////////////////////////////////
// set interface methods

  public void setInputUserName(java.lang.String inputUserName);
  public void setInputPoints(Integer inputPoints);
  public void setGreetings(java.lang.String greeting);
  
  public java.lang.String getInputUserName();
  public java.lang.Integer getInputPoints();
  public java.lang.String getGreetings();
  

/// End of section 2//////////////////////////////////////////

 

  b. 新增并修改WebSphereCommerceServerExtensionsLogic> src > com.ibm.commerce.sample.commands.MyNewTaskCmdImpl.java新任务命令接口实现类:

      参数inputUserName、greetings、inputPoints的get、set方法: 

//// Section 1A //////////////////////////////////////////////

  private java.lang.String inputUserName;
  private java.lang.String greetings; 
  private java.lang.Integer inputPoints;
  

////End of Section 1A /////////////////////////////////////////

//// Section 1B //////////////////////////////////////////////  
  
  public void setInputUserName(java.lang.String newInputUserName) {
    inputUserName = newInputUserName;
  }
 
  public void setInputPoints(Integer newInputPoints) {
    inputPoints = newInputPoints;
  }
  
  public void setGreetings(java.lang.String newGreetings) {
    greetings = newGreetings;
  }
  
  public java.lang.String getInputUserName() {
    return inputUserName;
  }
  
  public Integer getInputPoints() {
    return inputPoints;
  }
  
  public java.lang.String getGreetings() {
    return greetings;
  }

  
////End of Section 1B /////////////////////////////////////////


      对MyNewTaskCmdImpl.javaperformExecute方法进行实现

/// Section 1 ////////////////////////////////////////////////

/// modify the greetings and see it in the NVP list

  setGreetings( "Hello " + getInputUserName() );
  
/// End of section 1 /////////////////////////////////////////

 

2. 增加MyNewControllerCmdImpl.java代码实现,用于引用新任务命令

/// Section 4A
    /// see how the controller command calls a task command

  MyNewTaskCmd cmd = null;

  try {

    cmd = (MyNewTaskCmd) CommandFactory.createCommand(
         "com.ibm.commerce.sample.commands.MyNewTaskCmd", getStoreId());

        // this is required for all commands
        cmd.setCommandContext(getCommandContext());

    /// set input parameters to task command
    cmd.setInputUserName(getUserName());
    cmd.setInputPoints(getPoints()); // change to Integer

/// End Section 4A ///////////////////////////////////////

/// Section 4B ///////////////////////////////////

    /// invoke the command's performExecute method
    cmd.execute();

    /// retrieve output parameter from task command, then put it to 
     /// response properties
    rspProp.put("taskOutputGreetings", cmd.getGreetings());

/// End Section 4B /////////////////////////////////////

/// Start Section 4C ////////////////////////////////////
  } catch (ECException ex) {
    /// throw the exception as is
    throw (ECException) ex;
  }

/// End Section 4C //////////////////////////////////////

 

3. 修改JSP页面:

  a. 在MyNewJSPTemplate.jsp的SECTION 7中增加下面代码,用于显示taskOutputGreetings传递的信息

<!-- SECTION 7 -->

<fmt:message key="Greeting" bundle="${tutorial}" />
<c:out value="${taskOutputGreetings}"/> <br /> <br />
  
<!-- END OF SECTION 7 -->



4. 测试:

  a. 启动server

  b. 访问: http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=wscadmin&input2=1000  (注:由于缓存可在url后加如: ?StoreId=10051 )

 

参考: http://pic.dhe.ibm.com/infocenter/wchelp/v7r0m0/topic/com.ibm.commerce.developer.tutorial.doc/tutorial/ttd16.htm  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值