Domain Name (DDD 领域模型驱动)
Pattern
Framework
UI(Presentation) Layer -- Business Layer -- Persistence Layer
Structs Spring Hibernate
面向请求驱动(Structs1.x Structs 2.x Webwork)
面向事件驱动 (JSF)
Action, ActionForm, ActionMapping
JSP页面 --> ActionServlet (doPost ) --> RequestProcessor.process() 截取url (/login)
--> ProcessMapping --> findActionMapping (struts-config.xml)--> FormBean (ActionFrom)
--> 收集表单数据 (processPopulate) --> processValidate --> processActionPerform
--> processForwardConfig
动态验证框架
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="patchnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml/>
</plug-in>
ActionForm
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("----------LoginActionForm.reset()-----------");
}
收集数据前重置数据
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("----------LoginActionForm.validate()-----------");
return null;
}
服务器端验证(表单验证,原则上不做数据库的验证)
可以在struts-config.xml 里面设置是否执行。默认validate="true"
<body> <% LoginActionForm laf = (LoginActionForm)request.getAttribute("loginForm"); %> <%=laf.getUsername() %>,登录成功 </body>
动态ActionForm (避免actionform过多)
org.apache.struts.action.DynaActionForm
<form-beans> <form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"/> <form-property name="age" type="java.lang.Integer"/> </form-bean>
ActionForm_Struts上传
org.apache.struts.upload.FormFile
public class UploadTestAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UploadActionForm uaf = (UploadActionForm)form; System.out.println("title" + uaf.getTitle()); FormFile myFile = uaf.getMyfile(); if (myFile != null) { System.out.println("fileName=" + myFile.getFileName()); FileOutputStream fos = new FileOutputStream("c:\\" + myFile.getFileName()); fos.write(myFile.getFileData()); fos.flush(); fos.close(); } return mapping.findForward("success"); } }
<controller maxFileSize="10M"/> 可配置各种上传文件的限制
空字段问题
UI不存在这个字段, JS和EL效果不一样,JS为null,EL为“”
UI存在这个字段,JS和EL效果一样
类型转换器
实现接口Convert接口,需注册,在servlet启动时
public class UtilDateConverterInitWithServlet extends HttpServlet { @Override public void init() throws ServletException { System.out.println("UtilDateConverterInitWithServlet.init()"); ConvertUtils.register(new UtilDateConverter(), Date.class); } }
Plugin注册
<plug-in className="com.bjsxt.struts.UtilDateConverterInitWithPlugin"/>
public class UtilDateConverterInitWithPlugin implements PlugIn { public void destroy() { } public void init(ActionServlet servlet, ModuleConfig config) throws ServletException { System.out.println("UtilDateConverterInitWithPlugin.init()"); ConvertUtils.register(new UtilDateConverter(), Date.class); } }
public class UtilDateConverter implements Converter { public Object convert(Class type, Object value) { System.out.println("UtilDateConverter.value=" + value); if (value == null) { return value; } if (value instanceof Date) { return value; } Date d = null; if (value instanceof String) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { d = sdf.parse((String)value); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return d; } }
DTO 数据传输对象
VO 值对象
ActionForm就是一个DTO对象
ActionForward
重定向 <forward name="login" path="/login.jsp" redirect="true"/>
全局ActionForward
<global-forwards>
<forward name="login" path="/login.jsp" redirect="true"/>
</global-forwards>
ActionForward af = mapping.findForward("login");
af.setRedirect(false);
return af;
public class MustLoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { if (request.getSession().getAttribute("user") == null) { ActionForward af = mapping.findForward("login"); //af.setRedirect(false); return af; //response.sendRedirect(request.getContextPath() + "/login.jsp"); //return null; } return mapping.findForward("success"); } }
动态ActionForward
ActionForward af = new ActionForward();
af.setPath("/page" + page + ".jsp?name=Tom");
return af;
自己写actionForward className属性
ActionMapping
对应配置信息,一个标签对应一个instance
validate 校验错误到input
parameter
unknowActionMapping
forward属性,空action,从do过去
<action path="/login1" forward="/login.jsp" ></action>
<action path="/testunknown"
unknown="true"
forward="/testunknown.jsp"
></action>
针对do请求
和type互斥,配了以后就不执行action属性定义的类了
Struts 国际化
request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
或者
this.setLocale 在Action里面
动态国际化文本
创建国际化消息文本
ActionMessage message = new ActionMessage("user.login.success",username);
传递国际化消息文本
this.saveMessage(request,messages); 普通消息
this.saveErrors 错误消息
通过<html:messages>标签(可以显示普通和错误消息)
通过<html:errors>只能显示错误消息