struts2个人学习笔记.docx

上传人:b****4 文档编号:3733118 上传时间:2022-11-25 格式:DOCX 页数:20 大小:23.45KB
下载 相关 举报
struts2个人学习笔记.docx_第1页
第1页 / 共20页
struts2个人学习笔记.docx_第2页
第2页 / 共20页
struts2个人学习笔记.docx_第3页
第3页 / 共20页
struts2个人学习笔记.docx_第4页
第4页 / 共20页
struts2个人学习笔记.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

struts2个人学习笔记.docx

《struts2个人学习笔记.docx》由会员分享,可在线阅读,更多相关《struts2个人学习笔记.docx(20页珍藏版)》请在冰豆网上搜索。

struts2个人学习笔记.docx

struts2个人学习笔记

开发环境:

IDE:

myeclips6.0tomcat6.0

Struts.xml

只要配置拦截器默认拦截器defalutStack一定要加上

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEstrutsPUBLIC

"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

"http:

//struts.apache.org/dtds/struts-2.0.dtd">

/result.jsp

Struts2的标签库

<%@taglibprefix="s"uri="/struts-tags"%>

ActionSupport

的if("hello".equals(this.getUsername().trim())&&"".equals(this.getPassword().trim()))

尽量把常量写在前面,可以防止出现异常

验证

this.addFieldError("username"(要验证的字段),"reuqeried");

Struts2的类型转换

继承DefaultTypeConverter类(针对实体类)

publicclassPointConverterextendsDefaultTypeConverter{

publicObjectconvertValue(Mapcontext,Objectvalue,ClasstoType){

if(Point.class==toType)

{

Pointpoint=newPoint();

String[]str=(String[])value;

String[]paramValues=str[0].split(",");

intx=Integer.parseInt(paramValues[0]);

inty=Integer.parseInt(paramValues[1]);

point.setX(x);

point.setY(y);

returnpoint;

}

if(String.class==toType)

{

Pointpoint=(Point)value;

intx=point.getX();

inty=point.getY();

Stringresult="x="+x+"y="+y;

returnresult;

}

returnnull;

}

}

point-conversion.properties(配置)

point=com.test.action.PointConverter;

Point2=com.test.action.PointConverter;

Point:

指的是action中的属性,也就是jsp页面中的变量

com.test.action.PointConverter转换器类

全局类型转换

xwork-conversion.properties;

com.test.action.point=com.test.action.PointConverter;

Com.test.action.point表示要转换的类

com.test.action.PointConverter表示转换器

StrutsTypeConverter的类型转换相应的properties做相应的修改

com.test.action.point=com.test.action.PointConverter2;

publicclassPointConverter2extendsStrutsTypeConverter{

publicObjectconvertFromString(Mapcontext,String[]values,ClasstoClass){

Pointpoint=newPoint();

String[]paramValues=values[0].split(",");

intx=Integer.parseInt(paramValues[0]);

inty=Integer.parseInt(paramValues[1]);

point.setX(x);

point.setY(y);

returnpoint;

}

publicStringconvertToString(Mapcontext,Objecto){

Pointpoint=(Point)o;

intx=point.getX();

inty=point.getY();

Stringresult="x="+x+"y="+y;

returnresult;

}

}

集合类型的转换(注意properties也要修改)这里要注意泛型

publicclassPointConverter2extendsStrutsTypeConverter{

publicObjectconvertFromString(Mapcontext,String[]values,ClasstoClass){

Listlist=newArrayList();

for(Stringvalue:

values)

{

Pointpoint=newPoint();

String[]paramValues=value.split(",");

intx=Integer.parseInt(paramValues[0]);

inty=Integer.parseInt(paramValues[1]);

point.setX(x);

point.setY(y);

list.add(point);

}

returnlist;

}

publicStringconvertToString(Mapcontext,Objecto){

Listlist=(List)o;

StringBuildersb=newStringBuilder();//非同步效率高

for(Pointpoint:

list)

{

intx=point.getX();

inty=point.getY();

sb.append("x=").append(x).append(",y=").append(y);

}

returnsb.toString();

}

}

Struts2验证

1.在action中实现validate()方法进行验证

publicvoidvalidate(){

if(null==username||username.length()<6||username.length()>10)

{

this.addFieldError("username","出错提示信息");

}

}

遇到类型转换错误的时候(也就是说不能进行类型转换),struts2框架自动生成一条错误信息,并且将该错误信息放到addFieldError里面

替换struts标签的默认验证

在message.properties(全局)中设置

xwork.default.invalid.fieldvalue={0}error

来替换默认的struts标签的验证

要验证哪个action就要把其对应的资源文件放在对应的目录中那个

比如验证registerAction对应registerAction.properties

invalid.fieldvalue.age=ageerror(资源文件中的内容)

addActionError的用法

if(null==username||username.length()<6||username.length()>10)

{

this.addActionError("usernameerror");

}

由于jsp页面不会显示actionError错误信息所以,在页面要增加

actionerror>(默认只显示fieldError)

如果把

formaction=""theme="simple||ajax">

将不会显示表格

其他标签一样,几乎所有的标签都有一个theme属性

Struts1.0中的dispatchAction的处理模型在struts2中怎么处理

add.action">

delete.action">

/result.jsp

Action中要有add方法,默认执行execute方法

那么对应于add方法的验证就是validateAdd()方法

验证execute一样是validateExecute()

Struts2的验证框架

为每个需要验证的action创建对应的action-validation.xml

有多少了这样的action就要建立多少了这样的validation.xml

文件头dtd

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEvalidatorsPUBLIC"-//OpenSymphonyGroup//XWorkValidator1.0.2//EN""

Field校验(字段校验),出错信息放在fieldError级别中,如果页面用

actionError/>将不会显示错误信息

true

usernameshouldnotbeblank!

6

10

usernameshouldbebetween${minLength}and${maxLength}

passwordshouldnotbeblank!

6

10

passwordshouldbebetween${minLength}and${maxLength}

repasswordshouldnotbeblank!

6

10

repasswordshouldbebetween${minLength}and${maxLength}

ageshouldnotbeblank!

1

150

ageshouldbebetween${min}and${max}

birthdayshouldnotbeblank!

2001-01-01

2003-12-31

birthdayshouldbebetween${min}and${max}

graduationshouldnotbeblank!

2005-01-01

2007-12-31

graduationshouldbebetween${min}and${max}

非字段校验

username

usernameshouldnotbeblank!

username

6

10

usernameshouldbebetween${minLength}and${maxLength}

Struts2的客户端验证

1.form的theme不能设置为simple

2.Form的validate属性的值是true

(只做了解,最好不要使用)

为了解决action中多方法的校验要提供这样的

action-method-validation.xml文件

那么全局校验action-validation.xml是否提供自己注意,根据实际情况来定。

真正存放field级别错误信息的对象是LinkedHashMap

2.该LinkedHashMap的key是String类型的,value是ArrayList类型的

3.对于Action级别的错误信息,实际上是放置在ArrayList中的

设置断点debug调试

Validation.xml先执行,validate()方法后执行

Struts2的拦截器(intercepter)

拦截器的配置

如果action引用了自定义拦截器的话,默认的拦截器将不会执行,要执行默认的拦截器就必须也引入默认拦截器

/result.jsp

给拦截器赋值

aaa

method="add">

/result.jsp

bbb

拦截器栈的定义

自定义拦截器要实现Interceptor接口或者AbstractInterceptor抽象类

方法拦截器MethodFilterInterceptor

自定义一个拦截器继承MethodFilterInterceptor

实现方法doIntercept();

add,execute

包含要拦截的方法

add

不包含拦截的方法

拦截器实现权限验证

Mapmap=invocation.getInvocationContext().getSession();

if(map.get("user")==null){

returnAction.LOGIN;

}

returninvocation.invoke();

创建session

Mapmap=ActionContext.getContext().getSession();

map.put("user","aaa");

returnSUCCESS;

functionadd()

{

vartd=document.getElementById("more");

varbr=document.createElement("br");

varinput=document.createElement("input");

varbutton=document.createElement("input");

input.type="file";

input.name="file";

button.type="button";

button.value="Remove";

td.appendChild(br);

td.appendChild(input);

td.appendChild(button);

button.onclick=function()

{

td.removeChild(br);

td.removeChild(input);

td.removeChild(button);

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 临床医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1