如果所有这些动作都有相同的业务,那么更好的设计是让每个动作都扩展 MobileCheck 动作。
修改 MobileCheck Action 以在一个public String mobileView()
方法中做业务(而不是执行);
修改 Actions A,B,C,D 以扩展它 ( public class ActionA extends MobileCheck
), 他们的业务在execute()
;
最后,使mobileView()
方法返回execute()
:它将返回execute()
您正在运行的 Action 的方法:
public class MobileCheck extends ActionSupport {
public String execute() throws Exception{
log.debug("MobileCheck execute() should never be invoked");
return SUCCESS;
}
public String mobileView() throws Exception{
// do something
log.debug("mobileView() business performed");
return execute(); /* THIS IS THE TRICK */
}
}
public class ActionA extends MobileCheck {
public String execute() throws Exception{
// do something
log.debug("ActionA execute() is invoked");
return SUCCESS;
}
}
public class ActionB extends MobileCheck {
public String execute() throws Exception{
// do something
log.debug("ActionB execute() is invoked");
return SUCCESS;
}
}
一个.jsp
<s:form>
<s:submit method="mobileView" action="actionA"
value="call mobileView() and re-execute Action A" />
</s:form>
b.jsp
<s:form>
<s:submit method="mobileView" action="actionB"
value="call mobileView() and re-execute Action B" />
</s:form>
struts.xml
<action name="actionA" class="foo.bar.ActionA">
<result>a.jsp</result>
</action>
<action name="actionB" class="foo.bar.ActionB">
<result>b.jsp</result>
</action>
KISS 范式 For The Win