Structs
来源:互联网 发布:新淘宝网怎样充话费 编辑:程序博客网 时间:2024/05/18 08:38
Model1 :
client - jsp - JavaBean - DB
Model2:
Client - Servlet(Controller) - JavaBean - DB - JSP - Client
Structs框架结构:
ActionServlet Action ActionForm
JSP
1.Model
ActionForm & JavaBean 组成
ActionForm 封装用户请求,被ActionServlet 发送给 Action
JavaBean封装底层数据访问业务逻辑。
3.Controller
ActionServlet 负责拦截所有HTTP Request,根据用户请求决定是否调用业务逻辑控制器,需要则将请求转发给Action处理,否则直接JSP页面。
Action 负责处理用户请求,但本身不具有处理能力,调用Model完成。
跳转的实现
Servlet extends HttpServlet
{
service(ServletRequest req,ServletResponse resp)
{
RequestDispatcher rd=req.getRequestDispatcher("/welcome.jsp");
rd.forward(req,resp);
}
}
对于分层更清晰的j2ee应用,Model分 业务逻辑层和DAO层
Struts示例:
所有控制器都有两部分组成 - 核心控制器与业务逻辑控制器。
核心控制器负责拦截请求,业务控制器负责处理请求。
核心控制器拦截所有用户请求:
<servlet>
<servlet-name>actionServlet</>
<servlet-class>org.apache.struts.action.ActionServlet</>
</servlet>
<servlet-mapping>
<servlet-name>actionServlet</>
<url-pattern>*.do</>
</>
核心控制器由系统提供,负责拦截用户请求。
业务控制器处理请求,继承Action
LoginAction extends Action
{
//ActionMapping[控制转发] , ActionForm[用户请求] , HttpServletRequest/Response
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse resp)
{
//跳转
mapping.findForward("welcome");
//mapping.findForward("input");
}
}
配置Action:
struts-config.xml
<struts-config>
<action-mapping>
//login.do - /login , ActionServlet 负责将login.do 前面部分转发给Action处理
// login.do -> /login -> lee.LoginAction
<action path="/login" type="lee.LoginAction">
<forward name="welcome" path="/web-inf/jsp/welcome.jsp"/>
<forward name="input" path="/login.jsp"/>
</>
</>
</>
ActionMapping.findForward("逻辑名") 避免硬编码
Forward:
全局Forward:
局部Forward:
Client 请求 -> ActionServlet(Struts-config.xml) -> 填充ActionFrom -> 转发到Action (调用ActionForm) -> 调用JavaBean
-> 回送ActionForward -> 转发Http请求 给 JSP -> 调用JavaBean -> Client
需要逻辑控制器处理的请求:
调用对应Action,ActionServlet将请求转发到Action,如果请求配置对应FormBean,则ActionServlet用请求参数填充ActionForm。
Struts核心:
Action,
ActionForm,
ActionForward
核心控制器ActionServlet
<servlet>
<servlet-name>
<servlet-class>
<load-on-startup>
//加载配置文件 , 默认web-inf / struts-config.xml
<init-param>
<param-name>config</>
<param-value>/web-inf/....</>
</>
</>
<servlet-mapping>
<servlet-name>
<url-pattern>
</>
多个配置文件
<param-name>config/wawa</>
<param-value>...</>
映射到wawa子路径下
配置ActionForm:
继承org.apache.struts.action.ActionForm
setter/getter
JSP表单域 名字要和 ActionForm参数相同
LoginForm extends ActionForm
{
get/setUserName
get/setPassword
}
配置:
必须在struts-config.xml:
<form-beans>
<form-bean name="loginFrom" type="lee.LoginFrom" />
</>
配置Action:
public class LoginAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest req,resp)
{
LoginForm loginForm=(LoginForm)form;
loginForm.getUserName();
mapping.findForward();
}
}
ActionServlet -> (Action:ActionForm)->Action
Action:
path: 去掉.do后的路径匹配 Action的path
name:与Action关联的ActionForm
type:Action实现类,负责处理用户请求的业务控制器
局部Forward: Action转发到逻辑名
<action-mapping>
<action path="/login" type="lee.LoginForm" name="loginForm">
<forward name="welcome" path="/1.jsp"/>
</>
</>
配置Forward:
<global-forwards>
<forward name="" path="" redirect=""/>
</>
Struts国际化 Internationalization: I18N
Java国际化:
资源文件,key-value对
java.util.ResourceBundle 资源包
java.util.Locale 语言环境
java.text.MessageFormat 信息格式化
资源文件命名形式:
baseName_language_country.properties
baseName_language.properties
baseName.properties
ex:
Resouce.properties
Resource_zh_CN.properties
msg=你好,{0},今天是{1}
Locale.getAvailableLocale 获取Locale数组
//使用国际化
currentLocale=new ( , );
RecourceBundle bundle=ResouceBundle.getBundle("Resource",currentLocale);
String msg=(String)bundle.getObject("msg");
MessageFormat mf=new ("")
mf.setLocale(currentLocale);
mf.applyPattern(msg);
Object[] param={"yeeku",now};
mf.format(msgParams)
Struts加载资源文件:
struts-config.xml
web-inf/classes 开始,资源文件必须在该路径或子路径下,子路径必须以包形式配置
//parameter 资源位置
<message-resources parameter="lee.messages"/>
使用bean标签显示国际化:
struts-bean.tld 复制到 web-inf下
web.xml增加标签库:
<taglib>
<taglib-uri>/tags/struts-bean</>
<taglib-location>/web-inf/struts-bean.tld</>
</>
jsp国际化:
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<bean:message key="login"/>
动态ActionForm:
配置动态ActionForm:
继承 org.apache.struts.action.DynaAtionForm
配置struts-config.xml:
<struts-config>
<form-beans>
<form-bean name="loginForm" type="org.apache.struts.actionDynaActionForm">
<form-property name type="java.lang.String"/>
</form-bean>
使用:
DynaActionForm loginForm=(DynaActionForm)form;
(String)loginForm.get("username");
Struts标签库:
3个标签库: html , bean , logic
配置标签:
web.xml
<welcome-file-list>
<webcome-file>index.jsp</>
</>
<taglib>
<tablib-uri>/web-inf/struts-bean.tld</>
<taglib-location>/web-inf/struts-bean.tld</>
</>
struts-bean.tld 国际化,输出Action属性值
struts-html.tld:
struts-logic.tld: 循环选择等流程控制
html标签库:
base/button,cancel,checkbox,error
file,form,frame,hidde,html,image,img,javascript,link
message,multibox,option,password,radio...
<%@ include file="taglibs.jsp"%>
<html:form action="action.do" onsubmit="">
<bean:message key="login"/>
<html:submit><bean:message key="login"/></>
bean标签库:
-cookie -
<bean:cookie id="sess" name="JSESSIONID" value=""/>
<bean:write name="sess" property="comment"/>
<jsp:getProperty name="" property=""/>
-define-
<bean:define id="test1" value="aaa"/>
<bean:define id="test2" name="test1"/>
/<jsp:useBean id="bean1" class="lee.LoginForm" scope="page"/>
<bean:define id="test3" name="bean1" property="password"/>
<bean:define id="bean2" name="bean1" scope="page" toScope="session"/>
<bean:write name="bean2" property="password" scope="session"/>
= ((LoginForm)session.getAttribute("bean2")).getPassword()
-header-
//java.util.Enumeration names=((HttpServletRequest)request).getHeaderNames();
<bean:header id="head" name="<%=name %>"/>
-parameter-
(request parameter)
<bean:parameter id="a" name="b"/>
<bean:write name="a"/>
-include-
<bean:include id="beanName" page="uri"/>
-logic-
[ parameter / name /cookie / header]
<logic:empty name="name" property=" " scope="scope">
内容
</logic:empty>
<logic:match parameter="name" value="" scope=""> //包含
<logic:redirect href="page.jsp"/>
<logic:redirect page="page.jsp"/>
<logic:redirect forward=""/> 全局Forward
<logic:forward name="">
-iterate-
<logic:iterate id="item" collection="<%=list1%>" indexId="index" offset="1" length="2">
<%=item%> <%=index %>
<bean:write name="" property="" scope="">
</>
name="" scope=""
数据校验:
commons-validator.jar
校验方式:
ActionForm 代码校验
Action 代码校验
commons-validator.jar校验
ActionForm校验:(不再推荐)
错误封装在 ActionError,组合成ActionErrors中
LoginForm extends ActionForm implements Serializable
{
//重写validate方法校验
public ActionErrors validate(ActionMapping mapping,HttpServletRequest req)
{
ActionErrors errors=new ();
errors.add("username",new ActionError("error.username","用户名")); //error.username 资源文件key,"用户名"对应参数
}
}
输出错误提示:
<html:erros/>
国际化:
<input type="button" value='<bean:message key="button.submit">'/>
回传后保存文本:
<html:text property="pwd" size/>
Action代码校验:
LoginAction extends Action
{
execute(...)
{
request.setAttribute("errors","nameNotNull");
mapping.findForward("input");
}
}
commons-validator.jar校验:
增加校验资源
利用ValidatorPlugIn加载校验资源
ActionForm使用ValidatorForm子类
ActionForm必须是ValidatorForm子类:
public class LoginForm extends ValidatorForm
{
//getter/setter
}
校验规则文件:
validator-rules.xml (struts/lib下 -> web-inf/)
validation.xml:
<form-validation>
<formset>
<form name="loginForm"> //struts-config中定义的ActionForm名字一致
<field property="username" depends="required,mask">
<arg key="loginForm.username" position="0"/>
<var>
<var-name>mask</>
<var -value>^[a-zA-Z]+$</>
</>
</>
---------------------------
<field property="rpass" depends="requried,validwhen">
<arg key="loginForm.rpass" position="0"/>
<arg key="loginForm.pass" position="1"/>
<msg name="validwhen" key="loginForm.valid"/>
<var>
<var-name>test</>
<var-value>((*this*==null)or("this"==pass))</>
</>
</>
---------------------------
<field property="mail" depends="required,email">
<arg key="loginForm.email" position="0"/>
</>
--------------------------
规则:
required
validwhen
minlength
maxlength
mask
byte
integer....
**************************** p188 *******************
异常框架:
Exception Handling
struts-config.xml
全局异常、局部异常
<exception key="" type="" scope="" path=""/>
异常出现则页面自动转向uri,该页输出对应keyname出错提示
<form-beans>
<form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username" type="java.lang.String"/>
</>
</>
<global-exception>
<exception key="test.exceptionA" type="lee.exception.ExceptionTestA" scope="request" path="/error.jsp"/>
</>
<action-mapping>
<action path="/processLogin" type="lee.LoginAction" name="loginForm" scope="request" validate="true" input="/login.jsp">
<exception kye="test.exceptionB" type="" path=""/>
<forward name="sucess" path=""/>
<forward name="failure" path=""/>
</action>
</>
常用Action:
DispatchAction
ForwardAction
IncludeAction
LookupDispatchAction
MappingDispatchAction (extends DispatchAction)
SwitchAction
DispatchAction:
LoginAction extends DispatchAction
{
ActionForward add(ActionMapping mapping,ActionForm form,req,resp)
{
}
//modify
...
}
配置struts-config.xml
<action path="/login" type="lee.LoginAction" name="loginForm" scope="request" validate="true"
input="/login.jsp" parameter="method">
<forward name="success" path="/welcome.jsp"/>
</>
parameter参数:根据参数调用对应方法,
MappingDispatchAction:
-modify
-add
配置
<action path="/add" type="lee.LoginAction" name="loginForm" scope="" validate="" input="" parameter="add">
<...
</>
LookupDispatchAction:??????????
<html:submit property="method">
<bean:
</>
<html:submit property="method">
</>
ForwardAction:
<action-mappings>
<action path="/welcome" type="org.apache.struts.actions.ForwardAction" parameter="/welcome.jsp"/>
</action-mapping>
IncludeAction:
<action path="/welcome" type="org.apache.struts.actions.IncludeAction" parameter="/welcome.jsp"/>
<jsp:include page="welcome.do"/>
SwitchAction:
扩展方法:
PlugIn接口
RequestProcessor
ActionServlet
PlugIn:
SessionFactoryLoaderPlugIn implements PlugIn
{
init(ActionServlet servlet,ModuleConfig confg)
{
}
destory()
{
}
}
<plug-in className="">
<set-property property="" value=""/>
</plug-in>
RequestProcessor:
(super)
ActionForm processActionForm
Action processActionCreate
boolean processPreprocess
boolean processValidate
<controller processClass=""/>
ActionServlet:
process(HttpServletRequest req,resp)
{
req.setCharaterEncoding("GBK");
super.process(req,resp);
}
<servlet>
<servlet-class>
<load-on-startup>1</>
</>
- structs
- structs
- structs
- Structs
- Structs
- Structs
- structs
- structs
- Structs
- [structs]用户指南!
- Structs examples
- 8.8 Structs
- 18. Structs
- structs articles
- Structs入门
- structs读书笔记
- structs详解
- 管理structs
- sqlserver的导出导入和备份还原操作
- 知易行难?!你让执行力情以何堪!
- 创建消息时&作为符号显示的方法
- Symmetric Tree
- 树莓派上手实战之把Raspberry Pi 配置成无线路由器
- Structs
- 帧率、码流与分辨率
- android 发送状态栏通知
- 值得学习的网站
- C++ 类的设计规则
- linux 线程 pthread_create 源码 剖析
- 搜集的一些git的文章
- Partition取值
- include()和require()的区别 总结