📄 struts.txt
字号:
validation 框架 表单校验
tiles 框架 页面分割
///MVC
交互层(VC)--处理层(M)
交互层
接受请求 转换数据格式, 将请求委托给处理层 对客户端作出响应
处理层
处理请求(业务逻辑) 访问数据库 整合企业信息系统(EIS)EAI企业应用整合
为什么分层:
复用 区分职责: 解耦合业务逻辑和表现 代码集中
提供一个公共的地方加工请求和响应
M:
封装数据(entity) 行为(biz,service),不依赖于表现(可以单独测试)
V:(可以知道entity,但不能知道biz)
显示M的执行结果
C:
根据用户的语言,地区,访问级别,预处理
///basic
解耦 加快开发速度
focus on controller
配置:路径--action类--下一个页面
web.xml
AcitonServlet *.do init-param(config,/WEB-INF/aaa.xml) load-on-startup
struts-config.xml:
<struts-config>
<action-mappings>
<action path= type=
<forward name= path= redirect="true" 默认为false
Action 为单例
ActionMapping mapping
forward=mapping.findForward("")
////ActionForm
缺省保存在session中 (ActionForm属于M层) 封装请求
<form-bean name= type=>
格式校验:
<action name= scope="" (request/session 默认session) validate="false" 不校验(缺省为true)
input= 校验出错转到页面
<messag-resources parameter="com.csg.struts.core.MessageResources"/>
new ActionMessage("error.username") MessageResources.properties的key--value
errors.add("username",message) Map+List, 可以用property指定,否则迭代
form.validate() 返回null,或ActionErrors的元素为空时,通过校验
<%@taglib uri="http://struts.apache.org/tags-html” prefix="html"%>
<html:errors property=/>
////struts执行流程:
client-->ActionServlet
find or create an ActionForm instance
invoke reset
poplulate param
invoke validate
fails-->forward to input page-->end
find or create an Action instance
invoke execute.....
throw exception-->whether specified exception config-->end
get ActionForward from action
forward or redirect to next page/action
end
///actions(D2)
DispatchAction MappingDispatchAction LookupDispatchAction
不能覆盖perform execute 方法
方法特征与execute一致
////DispatchAction
parameter="method" 一般只写一个action标签(都不需要或需要校验),使用同样的form
页面需要提交额外的参数
有同名按钮时:
<input type="submit" name="method" value=""> 这种写法不推荐
<input type="hidden" name="method" value=""> 可以使用javascript
<input type="submit" value="ok" onclick=" 'this.form.method.value='print' " >
MappingDispatchAction
parameter="login" 适合于多个action标签分开写(比较重要的功能),可以使用不同的form
Lookup
配置与DispatchAction相同
用于同名按钮
<input type="submit" name="method"
value="<bean:message key="">"/> key--按钮value的映射
getKeyMethodMap() key--action方法的映射
///补充
创建公共类,复用相同代码
Action中尽量不使用成员,静态变量(被覆盖,线程不安全), 线程安全或只读时,可以使用
userService无状态可以是成员变量,user有状态不可以(会被覆盖)
////DynaActionForm
一般不使用
<form-beans>
<form-bean name= type="...DynaActionForm">
<form-property name="name" type="java.lang.String"/>
<form-property name="phone" type=" " />
dynaForm.get("name")
////ForwardAction
<action path= type="org.apache.struts.actions.ForwardAction" 常用
parameter="next.jsp"
////ActionForm 包装数据
User user=new User();
setUser()
getUser()
name="user.userName" "user.password" 可嵌套,setUser()并不是必须的
form粗粒度 实体--细粒度 表--粗粒度
form要及时从session中remove ,不要保存大量数据
///transaction tokens(D3)
isTokenValid(request,true)
saveToken()
<html:form action="/token/login"> 用于表单,必须要有actionForm
<html:link action="/token/login.do?...." transaction="true">
///异常处理(C6)
ActionErrors
property=""/>
new ActionMessage("key") MessageResource.properties
addErrors(request,errors) 不太好用
<action ...>
<exception key= path=页面 type=异常类 /> 声明的异常处理方式
<global-exceptions>
<exception key= path= type="java.lang.Exception" />
<global-forward> 最常转到的页面
<forward name="" login list message
errors.add("username",new ActionMessage("key")) ; 对异常的分类
///struts 标签库(C4)
bean logic--JSTL
////HTML
<html:errors/>
<html:messages id="msg"> 一样用于错误显示,迭代,更灵活
${msg}
</html:message>
<html:password property=" " redisplay="false">
<html:form action=/><html:link action= transaction="true"/> token
<html:radio property= value= >
<html:select property="student.degree">
<html:optionsCollection property="degrees">
Collection add(new OptionBeans(label,value))
<html:multibox property= value=> 数组
覆盖reset() form在会话范围时,清空之前的请求的遗留数据,每次请求都会调用这个方法
校验:
<html:javascript formName="loginForm"/> 利用validator自动生成js
<html:form 或 <form name="loginForm"
onclick="return validateLoginForm(this.form)"
////bean
bean:message
id= 声明变量 name= 查找引用 property= 可选
define--set write--out ${}
resource--定位文件
message 常用于国际化,LookupDispatchAction
header request header不常用
cookie parameter include
size--fn:length
////logic
present notPresent equal
iterate
///tiles框架
V 的复用:include page页面复用,css样式复用
layout布局复用--tiles(基于jsp, 页面的别名),将内容页面组合到布局页面中
需求:统一布局(header footer menu body),容易维护修改,布局内容分离
布局页面:
<tiles:getAsString name="title"/>
<tiels:insert attribute="header" />
组装页面:
<tiles:insert page="layout.jsp">
<tiles:put name="header" value="/common/header.jsp"/>
tiles-def.xml:
<tiles-definitions>
<definition name="base" path="xxx.layout">
<put name= value=>
<definition name="desktop" extends="base">
struts:
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" /> defs.xml可以有多个,用逗号分割
<forward name= path="desktop"/>
///validate 框架
继承ValidatorForm或ValidatorActionForm或DynaValidatorForm
struts-config:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
valiation.xml
<formset>
<form name=>
<field property="user.userName"
depends=""可以有多个,逗号分隔>
<arg0 key=${var:length}参数值 name= resource="false">
<var>
<var-name>
<var-value>
也可以在客户端校验: <html:javascript />
///项目:
用户管理系统
省市级联
eval()
hibernate:example
<property name="format_sql">true
<generator class="sequence'>
<param name="sequence">student_seq
type="string"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -