1. 新建task command接口和实现:
(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.java的performExecute方法进行实现
/// 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 )