struts21笔记.docx
- 文档编号:10135639
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:23
- 大小:645.55KB
struts21笔记.docx
《struts21笔记.docx》由会员分享,可在线阅读,更多相关《struts21笔记.docx(23页珍藏版)》请在冰豆网上搜索。
struts21笔记
1、
2、namespace决定了action的访问路径,默认为””,可以接收所有路径的action
Namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action,/xxx/index.action,或者/xxx/yyy/index.action.
Namespace最好也用模块来进行命名。
3、
4、具体视图的返回可以由用户自己定义的Action来决定;具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容;具体的Action的实现可以是一个普通的java类,里面有publicStringexecute方法即可,或者实现Action接口;不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法。
5、struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
Index.jsp虽然可以使用redirect方式解决,但redirect方式并非必要。
解决办法非常简单,统一使用绝对路径。
(在jsp中用request.getContextRoot方式来拿到webapp的路径)或者使用myeclipse经常用的,指定basePath.
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+”:
//”+request.getServerName()+”:
”+request.getServerPort()+path+”/”;
6、Action执行的时候不一定要执行execute方法,可以在配置文件中配置Action的时候用method=””来指定执行那个方法,也可以在url地址中动态指定(动态方法调用DMI)(推荐);这两种中,前者会产生太多的action,所以不推荐使用。
6、使用通配符,将配置量降到最低,不过一定要遵守“约定优于配置”的原则。
---------------------------------------------------------------------------------------------------------------------------------------------
7、使用action属性接收参数:
8、使用DomainModel接收参数:
遇到比如要密码确认的,没有该属性,第一种方法可以直接用属性接收;还可以用vo(有人叫do或dto)(veryobject)。
dto(数据传输对象)
9、使用ModelDriven接收参数:
10、1.6版本的中文问题:
11、使用addFieldError方法和s:
fieldError标签简单处理数据校验:
12、ServletContext的引用:
取得Map类型的request、session、application、HttpServletRequest、HttpServletSession、ServletContext的引用
1、前三者:
依赖于容器;2、前三者:
IOC;3、后三者:
依赖于容器;4、后三者:
IOC;
第二种action(只用这种)
Action第三种:
Action第四种:
13、默认Action
14、Result配置:
Result类型:
1.dispatcher;2.redirect;3.chain;4.redirectAction;5.freemarker;6.httpheader;7.stream;8.velocity;9.xslt;10.plaintext;11.tiles.
15、GlobalResult和extends:
16、动态结果集:
动态结果一定不要忘了为动态结果的保存值设置set、get方法
17、向结果传参数:
18、OGNL
1、ObjectGraphNavigationLanguage
2、想要初始化domainmodel,可以自己new,也可以传参数值,但这样需要保持参数为空的构造函数。
1、访问值栈中的action的普通属性:
username=u
2、访问值栈中对象的普通属性(getset方法):
|||wrong:
username= propertyvalue="username"/>
propertyvalue="user.age"/>| propertyvalue="user['age']"/>| propertyvalue="user[\"age\"]"/>|wrong: <%-- propertyvalue="user[age]"/>--%>
propertyvalue="cat.friend.name"/>
propertyvalue="m()"/>
propertyvalue="password.length()"/>
propertyvalue="cat.miaomiao()"/>
propertyvalue="@com.bjsxt.struts2.ognl.S@s()"/>
propertyvalue="@com.bjsxt.struts2.ognl.S@STR"/>
propertyvalue="@@max(2,3)"/>
propertyvalue="newcom.bjsxt.struts2.ognl.User(8)"/>
propertyvalue="users"/>
propertyvalue="users[1]"/>
propertyvalue="users.{age}"/>
propertyvalue="users.{age}[0]"/>| propertyvalue="users[0].age"/>
propertyvalue="dogs"/>
propertyvalue="dogs[1]"/>
propertyvalue="dogMap"/>
propertyvalue="dogMap.dog101"/>| propertyvalue="dogMap['dog101']"/>| propertyvalue="dogMap[\"dog101\"]"/>
propertyvalue="dogMap.keys"/>
propertyvalue="dogMap.values"/>
propertyvalue="dogMap.size()"/>| propertyvalue="users.size"/>
propertyvalue="users.{? #this.age==1}[0]"/>
propertyvalue="users.{^#this.age>1}.{age}"/>
propertyvalue="users.{$#this.age>1}.{age}"/>
propertyvalue="users.{$#this.age>1}.{age}==null"/>
propertyvalue="[1]"/>
Orgl.xml
publicStringgetUsername(){
returnusername;
}
publicList
returnusers;
}
publicStringm(){
return"hello";
}
publicvoidsetCat(Catcat){
this.cat=cat;
}
publicvoidsetDogMap(Map
this.dogMap=dogMap;
}
publicvoidsetDogs(Set
this.dogs=dogs;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicvoidsetUser(Useruser){
this.user=user;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicvoidsetUsers(List
this.users=users;
}
}
publicclassOgnlActionextendsActionSupport{
privateCatcat;
privateMap
privateSet
privateStringpassword;
privateUseruser;
privateStringusername;
privateList
publicOgnlAction(){
users.add(newUser
(1));
users.add(newUser
(2));
users.add(newUser(3));
dogs.add(newDog("dog1"));
dogs.add(newDog("dog2"));
dogs.add(newDog("dog3"));
dogMap.put("dog100",newDog("dog100"));
dogMap.put("dog101",newDog("dog101"));
dogMap.put("dog102",newDog("dog102"));
}
publicStringexecute(){
returnSUCCESS;
}
publicCatgetCat(){
returncat;
}
publicMap
returndogMap;
}
publicSet
returndogs;
}
publicStringgetPassword(){
returnpassword;
}
publicUsergetUser(){
returnuser;
}
19、Struts-Tags
1、通用标签:
a)property
b)set
i.默认为actionscope,会将值放入request和ActionContext中
ii.page,request,session,application
c)bean
d)include(对中文文件支持有问题,不建议使用,如需包含,改用jsp包含)
e)param
f)debug
2、控制标签
a)ifelseifelse
b)iteractor
i、collectionsmapenumerationiteratorarray
c)subset
3、UI标签
a)theme
i.simplexhtml(默认)cssxhtmlajax
4、AJAX标签
A)补充
5、$#%的区别
a)$用于i18n和struts配置文件
b)#取得ActionContext的值
c)%将原本的文本属性解析为ognl,对于本来就是ognl的属性不起作用
i.参考 property和 include> propertyvalue="username"/> propertyvalue="'username'"/> propertyvalue="admin"default="管理员"/> propertyvalue="'
'"escape="false"/>
setvar="adminName"value="username"/>
propertyvalue="#request.adminName"/>
propertyvalue="#adminName"/>
<%--
setname="adminPassword"value="password"scope="page"/>
<%=pageContext.getAttribute("adminPassword")%>
--%>
setvar="adminPassword"value="password"scope="session"/>
propertyvalue="#adminPassword"/>
propertyvalue="#session.adminPassword"/>
setname="myDog"value="newcom.bjsxt.struts2.ognl.Dog('oudy')">
set>
push:
pushvalue="#myDog"> propertyvalue="name"/> push>
propertyvalue="name"/>
beanname="com.bjsxt.struts2.tags.Dog"> paramname="name"value="'pp'"> param> propertyvalue="name"/> bean>
beanname="com.bjsxt.struts2.tags.Dog"var="myDog"> paramname="name"value="'oudy'"> param>设置默认值
bean>
拿出值:
propertyvalue="#myDog.name"/>
includevalue="/_include1.html">
include>
includevalue="/_include2.html">
include>
setvar="incPage"value="%{'/_include1.html'}"/> includevalue="%{#incPage}"> include>%强制转化为OGNL表达式
age= propertyvalue="#parameters.age[0]"/> setvar="age"value="#parameters.age[0]"/> iftest="#age<0">wrongage! if> elseiftest="#parameters.age[0]<20">tooyoung! elseif> else>yeah! else> iftest="#parameters.aaa==null">null if>
iteratorvalue="{1,2,3}"> property/>| iterator>
iteratorvalue="{'aaa','bbb','ccc'}"var="x"> propertyvalue="#x.toUpperCase()"/>| iterator>
iteratorvalue="{'aaa','bbb','ccc'}"status="status"> property/>| 遍历过的元素总数: propertyvalue="#status.count"/>| 遍历过的元素索引: propertyvalue="#status.index"/>| 当前是偶数? : propertyvalue="#status.even"/>| 当前是奇数? : propertyvalue="#status.odd"/>| 是第一个元素吗? : propertyvalue="#status.first"/>| 是最后一个元素吗? : propertyvalue="#status.last"/> iterator> iteratorvalue="#{1: 'a',2: 'b',3: 'c'}"> propertyvalue="key"/>| propertyvalue="value"/> iterator>
iteratorvalue="#{1: 'a',2: 'b',3: 'c'}"var="x"> propertyvalue="#x.key"/>| propertyvalue="#x.value"/> iterator>
fielderrorfieldName="fielderror.test"theme="simple">
fielderror>
Struts标签_ui:
Struts.xml中控制theme,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- struts21 笔记