Struts2知识点总结报告Word格式.docx

上传人:b****2 文档编号:1359210 上传时间:2023-04-30 格式:DOCX 页数:14 大小:29.05KB
下载 相关 举报
Struts2知识点总结报告Word格式.docx_第1页
第1页 / 共14页
Struts2知识点总结报告Word格式.docx_第2页
第2页 / 共14页
Struts2知识点总结报告Word格式.docx_第3页
第3页 / 共14页
Struts2知识点总结报告Word格式.docx_第4页
第4页 / 共14页
Struts2知识点总结报告Word格式.docx_第5页
第5页 / 共14页
Struts2知识点总结报告Word格式.docx_第6页
第6页 / 共14页
Struts2知识点总结报告Word格式.docx_第7页
第7页 / 共14页
Struts2知识点总结报告Word格式.docx_第8页
第8页 / 共14页
Struts2知识点总结报告Word格式.docx_第9页
第9页 / 共14页
Struts2知识点总结报告Word格式.docx_第10页
第10页 / 共14页
Struts2知识点总结报告Word格式.docx_第11页
第11页 / 共14页
Struts2知识点总结报告Word格式.docx_第12页
第12页 / 共14页
Struts2知识点总结报告Word格式.docx_第13页
第13页 / 共14页
Struts2知识点总结报告Word格式.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Struts2知识点总结报告Word格式.docx

《Struts2知识点总结报告Word格式.docx》由会员分享,可在线阅读,更多相关《Struts2知识点总结报告Word格式.docx(14页珍藏版)》请在冰点文库上搜索。

Struts2知识点总结报告Word格式.docx

2.以依然注入(IoC)方式访问,实现响应的接口即可

3.使用以耦合方式使用ServletActionContext类访问

OGNL与struts标签

引入的标签指令<

%@tagliburi="

/struts-tags"

prefix="

s"

%>

理解值栈(存储action属性的地方)和stack context(存储request,session的地方)概念,并且使用<s:

debug>查看值栈访问的值

OGNL访问集合知识点,不常用

字符串转换OGNL表达式用户%{}相反的加’’单引号

Struts2标签

可分为通用标签,控制标签,Ajax标签

通用标签又分为数据标签和控制标签

数据标签:

<s:

property>

debug>

date>

set>

url>和<s:

a>

param>

include>

控制标签

if>,<s:

elseif><s:

else>

iterator>

UI标签

理解模板与主题,模板用于生成HTML代码,共同风格和观感的模板组织到一起就成了一个主题,内建的主题有simple,xhtml,css_xhtml(默认),css_xhtml,ajax主题的更改用标签属性theme=”simple”

标签:

s:

form>

textfield>

textarea>

submit>

select>

doubleselect>

下拉列框和联动列框属性及实现参观相关代码

Ajax标签

Datetimepicker是一个日历控件

导入相应的包引入<

/struts-dojo-tags"

sx"

指令

在<

head>

中添加<

sx:

headparseContent="

true"

/>

添加标签<

datetimepickername="

timer"

label="

发布时间"

4.数据校验

数据校验分为客户端验证和服务器端验证,sruts2对服务器端的验证有两种,

1.使用actionsupport编码实现验证

2.使用验证框架实现验证,

使用actionsupport编码进行验证的方法有三种,

1.直接在action类业务方法中验证,

2.重写validate()方法实现验证,

3.使用validateXXX()方法实现验证

验证的关键点在于当用户名或密码没填写时添加addFieldError()方法,此方法处理与字段相关的错误,比如输入的年龄不合理,响应的jsp输出错误的标签是<

fielderrorfieldname=”user.name/>

addActionError()方法是用来添加与action所处理的业务相关的错误信息,在JSP输出错误的标签是<

actionerror>

如果两种情况有任何一种错误(hasErrors())就return到INPUT结果集中(事先已经配置好)

使用验证框架

使用特定的配置框架即可,命名的前半部分是action的名字,可对全局和局部进行验证!

具体查看相关资料

5.拦截器

拦截器,在AOP(Aspect-OrientedProgramming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。

拦截是AOP的一种实现策略

谈到拦截器,还有一个词大家应该知道——拦截器链(InterceptorChain,在Struts2中称为拦截器栈InterceptorStack)。

拦截器链就是将拦截器按一定的顺序联结成一条链。

在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。

struts2拦截器是action调用之前或之后调用的代码

拦截器实现原理:

大部分时候,拦截器方法都是通过代理的方式来调用的。

Struts2的拦截器实现相对简单。

当请求到达Struts2的ServletDispatcher时,Struts2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器

返回类型区别

struts2中关于result的返回类型一般我们是转发到一个jsp页面或者是html页面等,但是struts2中的result的返回类型还有redirect,redirectAction,chain。

对于这三种返回类型之间肯定是有区别的,下面我们来看看关于redirectredirectActionchain这三种struts2的返回类型之间的区别。

当使用type=“redirectAction”或type=“redirect”提交到一个action并且需要传递一个参数时。

这里是有区别的:

使用type=“redirectAction”时,结果就只能写Action的配置名,不能带有后缀:

“.action”

 

拦截器的配置

定义拦截器的标签<

interceptor>

定义拦截器栈的标签<

interceptor-stack>

引用拦截器<

interceptor-ref>

默认的拦截器<

default-interceptor-ref>

自定义拦截器

实现自定义拦截器类的方式有3种

实现Interceptor接口

继承AbstractInterceptor抽象类(常用)

继承MethodFilterIntercePtor类

具体用法参见帮助文档

6.MVC的优缺点

优点

1各司其职,互不干涉

2.并行开发,提高效率

3.有利于组件的重用

缺点

1.系统结构和实现比较复杂

2.视图与控制器过于紧密

3.不适用于小型甚至中等规模的应用程序

7.Struts2注解

必要的jar包

commons-fileupload-1.2.1.jar

commons-io-1.4.jar

commons-logging-1.1.1.jar

commons-logging-api.jar

freemarker-2.3.15.jar

ognl-2.7.3.jar

struts2-core-2.1.8.1.jar

xwork-core-2.1.6.jar

struts2-convention-plugin-2.1.8.1.jar

其中struts2-convention-plugin-2.x.x.jar是用于支持注解的

2.web.xml的配置:

[html] 

viewplain 

copy 

print?

1.<

filter>

2.<

filter-name>

struts2<

/filter-name>

3.<

filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<

/filter-class>

4.<

init-param>

5.<

param-name>

actionPackages<

/param-name>

6.<

param-value>

com.test.action<

/param-value>

7.<

/init-param>

8.<

/filter>

9. 

10.<

filter-mapping>

11.<

12.<

url-pattern>

/*<

/url-pattern>

13.<

/filter-mapping>

3.struts.xml的配置:

?

xml 

version="

1.0"

encoding="

UTF-8"

>

!

DOCTYPE 

struts 

PUBLIC 

3. 

"

-//Apache 

Software 

Foundation//DTD 

Struts 

Configuration 

2.1.7//EN"

4. 

http:

//struts.apache.org/dtds/struts-2.1.7.dtd"

5. 

struts>

7. 

-- 

请求参数的编码方式 

-->

8. 

constant 

name="

struts.i18n.encoding"

value="

指定被struts2处理的请求后缀类型。

多个用逗号隔开 

10. 

struts.action.extension"

action,do,htm"

11. 

当struts.xml改动后,是否重新加载。

默认值为false(生产环境下使用),开发阶段最好打开 

12. 

struts.configuration.xml.reload"

13. 

是否使用struts的开发模式。

开发模式会有更多的调试信息。

14. 

struts.devMode"

false"

15. 

设置浏览器是否缓存静态内容。

默认值为true(生产环境下使用),开发阶段最好关闭 

16. 

struts.serve.static.browserCache"

17. 

指定由spring负责action对象的创建 

18. 

struts.objectFactory"

spring"

19. 

20. 

是否开启动态方法调用 

21. 

struts.enable.DynamicMethodInvocation"

22.<

/struts>

4.常用的注解如下:

Namespace:

指定命名空间。

ParentPackage:

指定父包。

Result:

提供了Action结果的映射。

(一个结果的映射)

Results:

“Result”注解列表

ResultPath:

指定结果页面的基路径。

Action:

指定Action的访问URL。

Actions:

“Action”注解列表。

ExceptionMapping:

指定异常映射。

(映射一个声明异常)

ExceptionMappings:

一级声明异常的数组。

InterceptorRef:

拦截器引用。

InterceptorRefs:

拦截器引用组。

5.示例代码如下:

那些类会被作为Action,

对于Convention插件而言,它会自动搜索位于action,actions,struts,struts2包下的所有java类,

Convention插件会把如下两种java类当成Action处理

①类名以XXXAction命令。

②继承ActionSuppot。

6.查看struts2配置 

为了看到struts2应用里的Action等各种资源的影射情况,struts2提供了ConfigBrowser插件。

使用方法:

将struts2-config-browser-plugin-2.1.6.jar文件复制到struts2应用的WEB-INF/lib目录中。

打开首页地址:

//localhost:

8080/应用名字/config-browser/actionNames.action这里可以看到ConfigBrowser插件的首页。

7.详解

1、Action级的注解

@ParentPackage--父包

 

该注解相当于<

packagename="

login"

extends="

struts-default"

/package>

中的extends属性,例如:

@ParentPackage(value="

@Namespace----命名空间

该注解相当于

namespace="

logintest"

中的namespace属性,例如:

@@Namespace(value="

/longspace"

@Result---转向

该注解相当于

actionname="

class="

com.lwsoft.action.LoginAction"

<

resultname="

success"

/loginsuc.jsp<

/result>

/action>

中的<

例如:

@Result(name="

location="

/loginsuc.jsp"

type="

redirect"

)!

注意,在struts2.1.6中是location来转向页面,而在struts2.0.4是value属性

@Results来配置多个转向

@Results({

@Result(name="

/usersuc.jsp"

),

error"

/usererror.jsp"

})以上是对struts2注解简单的介绍,

要想在Action上使用注解,还得修web.xml文件中的一些配置

struts<

org.apache.struts2.dispatcher.FilterDispatcher<

//固定格式-->

action所在的包,如果在多个包中,用逗号隔开 

com.struts2.action1,com.struts.action2-->

com.struts2.action<

@Results({ 

input"

value="

/input.jsp"

type=NullResult.class), 

@Reuslt(name="

/success.jsp"

/error.jsp"

type=NullResult.class) 

})

上面是一个典型的多Result配置,name属性指定返回的字符串,value指定要跳转的页面,type指定Result的类型,type是一个很重要的属性,他有一下几种情况:

1)NullResult 

:

默认的可以省略

2)ActionChainResult:

用于从一个Action跳转到另外一个Action。

例如:

@Result 

(name="

search"

value="

type=ActionChainResult.class,params={"

method"

"

这个配置是如果返回值为"

就跳转掉SearchAction的search方法,如果不指定params就跳转到SearchAction的execute方法 

3)StreamResult:

用于文件下载。

(name="

value="

inputStream"

type=StreamResult.class,params={"

bufferSize"

FileConstant.DOWNLOAD_BUFFER_SIZE}) 

案例

1.package 

com.tjcyjd.web.action;

2. 

3.import 

org.apache.struts2.convention.annotation.Action;

4.import 

org.apache.struts2.convention.annotation.ExceptionMapping;

5.import 

org.apache.struts2.convention.annotation.ExceptionMappings;

6.import 

org.apache.struts2.convention.annotation.Namespace;

7.import 

org.apache.struts2.convention.annotation.ParentPackage;

8.import 

org.apache.struts2.convention.annotation.Result;

9.import 

org.apache.struts2.convention.annotation.Results;

11.import 

com.opensymphony.xwork2.ActionSupport;

13./** 

Struts2基于注解的Action配置 

*/ 

17.@ParentPackage("

) 

18.@Namespace("

/annotation_test"

19.@Results({ 

@Result(name 

 

location 

/main.jsp"

), 

}) 

21.@ExceptionMappings({ 

@ExceptionMapping(exception 

java.lange.RuntimeException"

result 

22.public 

class 

LoginAction 

extends 

ActionSupport 

23. 

private 

static 

final 

long 

serialVersionUID 

2730268055700929183L;

24. 

String 

loginName;

25. 

password;

26. 

27. 

@Action("

28. 

// 

或者写成 

@Action(value 

29. 

public 

login() 

throws 

Exception 

30. 

31. 

if 

("

yjd"

.equals(loginName) 

&

.equals(password)) 

32. 

return 

SUCCESS;

33. 

else 

34. 

ERROR;

35. 

36. 

37. 

38. 

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

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2