1.引用头:
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
2.全局跳转与异常
2.1:
<global-forwards>
<forward name="loginsuccess" path="/index.jsp" />
<forward name="fail" path="/fail.jsp"></forward>
</global-forwards>
2.2首先在<global-exception>(全局异常)或<action>(局部异 常)元素中嵌 套<exception>元素,一个<exception>元素代表一种类别的异常,如下:
<exception type="type" handler="handler" path="path" key="key" bundle="bundle" scope="scope"/>
type:指定待处理的异常类。
handler:指定异常处理类,默认值为 org.apache.struts.action.ExceptionHandler。如果设置为用户 自定义的异常处理类,则该类必须继承ExceptionHandler。
path:指定转发路径。
key:指定错误消息的key,Struts框架将根据这个key到Resource Bundle中寻找匹配的消息文本。
bundle:指定Resource Bundle,如果没有设置此项,将使用默认的Resource Bundle。
scope:指定ActionMessages的存放范围,可选值包括request和session。默认值为 request。
在指定了所有的异常后,可以加一个<exception type="java.lang.Exception" .../>来捕获尚未指定的其它异常,并可以指定该key的值为"系统错误......"。
<global-exceptions>
<exception key="error.exception"
type="com.struts.action.ErrorCodeException"
handler="com.struts.action.ErrorCodeExceptionHandler"
path="/fail.jsp">
</exception>
</global-exceptions>
public class ErrorCodeException extends RuntimeException {
public ErrorCodeException(String errorCode) {
super(errorCode);
}
}
public class ErrorCodeExceptionHandler extends ExceptionHandler {
public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
if(!(ex instanceof ErrorCodeException)){
if (ex instanceof NullPointerException) {
throw new ErrorCodeException("请确认数据完整,否则联系管理员!");
}
return super.execute(ex, ae, mapping, formInstance, request, response);
}
ActionForward forward = null;
ActionMessage error = null;
String property = null;
// Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
ErrorCodeException appe = (ErrorCodeException) ex;
// ae.getKey() 没用
// appe.getMessage() ("用户不能找到【" + username + "】
error = new ActionMessage(ae.getKey(), appe.getMessage());
property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
return forward;
}
}
3.action、actionform配置:
3.1---actionform分 ActionForm与 DynaValidatorForm细分带验证与非验证的
<form-bean type="com.struts.action.login.LoginForm" name="LoginForm" />
<form-bean name="upFileForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="file" type="org.apache.struts.upload.FormFile" />
<form-property name="userid" type="java.lang.String[]" />
</form-bean>
<form-bean name="addUserActionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="phone" type="java.lang.String" />
<form-property name="userid" type="java.lang.String" />
<form-property name="username" type="java.lang.String" />
</form-bean>
3.2--action分Action、DispatchAction、 LookupDispatchAction、MappingDispatchAction
LookupDispatchAction
通过这个Action抽象类继承DispatchAction,它的相应方法的执行由 ActionMapping中parameter属性决定。它适合在一个form中有很多按钮,按不同的按钮则执行不同的操作。
<action path="/test" type="org.example.MyAction" name="MyForm scope="request" input="/test.jsp" parameter="method"/>
ApplicationResources.properties:
button.add=Add Record
button.delete=Delete Record
JSP:
<html:form action="/test">
<html:submit property="method">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
在Action 中必须实现getKeyMethodMap:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.add", "add");
map.put("button.delete", "delete");
return map;
}
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do add
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do delete
return mapping.findForward("success");
}
MappingDispatchAction
public class MappingDispatchAction extends DispatchAction
它的相应方法的执行由 ActionMapping中parameter名决定,注意这里和LookupDispatchAction不同,LookupDispatchAction的 相应方法的执行由 ActionMapping中parameter属 性决定,
struts-config.xml:
<action path="/saveSubscription"
type="org.example.SubscriptionAction"
name="subscriptionForm"
scope="request"
input="/subscription.jsp"
parameter="method"/>
Action:
public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
for which you would create corresponding <action> configurations that reference this class:
<action path="/createSubscription"
type="org.example.SubscriptionAction"
parameter="create">
<forward name="success" path="/editSubscription.jsp"/>
</action>
<action path="/editSubscription"
type="org.example.SubscriptionAction"
parameter="edit">
<forward name="success" path="/editSubscription.jsp"/>
</action>
<action path="/saveSubscription"
type="org.example.SubscriptionAction"
parameter="save"
name="subscriptionForm"
validate="true"
input="/editSubscription.jsp"
scope="request">
<forward name="success" path="/savedSubscription.jsp"/>
</action>
<action path="/deleteSubscription"
type="org.example.SubscriptionAction"
name="subscriptionForm"
scope="request"
input="/subscription.jsp"
parameter="delete">
<forward name="success" path="/deletedSubscription.jsp"/>
</action>
<action path="/listSubscriptions"
type="org.example.SubscriptionAction"
parameter="list">
<forward name="success" path="/subscriptionList.jsp"/>
</action>
4.资源文件:
<message-resources parameter="com.sordine.meter.struts.ApplicationResources" />
5.验证插件:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>