###########################################
## Date: 2007-11-12
## Description: Struts FrameWork day3
## Author: yejf
############################################
[b]Struts中 模板模式 的应用: [/b]
如何来写一个公共的 ACTION:
public abstract class BaseAction extends Action {
//模板模式的应用.
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException {
executeBefore(); //做一些共同的功能
ActionForward forward = executeAction(mapping,form,req,res);
executeAfter();
return forward;
}
public void executeBefore() {
//实现共同的功能.
//1. 用户的登录
//2. 用户只能访问被授权的页面
//3. 跟踪每一次用户点击页面
//4. 可以用来显示登录过用户的姓名.
//以上有些功能可以被用来在FILTER中或LISTENER,进行实现.
}
//子类必须要实现的抽象方法.
public abstract ActionForward executeAction(ActionMapping mapping,ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException ;
public void executeAfter() {
//实现共同的功能
//1. 在ACTION执行完之后要做的其它事情.
}
protected CommonService getCommonService() {
// . . . 获取公共的业务对象
}
}
Struts高级特性:
一, 动态表单:
org.apache.struts.action.DynaActionForm
Struts提供了此类,我们只需要在struts-config.xml
文件当中来配置一个formbean, 而再也不需要去coding
一个ActionForm Bean
<!-- 配置动态表单 -->
<form-beans>
<form-bean name="dynyLoingForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="" type="" />
. . .
</form-bean>
</form-beans>
二, Prebuilt Action
Struts提供了多个 prebuilt Action,我们重点使用如下
三个:
org.apache.struts.actions.DispatchAction
org.apache.struts.actions.MappingDispatchAction
org.apache.struts.actions.LookupDispatchAction
1. DispatchAction:
public class DispatchAction extends Action {
protected HashMap methods = new HashMap();
protected Class[] types = null;
protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
String name)
throws Exception {
if(name == null) { return this.unspecified(mapping,form,request,response);
Method m = null;
try {
m = getMethod(name);
} catch (NoSuchMethodException nsme) {
//. . .
}
//依要求进行处理
//...
}
protected String getParameter(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
//已实现
}
protected Method getMethod(String name)
throws NoSuchMethodException {
// 已实现
}
protected ActionForward unspecified(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { //已实现 }
}
//例
public class ProductDispatchAction extends DispatchAction {
//如有需要,可以重写 dispatchMehod()方法
public ActionFoward add(mapping,form,req,res)throws Exception {
}
public ActionForward modify(mapping,form,req,res)throws Exception {
}
. ..
}
配置DispatchAction:
<action path="/product"
type="xx.xx.ProductDispatchAction"
paramemter="method">
</action>
<a href="/product.do?method=add">添加</a>
<from action="/product.do">
<input type="hidden" name="method" value="modify">
</form>
------------------------------------------------------
org.apache.struts.actions.DispatchAction
1.特点:
a. struts-config.xml 只与一个<action>,
所有与本DispatchAction相关的请求
都对应一个<action>
b. 只能使用一个form 表单 或者不使用.
c. 做表单验证时,失败都到同一页面.
d. 由于所有请求都对应同一 path, 所以
要用parameter="method" 来指定一个"代理方法名"
真正的方法名要通过前台JSP页面使用 reqUrl?method=real method name 来指定.
org.apache.struts.actions.LookupDispatchAction
1.特点:
a. 适合用于一个页面中存在多个提交按钮的情况.
b. 根据在资源文件中配置的key 来找到方法名.
c. 此KEY 值也是前台JSP页面中的提交按钮的值, 它的name
属性要与<action>中的 parameter="" 保持一样.
d. 如果按钮是中文时要注意,编码方式一至. 这样才可以.
org.apache.struts.actions.MappingDispatchAction
1.特点:
a. 每一个请求都对应一个<action>
b. 所以可以为每个请求指定 form 表单.
c. 用 parameter 属性直接指定 方法名
## Date: 2007-11-12
## Description: Struts FrameWork day3
## Author: yejf
############################################
[b]Struts中 模板模式 的应用: [/b]
如何来写一个公共的 ACTION:
public abstract class BaseAction extends Action {
//模板模式的应用.
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException {
executeBefore(); //做一些共同的功能
ActionForward forward = executeAction(mapping,form,req,res);
executeAfter();
return forward;
}
public void executeBefore() {
//实现共同的功能.
//1. 用户的登录
//2. 用户只能访问被授权的页面
//3. 跟踪每一次用户点击页面
//4. 可以用来显示登录过用户的姓名.
//以上有些功能可以被用来在FILTER中或LISTENER,进行实现.
}
//子类必须要实现的抽象方法.
public abstract ActionForward executeAction(ActionMapping mapping,ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException ;
public void executeAfter() {
//实现共同的功能
//1. 在ACTION执行完之后要做的其它事情.
}
protected CommonService getCommonService() {
// . . . 获取公共的业务对象
}
}
Struts高级特性:
一, 动态表单:
org.apache.struts.action.DynaActionForm
Struts提供了此类,我们只需要在struts-config.xml
文件当中来配置一个formbean, 而再也不需要去coding
一个ActionForm Bean
<!-- 配置动态表单 -->
<form-beans>
<form-bean name="dynyLoingForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="" type="" />
. . .
</form-bean>
</form-beans>
二, Prebuilt Action
Struts提供了多个 prebuilt Action,我们重点使用如下
三个:
org.apache.struts.actions.DispatchAction
org.apache.struts.actions.MappingDispatchAction
org.apache.struts.actions.LookupDispatchAction
1. DispatchAction:
public class DispatchAction extends Action {
protected HashMap methods = new HashMap();
protected Class[] types = null;
protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
String name)
throws Exception {
if(name == null) { return this.unspecified(mapping,form,request,response);
Method m = null;
try {
m = getMethod(name);
} catch (NoSuchMethodException nsme) {
//. . .
}
//依要求进行处理
//...
}
protected String getParameter(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
//已实现
}
protected Method getMethod(String name)
throws NoSuchMethodException {
// 已实现
}
protected ActionForward unspecified(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { //已实现 }
}
//例
public class ProductDispatchAction extends DispatchAction {
//如有需要,可以重写 dispatchMehod()方法
public ActionFoward add(mapping,form,req,res)throws Exception {
}
public ActionForward modify(mapping,form,req,res)throws Exception {
}
. ..
}
配置DispatchAction:
<action path="/product"
type="xx.xx.ProductDispatchAction"
paramemter="method">
</action>
<a href="/product.do?method=add">添加</a>
<from action="/product.do">
<input type="hidden" name="method" value="modify">
</form>
------------------------------------------------------
org.apache.struts.actions.DispatchAction
1.特点:
a. struts-config.xml 只与一个<action>,
所有与本DispatchAction相关的请求
都对应一个<action>
b. 只能使用一个form 表单 或者不使用.
c. 做表单验证时,失败都到同一页面.
d. 由于所有请求都对应同一 path, 所以
要用parameter="method" 来指定一个"代理方法名"
真正的方法名要通过前台JSP页面使用 reqUrl?method=real method name 来指定.
org.apache.struts.actions.LookupDispatchAction
1.特点:
a. 适合用于一个页面中存在多个提交按钮的情况.
b. 根据在资源文件中配置的key 来找到方法名.
c. 此KEY 值也是前台JSP页面中的提交按钮的值, 它的name
属性要与<action>中的 parameter="" 保持一样.
d. 如果按钮是中文时要注意,编码方式一至. 这样才可以.
org.apache.struts.actions.MappingDispatchAction
1.特点:
a. 每一个请求都对应一个<action>
b. 所以可以为每个请求指定 form 表单.
c. 用 parameter 属性直接指定 方法名