<html:form action="/DiaryAction.do">//action属性和配置文件中的path属性一致
<html:text property="Form中对象 。 实体类属性" />//property属性和FormBean中的值绑定
</html:form>
<html:form action="/DiaryAction.do">//action属性和配置文件中的path属性一致 <html:text property="Form中对象 。 实体类属性" />//property属性和FormBean中的值绑定 </html:form>
1.Struts的组件构成:控制器组件,模型组件,视图组件。
控制器组件:ActionServlet,是Struts的核心控制器,还包括Action类。
模型组件:不由Struts提供,一般由普通的接口和JavaBean充当。
视图组件:JSP页面,ActionForm和Struts标签。
2.如何快速的创建Struts框架?
(1.)创建Web Project
(2.)选中Web Project项目,点击编辑器的工具栏中的MyEclipse选项中的Project Cababilities选中Add Struts Cababilities 将Install Struts TLDs选项的勾去掉不让选中
(3.)然后创建我们的包结构
biz ——业务逻辑层
dao ——数据访问层
entity ——实体类
filter ——过滤器
web ——视图层
(4.)分别实现数据访问层、业务逻辑层的代码 和 form、action 并作出相应的配置
(5.)一个简单的struts就做好了
3.DispatchAction的运用
但是这种做法每个访问只对应一个action 所以我们选择使用DispatchAction来实现一组相关的几种业务操作。
(1.)
DispatchAction和创建Action基本相似,只是把execute方法改为自己定义的方法名就可以了,不能有execute方法在该类中,并且该类继承DispatchAction。
(2.)在struts-config.xml中配置和action一样只是多了一个 parameter属性 如:
<action path="/PetAction" type="net.lyqidao.epet.web.action.PetAction" parameter="operate" name="petForm">
<forward name="petInfo" path="/ch02/petInfo.jsp"></forward>
<forward name="fail" path="/ch03/index.jsp"></forward>
<forward name="listPet" path="/ch02/pet_list.jsp"></forward>
<forward name="modify_petInfo" path="/ch02/modify_petInfo.jsp"></forward>
</action>
(3.)在页面首先写一段JS代码
<script >
function doAdd()
{
document.forms[0].elements["operate"].value = "doAdd";
document.forms[0].submit();
}
</script>
然后在form表单中添加一个隐藏表单域表示操作类型
<input type="hidden" name="operate">
指明struts-config.xml配置中的parameter属性中的值 JS中的value值指定要调用Action中的哪一个方法,也可以直接使用地址的方式去访问:
<a href="PetAction.do?operate=toEdit&id=${item.id }">${item.name } || 编辑宠物资料</a> 宝贝!
4.Struts错误信息的国际化
(1.)英文错误信息:ApplicationResources.properties
errors.header = <span style="color:red;">
errors.footer = </span>
error.validate.login = login failure.
(2.)中午错误信息:ApplicationResources_zh_CN.properties
使用cmd运行指令将中文编译成unicode编码,步骤如下:
先将ApplicationResources_zh_CN.properties文件复制到java bin目录下
cmd——找到Java bin目录——native2ascii(nat 按tab会自动输入整个单词)—— -encoding gbk ApplicationResources.properties (applica tab自动提示)(含中文错误提示) —— 输入要转译到哪一个文件 中ApplicationResources_zh_CN.properties
然后将bin目录的ApplicationResources_zh_CN.properties文件拷贝到项目中
修改相应Action
ActionMessages errors = new ActionMessages();
errors.add("error",new ActionMessage("error.validate.login"));//error.validate.login为配置文件
//ApplicationResources_zh_CN.properties中的error.validate.login 属
<html:form action="/DiaryAction.do">//action属性和配置文件中的path属性一致
<html:text property="Form中对象 。 实体类属性" />//property属性和FormBean中的值绑定
</html:form>
性 super.saveErrors(request, errors);
页面显示错误信息
<html:errors property="error" /> 提示:需要引入html标签
5.使用实体对象作为Form属性简化开发
6.Struts标签库
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
(1.)html标签的使用:
<html:form action="/DiaryAction.do">//action属性和配置文件中的path属性一致
<html:text property="Form中对象 。 实体类属性" />//property属性和FormBean中的值绑定
</html:form>
(2.)待续
(3.)
(4.)