1. 编写Controller接口和实现:
(增加参数接口方法),方法体主要对需验证参数的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页面传值:
(增加参数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 )