WCS学习笔记(第五天) - Controller实现参数分析与验证validateParameters()

本文详细介绍了如何在WebSphere Commerce Server中构建并实现控制器接口,包括参数验证、数据绑定以及与JSP页面的交互。通过增加特定的Controller、Data Bean和调整JSP模板,实现了对输入参数的有效管理和展示。

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

1. 编写Controller接口和实现:
  a.WebSphereCommerceServerExtensionsLogic >src >com.ibm.commerce.sample.commands修改MyNewControllerCmd.java(增加参数接口方法),方法体主要对需验证参数的set、get方法

/// Section 2 ////////////////////////////////////////////////

// set interface methods
  
  
  public java.lang.Integer getPoints() ;
  
  public java.lang.String getUserName() ;

  public void setPoints(java.lang.Integer newPoints) ;

  public void setUserName(java.lang.String newUserName)  ;

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


 

  b. 修改WebSphereCommerceServerExtensionsLogic> src > com.ibm.commerce.sample.commands.MyNewControllerCmdImpl.java控制验证实现:

      实现接口set、get方法

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

/// create and implement controller command's fields and accessors 
/// (setter/getter methods)

  private java.lang.String userName = null;
  private java.lang.Integer points;
  
    
  public java.lang.Integer getPoints() {
    return points;
  }
  
  public java.lang.String getUserName() {
    return userName;
  }

  public void setPoints(java.lang.Integer newPoints) {
    points = newPoints;
  }

  public void setUserName(java.lang.String newUserName) {
    userName = newUserName;
  }

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


 

    实现validateParameters方法具体编程,用于对url参数input1和input2进行验证或赋值,代码实现如下:

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

/// uncomment to check parameters
    
    final String strMethodName = "validateParameters";
    
  TypedProperty prop = getRequestProperties();

  /// retrieve required parameters 
  try { 
    setUserName(prop.getString("input1"));
    
  } catch (ParameterNotFoundException e) {
    /// the next exception uses _ERR_CMD_MISSING_PARAM ECMessage object 
     /// defined in ECMessage class
    throw new ECApplicationException(ECMessage._ERR_CMD_MISSING_PARAM, 
        this.getClass().getName(), strMethodName,
       ECMessageHelper.generateMsgParms(e.getParamName()));
  }

  /// retrieve optional Integer
  // set input2 = 0 if no input value 
  setPoints(prop.getInteger("input2",0));

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

 

 

2. 增加MyNewDataBean方法,用于jsp页面传值:
  a.WebSphereCommerceServerExtensionsLogic >src >com.ibm.commerce.sample.databeans修改MyNewDataBean.java(增加参数userName、points的set、get方法)

/// Section 2 ///////////////////////////////////////////

  private java.lang.String userName = null;
  private java.lang.Integer points;
  
  
  public String getUserName() {
    return userName;
  }
  
  public void setUserName(java.lang.String newUserName) {
    userName = newUserName;
  }
  
  
  public Integer getPoints() {
    return points;
  }
  
  public void setPoints(java.lang.Integer newPoints) {
    points = newPoints;
  }
  
  
/// End of Section 2 ////////////////////////////////////

 

  b. 修改WebSphereCommerceServerExtensionsLogic> src > com.ibm.commerce.sample.commands.MyNewControllerCmdImpl.java对JSP页面传值显示

/// Section 3C/////////////////////////////////////////

  // pass the input information to the databean
  mndb.setUserName(this.getUserName());
  mndb.setPoints(this.getPoints());

/// end of section 3C/////////////////////////////////////////

 

3. 修改JSP页面:

  a. 在MyNewJSPTemplate.jsp的SECTION 6中增加下面代码,用于显示UserName和Points传递的信息

<!-- SECTION 5 -->

<c:if test="${mndbInstance.calledByControllerCmd}">
   <fmt:message key="Example" bundle="${tutorial}" /> <br />
   <fmt:message key="CalledByControllerCmd" bundle="${tutorial}" /> 
      <br />
   <fmt:message key="CalledByWhichControllerCmd" bundle="${tutorial}" /> 
   <b><c:out value="${mndbInstance.callingCommandName}" /></b> <br /> 
      <br />
</c:if>

<!-- END OF SECTION 5 -->



4. 测试:

  a. 启动server

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

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

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值