用通配符的方法配置action
第一步
创建一个action 在此action中不用实现或者继承(extents)任何类
在此action中编写所需要的方法,并提供必要返回值(这里并不一定要有返回值才行,也可以返回为空none 会自动调用struct里面的NONE)有传值时提供set get方法
public class LabelAction {
private String price;
private String msg;
public String propertyLabel(){
System.out.println(price);
if("4.5".equals(price) || ("4.5元").equals(price)){
msg="你是位贴心的男友,吃个墨西哥卷肯定不会让女朋友 出一分钱,看好你呦!";
}else if("31.5".equals(price) || ("31.5元").equals(price)){
msg="一个鸡肉卷这么贵,还有必要吃吗,败家!";
}else
msg="少壮不努力,老大徒伤悲,这算数是咋算的";
return "propertyLabel";
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
第二步
配置structs.xml
主要配置:
<action name="labelAction_*" class="com.chinasoft.ssh.LabelAction"
method="{1}">
<result name="{1}">{1}.jsp</result>
</action>
说明:①action中的name用统配符的方式匹配(为了规范和避免混淆 这个名字与action的类名相同然后加上_*)*号就是propertyLabel方法(即提交过来的方法)
②method中{1};
③result 中的name {1};这里的{1},就是从容器中获取到的具体方法
第三步
写jsp页面 :
进入action,eg:表单提交方式:
<form action="labelAction_propertyLabel.action" method="post">
<div class="bg">
<p>
有一个墨西哥鸡肉卷,你的女朋友吃了3/7,你吃掉了剩下的
4/7,你比你的女朋友多出了4.5元钱,
请问这个墨西哥鸡肉卷的价格是多少钱?
</p>
<p class="as">
<input class="price" type="text" name="price">
</p>
<p><input type="submit" value="提交"></p>
</div>
</form>
展示从action中获取的值两种展示方法:一,el表达式;二,导入<%@taglib prefix="s" uri="/struts-tags"%>,实现:<s:property value="msg"/>
执行过程: