import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.action;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import businesslayer;
/**
* sample struts action with pseudocode
* 使用伪代码的struts行为示例
*/
public class samplestrutsaction extends action{
/**
* standard struts doperfom method
* 标准的struts doperform方法
*/
public actionforward doperform(
actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws invalidentrypointexception {
//local variables
//本地变量
stockoffer useroffer =null;
//get any previous values from the session
//从session取得以前的数据
useroffer=(stockoffer)request.getsession()
.getattribute("previous_stock_offer");
//create this object if it is null
//如为null则创建新对象
if (null==useroffer){
useroffer = new stockoffer();
}
//update with the incoming values
//用上送的数据更新
//these values match those on the form
//这些数据是与form中的数据相对应的
useroffer.setstockname(request.
getparametervalue("stock_name"));
useroffer.setstockprice(request
.getparametervalue("stock_price"));
useroffer.setstockquantity(request
.getparametervalue("stock_qty"));
//reset the output value
//重置输出数据
useroffer.setrecommendpurchase(null);
//call the business layer
//调用商业层
businesslayer
.evaluatestockpurchase(useroffer);
//forward to the appropriate page
//转向合适的页面
if ("yes".equals(
testoffer.getrecommendpurchase()){
return mapping.findforward("yes_web_page");
}
//otherwise default to the no page
//否则指向无此页面
return mapping.findforward("no_web_page");
}
}
/**
* defines a data access object - a non data
* source specific way of obtaining data.
* 定义一个数据存取对象-一种非数据源获取数据的方法
*/
public interface stocknamedao {
/**
* get a list of stock names for the application
* @return string[] array of stock names
* 得到一个股票名字的列表
* 返回股票名称的string[]数组
*/
public string [] getstocknames();
/**
* check if our stock is on the list
* 检查股票是否在列表中
* @param stockname
* @return
*/
public boolean isonstocklist(string stockname);
}
and here's the daoimplementation:
这是daoimplementation:
/**
* concrete definition of a data access object
* 数据存取对象的具体定义
*/
public class daoimplementation
implements stocknamedao {
/**
* constructor with package level access only
* to encourage use of factory method
* 这里的构造器只是让你使用工厂(factory)方法
*/
daoimplementation(){}
/**
* get a list of stock names for the app.
* this is a hard coded sample
* normally we would get this from
* a database or other datasource.
* 得到一个股票名字的列表,这只是一个硬编码的例子,一般来
* 说我们应该从数据库或其它数据源取得数据
* @return string[] array of stock names
*/
public string[] getstocknames() {
string[] stocknames=
{"xyz","abc","megacorp","someothercompany"};
return stocknames;
}
/**
* check if our stock is on the list
* 检查我们的股票是否在列表中
* @param stockname
* @return true / false as appropriate
*/
public boolean isonstocklist(string stockname){
//get our list of stocks
//获取股票列表
string stocklist[] = getstocknames();
//loop and see if our stock is on it
// done this way for clarity . not speed!
//循环看股票是否存在,这样做是为了清晰不是速度!
for (int a=0; a<stocklist.length;a++){
if(stocklist[a].equals(stockname)){
return true;
}
}
//default return value
return false;
}
}
package.net.firstpartners.rp;
/**
* factory method to get the data access object.
* normally we could replace this with a
* framework like spring or hibernate
* 得到数据存取对象的工厂方法,通常我们可以将它替换为像spring或
* hibernatte这样的框架
*/
public class daofactory {
/**
* get the stock name dao
* this sample is hardcoded - in reality
* we would make this configurable / cache
* instances of the dao as appropriate
* 得到股票名字的dao,这个例子是硬编码的-实际上我们可以让它成为
* 可配的,缓存的合适的dao对象。
* @return an instance of stocknamedao
*/
public static stocknamedao getstockdao(){
return new daoimplementation();
}
}
<?xml version="1.0"?>
<rule-set name="businessrulessample"
xmlns="http://drools.org/rules"
xmlns:java="http://drools.org/semantics/java"
xmlns:xs="
http://www.w3.org/2001/xmlschema-instance"
xs:schemalocation="
http://drools.org/rules rules.xsd
http://drools.org/semantics/java java.xsd">
<!-- import the java objects that
we refer to in our rules -->
<!-- 导入规则中使用的对象 -->
<java:import>
java.lang.object
</java:import>
<java:import>
java.lang.string
</java:import>
<java:import>
 .net.firstpartners.rp.stockoffer
</java:import>
<java:import>
 .net.firstpartners.rp.daofactory
</java:import>
<java:import>
 .net.firstpartners.rp.stocknamedao
</java:import>
<!-- application data not associated -->
<!-- with any particular rule -->
<!-- in this case it's our factory -->
<!-- object which gives us back -->
<!-- a handle to whatever dao (data -->
<!-- access object) that we need -->
<!-- 没有和任何规则联系的应用数据,这里是我们的工厂对象,-->
<!—它向我们提供向后的操作,告诉我们什么dao 是我们需要的。-->
<application-data
identifier="daofactory">daofactory
</application-data>
<!-- a java (utility) function -->
<!-- 一个java方法 -->
<!-- we reference in our rules -->
<!-- 在我们的规则中打印跟踪信息 -->
<java:functions>
public void printstock(
 .net.firstpartners.rp.stockoffer stock)
{
system.out.println(
"name:"+stock.getstockname()
+" price: "+stock.getstockprice()
+" buy:"+stock.getrecommendpurchase());
}
</java:functions>
<!-- check for xyz corp-->
<!-- 检查xyz公司 -->
<rule name="xyzcorp" salience="-1">
<!-- parameters we can pass into-->
<!-- the business rule -->
<!-- 可以传入规则中的参数 -->
<parameter identifier="stockoffer">
<class>stockoffer</class>
</parameter">
<!-- conditions that must be met for -->
<!-- business rule to fire -->
<!-- 激活规则必须满足的条件 -->
<java:condition>
stockoffer.getstockname().equals("xyz")
</java:condition>
<java:condition>
stockoffer.getrecommendpurchase() == null
</java:condition>
<java:condition>
stockoffer.getstockprice() > 10
</java:condition>
<!-- what happens when the business -->
<!-- rule is activated -->
<!-- 规则激活后执行的步骤 -->
<java:consequence>
stockoffer.setrecommendpurchase(
stockoffer.no);
printstock(stockoffer);
</java:consequence>
</rule>
<!-- ensure that negative prices -->
<!-- are not accepted -->
<!-- 确定负数不被接受 -->
<rule name="stock price not negative">
<!-- parameters we can pass into the -->
<!-- business rule -->
<!-- 可以传入规则中的参数 -->
<parameter identifier="stockoffer">
<class>stockoffer</class>
</parameter>
<!-- conditions for rule to fire -->
<!-- 激活规则必须满足的条件 -->
<java:condition>
stockoffer.getstockprice() < 0
</java:condition>
<!--when rule is activated then ... -->
<!-- 规则激活后执行的步骤 -->
<java:consequence>
stockoffer.setrecommendpurchase
(stockoffer.no);
printstock(stockoffer);
</java:consequence>
</rule>
<!-- check for negative prices-->
<!-- 寻找低价 -->
<rule name="stock price low enough">
<!-- parameters for the rule -->
<!-- 可以传入规则中的参数 -->
<parameter identifier="stockoffer">
<class>stockoffer</class>
</parameter>
<!-- now uses dao to get stock list -->
<!-- 现在使用dao获取股票列表 -->
<java:condition>
daofactory.getstockdao().isonstocklist(
stockoffer.getstockname())
</java:condition>
<java:condition>
stockoffer.getrecommendpurchase() == null
</java:condition>
<java:condition>
stockoffer.getstockprice() < 100
</java:condition>
<!-- when rule is activated do this -->
<!-- 规则激活后执行的步骤 -->
<java:consequence>
stockoffer.setrecommendpurchase(
stockoffer.yes);
printstock(stockoffer);
</java:consequence>
</rule>
</rule-set>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器