struts2 异常处理总结
1---:https://www.sodocs.net/doc/cd18971078.html,ng.NoClassDefFoundError:
org/apache/struts2/dojo/views/jsp/ui/HeadTag
解决办法:原因缺少了dojo的JAR包,引入即可:struts2-dojo-plugin-2.1.2.jar
The "head" tag renders required JavaScript code to configure Dojo and is required in order to use any of the tags included in the Dojo plugin.
--------------------
If you are planning to nest tags from the Dojo plugin, make sure you set parseContent="false", otherwise each request made by the inner tags will be performed twice.
2---:Unable to load configuration. -bean
-jar:file:/F:/Struts2/Struts2/WebRoot/WEB-INF/lib/struts2-core-
2.1.2.jar!/struts-default.xml:46:178
Caused by: Unable to load bean:
type:org.apache.struts2.dispatcher.multipart.MultiPartRequest
class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest -bean -jar:file:/F:/Struts2/Struts2/WebRoot/WEB-INF/lib/struts2-core-2.1.2.jar!/str uts-default.xml:46:178
Caused by: https://www.sodocs.net/doc/cd18971078.html,ng.NoClassDefFoundError:
org/apache/commons/fileupload/RequestContext
解决办法:缺少JAR包,引入commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar 即可
3---:No tag "datetimepicker" defined in tag library imported with prefix "s"
原因版本问题:缺少struts-dojo-plugin JAR包,以及HTML的,
中没有使用If you're using Struts 2.1.x you're probablymissing the>struts-dojo-plugin. Michal's reference below applies to Struts 2.1.xonly. In the lasts versions (since 2.0.9 I guess), all AJAX are in dojo> plugin.
> > So you need toinclude <%@ taglib uri="/struts-dojo-tags" prefix="sx"%> > > and
> >( Temp1:
Temp2:
Temp3:
Temp4:
> > and call :
4---:使用TILES框架
If you use the Tiles 2 plugin, check your tiles.xml file(s) to ensure they contain a DOCTYPE.
"https://www.sodocs.net/doc/cd18971078.html,/dtds/tiles-config_2_0.dtd">
5---:struts2中不支持EL表达式
Struts2.1 tags do not allow evaluation of JSP EL within their attributes.Instead, Struts2 tags evaluate attribute values as OGNL. Allowing bothexpression languages within the same attribute opens major securityvulnerabilities.
6---文件上传过程中取不到文件名和文件类型,即都取到NULL
原因:如果页面中file的name=“a”则我们ACTION中设置String aContentType,String aFileName;(此两个其实无所谓,关键是SET方法)
setA(File file)(){}; setAContentType(String s){};setAFileName(String name){} 即格式如下:setXContentType() setXFileName().X代表你给FILE取的NAME名字
必须和它相同,固定格式
7---严重: Unable to parse request
https://www.sodocs.net/doc/cd18971078.html,mons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (8523356) exceeds the configured maximum (2097152)
at
https://www.sodocs.net/doc/cd18971078.html,mons.fileupload.FileUploadBase$FileItemIteratorImpl.
https://www.sodocs.net/doc/cd18971078.html,monsLogger warn
警告: Could not find property [struts.valueStack]
2008-5-27 17:46:51
https://www.sodocs.net/doc/cd18971078.html,monsLogger error
严重: the request was rejected because its size (8523356) exceeds the configured maximum (2097152)
2008-5-27 17:46:51
https://www.sodocs.net/doc/cd18971078.html,monsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
原因:上传文件大小超过预定大小,可以在struts.properties配置文件中设置
struts.multipart.maxSize=XXX(XXX为文件大小)
8---配置了文件类型限制后,当传错误类型可以拦截不让用户上传该文件,可是跳转的页面却没有跳转到input配置的错误页面,而是返回
到了success正确页面。
严重: Content-Type not allowed: filedata
"upload__5b01657_11a329d4dcf__8000_00000000.tmp" text/plain
《我的打印输出DEBUG语句内容:File:null FlieName:null type:null》即拦截类型成功了
https://www.sodocs.net/doc/cd18971078.html,ng.NullPointerException
at java.io.FileInputStream.
at com.study.web.util.FileUploadUtil.uploadFile(FileUploadUtil.java:36)
at com.study.web.action.UploadFileAction.execute(UploadFileAction.java:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)。。。
2008-5-29 10:59:07
https://www.sodocs.net/doc/cd18971078.html,monsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
2008-5-29 10:59:07
https://www.sodocs.net/doc/cd18971078.html,monsLogger warn
警告: Could not find property [struts]
2008-5-29 10:59:07
https://www.sodocs.net/doc/cd18971078.html,monsLogger info
信息: Removing file filedata
\tmp\upload__5b01657_11a329d4dcf__8000_00000000.tmp
原因及解决办法:在该文件上传Action中只配置了FileUploadInterceptor后缺少配置了defaultStack拦截器.
在ACTION中配置玩defaultStack拦截器后改错误消失。
9---struts.properties中全局配置文件大小,再使用默认FileUploadInterceptor拦截器时候能实现拦截的功能但是后台出现异常。
严重: Unable to parse request
https://www.sodocs.net/doc/cd18971078.html,mons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (380) exceeds the configured maximum (10)
at
https://www.sodocs.net/doc/cd18971078.html,mons.fileupload.FileUploadBase$FileItemIteratorImpl.
原因:未知。将全局配置中的限制大小去掉,再重新在ACTION中覆盖配置FileUploadInterceptor拦截器设置大小和文件类型限制,可消除异常。
10---多文件上传中,其中一个传被限制的文件如限制aplication/msword(即.doc)文件,允许传文本文件。然而当夹杂在一起传的时候,后台能截获类型错误不可传信息,但是
实际却还是上传成功。
严重: Content-Type not allowed: filedata
"upload_4d958287_11a33e76ab9__8000_00000007.tmp" application/msword file is :\tmp\upload_4d958287_11a33e76ab9__8000_00000006.tmp fileName:project.txt fileType:text/plain
file is :\tmp\upload_4d958287_11a33e76ab9__8000_00000007.tmp fileName:application base.doc fileType:application/msword
file is :\tmp\upload_4d958287_11a33e76ab9__8000_00000008.tmp fileName:zhongqi-bug.txt fileType:text/plain
原因:忘记了继承ActionSupport类。继承后异常消失。
11--文件上传异常,不能创建File文件
Cannot create type class java.io.File from value C:\Documents and
Settings\admin\桌面\OrderReporterServiceImp.java - [unknown location]
原因:忘记了在form表单里将enctype设置成文件上传格式:
enctype="multipart/form-data"
12--当使用限制文件类型和大小的时候抛出空指针异常
https://www.sodocs.net/doc/cd18971078.html,ng.NullPointerExceptiondemo.struts2.action.ValidatFileUploadAction.ex ecute(ValidatFileUploadAction.java:71)sun.reflect.NativeMethodAccessorImpl.i nvoke0(Native Method)原因:在ACTION中配置了fileUpload拦截器后忘记了配置默认拦截器:defautlStack。注意当ACTION中配置自己的拦截器后需要显示配置默认
拦截器defaultStack
13--在国际化时候抛空指针异常:
16:31:12,812 ERROR [jsp]:253 - Servlet.service() for servlet jsp threw exception
https://www.sodocs.net/doc/cd18971078.html,ng.NullPointerException at
java.text.MessageFormat.applyPattern(MessageFormat.java:414)
at java.text.MessageFormat.
at
com.opensymphony.xwork2.DefaultTextProvider.getText(DefaultTextProvider. java:70)
原因:忘记了在struts.xml中配置国际化常量,或者在struts.properties中配置全局国际化常量struts.custom.i18n.resources=globeMessage
14-- struts action的配置文件加载失败:
Unable to load configuration. - result -
file:/D:/Java/apache-tomcat-5.5.20/webapps/mysts/WEB-INF/classes/test.x ml:10:26
Caused by: No result type specified for result named 'error', perhaps the parent package does not specify the result type? - result -
file:/D:/Java/apache-tomcat-5.5.20/webapps/mysts/WEB-INF/classes/test.x ml:10:26
at
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildR esults(XmlConfigurationProvider.java:609)
原因:忘记了继承包struts-default
15-- struts action 配置文件注意事项:
1. 别忘记了继承struts-default默认配置包
2.如果配置命名空间,一定要以"/"开始,例如:namespace="/test"
3.每个模块struts action配置文件中都可以配置全局result,global-result 经测试不会冲突,STRTUS会智能寻找该Action请求的
模块STRUTS ACTION配置包
关键字: 警告: no configuration found for the specified action
警告: No configuration found for the specified action: 'ShowMessage' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
解决方法:
经过测试发现,是没有正确使用tag的原因,这种情况下,正确的写法应该是,
您是否添加了action这个呢?
1---:https://www.sodocs.net/doc/cd18971078.html,ng.NoClassDefFoundError:
org/apache/struts2/dojo/views/jsp/ui/HeadTag
解决办法:原因缺少了dojo的JAR包,引入即可:struts2-dojo-plugin-2.1.2.jar
The "head" tag renders required JavaScript code to configure Dojo and is required in order to use any of the tags included in the Dojo plugin.
--------------------
If you are planning to nest tags from the Dojo plugin, make sure you set parseContent="false", otherwise each request made by the inner tags will be performed twice.
2---:Unable to load configuration. -bean
-jar:file:/F:/Struts2/Struts2/WebRoot/WEB-INF/lib/struts2-core-
2.1.2.jar!/struts-default.xml:46:178
Caused by: Unable to load bean:
type:org.apache.struts2.dispatcher.multipart.MultiPartRequest
class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest -bean -jar:file:/F:/Struts2/Struts2/WebRoot/WEB-INF/lib/struts2-core-2.1.2.jar!/str uts-default.xml:46:178
Caused by: https://www.sodocs.net/doc/cd18971078.html,ng.NoClassDefFoundError:
org/apache/commons/fileupload/RequestContext
解决办法:缺少JAR包,引入commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar 即可
3---:No tag "datetimepicker" defined in tag library imported with prefix "s"
原因版本问题:缺少struts-dojo-plugin JAR包,以及HTML的,
中没有使用If you're using Struts 2.1.x you're probablymissing the>struts-dojo-plugin. Michal's reference below applies to Struts 2.1.xonly. In the lasts versions (since 2.0.9 I guess), all AJAX are in dojo> plugin.
> > So you need toinclude <%@ taglib uri="/struts-dojo-tags" prefix="sx"%> > > and
> >( Temp1:
Temp2:
Temp3:
Temp4:
> > and call :
4---:使用TILES框架
If you use the Tiles 2 plugin, check your tiles.xml file(s) to ensure they contain a DOCTYPE.
"https://www.sodocs.net/doc/cd18971078.html,/dtds/tiles-config_2_0.dtd">
5---:struts2中不支持EL表达式
Struts2.1 tags do not allow evaluation of JSP EL within their attributes.Instead, Struts2 tags evaluate attribute values as OGNL. Allowing bothexpression languages within the same attribute opens major securityvulnerabilities.
6---文件上传过程中取不到文件名和文件类型,即都取到NULL
原因:如果页面中file的name=“a”则我们ACTION中设置String aContentType,String aFileName;(此两个其实无所谓,关键是SET方法)
setA(File file)(){}; setAContentType(String s){};setAFileName(String name){} 即格式如下:setXContentType() setXFileName().X代表你给FILE取的NAME名字
必须和它相同,固定格式
7---严重: Unable to parse request
https://www.sodocs.net/doc/cd18971078.html,mons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (8523356) exceeds the configured maximum (2097152)
at
https://www.sodocs.net/doc/cd18971078.html,mons.fileupload.FileUploadBase$FileItemIteratorImpl.
https://www.sodocs.net/doc/cd18971078.html,monsLogger warn
警告: Could not find property [struts.valueStack]
2008-5-27 17:46:51
https://www.sodocs.net/doc/cd18971078.html,monsLogger error
严重: the request was rejected because its size (8523356) exceeds the configured maximum (2097152)
2008-5-27 17:46:51
https://www.sodocs.net/doc/cd18971078.html,monsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
原因:上传文件大小超过预定大小,可以在struts.properties配置文件中设置
struts.multipart.maxSize=XXX(XXX为文件大小)
8---配置了文件类型限制后,当传错误类型可以拦截不让用户上传该文件,可是跳转的页面却没有跳转到input配置的错误页面,而是返回
到了success正确页面。
严重: Content-Type not allowed: filedata
"upload__5b01657_11a329d4dcf__8000_00000000.tmp" text/plain
《我的打印输出DEBUG语句内容:File:null FlieName:null type:null》即拦截类型成功了
https://www.sodocs.net/doc/cd18971078.html,ng.NullPointerException
at java.io.FileInputStream.
at com.study.web.util.FileUploadUtil.uploadFile(FileUploadUtil.java:36)
at com.study.web.action.UploadFileAction.execute(UploadFileAction.java:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)。。。
2008-5-29 10:59:07
https://www.sodocs.net/doc/cd18971078.html,monsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
2008-5-29 10:59:07
https://www.sodocs.net/doc/cd18971078.html,monsLogger warn
警告: Could not find property [struts]
2008-5-29 10:59:07
https://www.sodocs.net/doc/cd18971078.html,monsLogger info
信息: Removing file filedata
\tmp\upload__5b01657_11a329d4dcf__8000_00000000.tmp
原因及解决办法:在该文件上传Action中只配置了FileUploadInterceptor后缺少配置了defaultStack拦截器.
在ACTION中配置玩defaultStack拦截器后改错误消失。
9---struts.properties中全局配置文件大小,再使用默认FileUploadInterceptor拦截器时候能实现拦截的功能但是后台出现异常。
严重: Unable to parse request
https://www.sodocs.net/doc/cd18971078.html,mons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (380) exceeds the configured maximum (10)
at
https://www.sodocs.net/doc/cd18971078.html,mons.fileupload.FileUploadBase$FileItemIteratorImpl.
原因:未知。将全局配置中的限制大小去掉,再重新在ACTION中覆盖配置FileUploadInterceptor拦截器设置大小和文件类型限制,可消除异常。
10---多文件上传中,其中一个传被限制的文件如限制aplication/msword(即.doc)文件,允许传文本文件。然而当夹杂在一起传的时候,后台能截获类型错误不可传信息,但是
实际却还是上传成功。
严重: Content-Type not allowed: filedata
"upload_4d958287_11a33e76ab9__8000_00000007.tmp" application/msword file is :\tmp\upload_4d958287_11a33e76ab9__8000_00000006.tmp fileName:project.txt fileType:text/plain
file is :\tmp\upload_4d958287_11a33e76ab9__8000_00000007.tmp fileName:application base.doc fileType:application/msword
file is :\tmp\upload_4d958287_11a33e76ab9__8000_00000008.tmp fileName:zhongqi-bug.txt fileType:text/plain
原因:忘记了继承ActionSupport类。继承后异常消失。
11--文件上传异常,不能创建File文件
Cannot create type class java.io.File from value C:\Documents and
Settings\admin\桌面\OrderReporterServiceImp.java - [unknown location]
原因:忘记了在form表单里将enctype设置成文件上传格式:
enctype="multipart/form-data"
12--当使用限制文件类型和大小的时候抛出空指针异常
https://www.sodocs.net/doc/cd18971078.html,ng.NullPointerExceptiondemo.struts2.action.ValidatFileUploadAction.ex ecute(ValidatFileUploadAction.java:71)sun.reflect.NativeMethodAccessorImpl.i nvoke0(Native Method)原因:在ACTION中配置了fileUpload拦截器后忘记了配置默认拦截器:defautlStack。注意当ACTION中配置自己的拦截器后需要显示配置默认
拦截器defaultStack
13--在国际化时候抛空指针异常:
16:31:12,812 ERROR [jsp]:253 - Servlet.service() for servlet jsp threw exception
https://www.sodocs.net/doc/cd18971078.html,ng.NullPointerException at
java.text.MessageFormat.applyPattern(MessageFormat.java:414)
at java.text.MessageFormat.
at
com.opensymphony.xwork2.DefaultTextProvider.getText(DefaultTextProvider. java:70)
原因:忘记了在struts.xml中配置国际化常量,或者在struts.properties中配置全局国际化常量struts.custom.i18n.resources=globeMessage
14-- struts action的配置文件加载失败:
Unable to load configuration. - result -
file:/D:/Java/apache-tomcat-5.5.20/webapps/mysts/WEB-INF/classes/test.x ml:10:26
Caused by: No result type specified for result named 'error', perhaps the parent package does not specify the result type? - result -
file:/D:/Java/apache-tomcat-5.5.20/webapps/mysts/WEB-INF/classes/test.x ml:10:26
at
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildR esults(XmlConfigurationProvider.java:609)
原因:忘记了继承包struts-default
15-- struts action 配置文件注意事项:
1. 别忘记了继承struts-default默认配置包
2.如果配置命名空间,一定要以"/"开始,例如:namespace="/test"
3.每个模块struts action配置文件中都可以配置全局result,global-result 经测试不会冲突,STRTUS会智能寻找该Action请求的
模块STRUTS ACTION配置包
关键字: 警告: no configuration found for the specified action
警告: No configuration found for the specified action: 'ShowMessage' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
解决方法:
经过测试发现,是没有正确使用tag的原因,这种情况下,正确的写法应该是,
您是否添加了action这个呢?
1、自定义拦截器,实现对注册页面上的文字信息进行过滤拦截,不允许出现字符集合中{“佛法”,”集会”,”党派”}的文字信息。如出现,则返回到注册页面,重新填写。 2、定义拦截器,实现登录检查。由于在项目开发时,需要对大多数的页面进行登录检查。当没有登录就无法进行操作,并返回到登录页面。为减少代码量利用Struts2中自定义拦截器的功能,实现登录检查。(避免不登录直接访问某个Action) Web.xml
Struts2拦截器详细配置过程 1:所有拦截器的超级接口Interceptor,拦截器去实现这个接口; Interceptor它其中有三个方法 (init(),destroy(),interceptor()):Init()方法:在服务器起动的时候加载一次,并且只加载一次; Destroy()方法:当拦截器销毁时执行的方法; Interceptor()方法:其中里边有一个参数invocation public String intercept(ActionInvocation invocation)throws xception { System.out.println("interceptor!!"); String result=invocation.invoke(); return result; }Invocation.invoke()是如果只有一个拦截器执行完这个方法后,会返回给视图,如果有多 个拦截器,它顺序的执行完所有的拦截器,才返回给视图. 2:可以在系统初始化中给拦截器指定默认的参数(也包括了定义拦截器方式)如下:在拦截器类中把hello当做属性set/get方式注入到拦截器类中;
题目1 以下不届丁 Struts2中result 的type 届性() ? A. action B. redirect 题目2 下歹0有关拦截器说法错误的是? 「A.struts 通过拦截器完成执行action 请求处理方法前一系歹U 操作。例如: 数据封装、文件上传、数据校验等 'B.在struts 中,直接访问jsp 页面,struts 将使用默认拦截器栈处理当前 请求。 厂C.在执行action 时,struts 将执行若干拦截器1、2、3,执行action 完成 后,将继续执行拦截器3、2、1 'D.默认情况,在一个action 没有配置拦截器的引用,说明当前action 将不 使用拦截器 题目3 以下哪些是Action 接口提供的返回值? W A A. success ,D B. none C. error 财 D.input 题目4 如果要实现struts2的数据检验功能 广A 普通的Action 类可以实现 C. redirectAction D. dispatcher
「B继承自Action接口的可以实现 面C继承自ActionSupport类可以实现 厂D继承自ActionValidate 类可以实现 题目5 struts2默认的处理结果类型是: ? A.dispatcher ' B.redirect 「C.chain D. forward 题目6 在值栈的上下文Context中,存在一些固定的key表示不同的对象,以下描述正确的是? A. request,表示request作用域的数据 'B.session,表示session 作用域的数据 阿 C.application ,表示application 作用域的数据 * D.parameters ,表示请求参数的所有数据 题目7 以下届丁struts2配置文件中的配置元素是:()多选) A.
由于struts2标签的性能不好,项目组决定不使用,但是如果用struts2自带的拦截器防止重复提交又必须struts标签,所以只好自定拦器实现,具体步骤如下: 新建拦截器类: public class TokenAtionInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception { Map
结果类型 Action处理完用户请求后,将返回一个普通的字符串,整个普通字符串就是一个逻辑视图。Struts2通过配置一个逻辑视图和物理视图的映射关系,一旦系统返回某个逻辑视图系统就会把对应的物理视图呈现给用户。 Struts2 在struts.xml中使用
com.opensymphony.xwork2.interceptor.Interceptor接口: public class PermissionInterceptor implements Interceptor { private static final long serialVersionUID = -5178310397732210602L; public void destroy() { } public void init() { } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("进入拦截器"); if(session里存在用户){ String result = invocation.invoke(); }else{ return “logon”; } //System.out.println("返回值:"+ result); //return result; } }
Struts S2-045漏洞调试及分析 Auth:Cryin’ Date:2016.3.9 漏洞公告 首先看官方给出的漏洞公告信息: “Possible Remote Code Execution when performing file upload based on Jakarta Multipart parser.” 问题原因: “It is possible to perform a RCE attack with a malicious Content-Type value. If the Content-Type value isn't valid an exception is thrown which is then used to display an error message to a user.” 从公告信息可以得到几个漏洞的重点信息: ●存在漏洞的模块是Jakarta ●漏洞产生的点是恶意的Content-Type头 ●恶意的Content-Type头会造成程序抛出异常,在显示错误消息给用户时造成RCE 补丁对比 查看Struts2版本2.3.32在github上的commit(Uses default error key if specified key doesn't exist)并对比修改内容: https://https://www.sodocs.net/doc/cd18971078.html,/apache/struts/commit/352306493971e7d5a756d61780d57a76eb1 f519a 可以看到对LocalizedTextUtil.findText方法进行了重写,并添加了判断。Struts2RCE漏洞的根本原因是程序将用户可控数据带入OGNL表达式解析并执行,而OGNL(Object Graph Navigation Language)对象图形导航语言是一个功能强大的表达式语言,可以用来获取和设置Java对象的属性,但同时也可以对服务端对象进行修改,绕过沙盒甚至可以执行系统命令。 所以,从补丁分析来看LocalizedTextUtil.findText函数很可能是OGNL表达式的传入点,在调试分析时可在此处下断点跟踪分析。 关于jakarta Jakarta是struts2默认处理multipart报文的解析器,该组件定义在struts-default.xml中: 默认使用org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest类对上传数据进行解析并调用第三方组件commons-fileupload.jar完成上传操作。
ERROR [com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler]-Could not create and/or set value back on to object 引用 struts2类型转换异常时错误信息屏蔽 2011-05-2416:29 类型转换异常导致转换方法没有正常返回,fieldError中添加了相应的错误记录,系统打印出改异常,级别为warm。我认为不算是struts2的bug.转换异常抛出警告很正常。如想屏蔽可用logger4j配置级别到error即可 ognl.MethodFailedException:Method"setLoc"failed for object tjx.HelloWorld@651e95[https://www.sodocs.net/doc/cd18971078.html,ng.NoSuchMethodException: tjx.HelloWorld.setLoc([https://www.sodocs.net/doc/cd18971078.html,ng.String;)] at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1285) at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1474) at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccesso r.java:85) at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:16 2) at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(Ob jectAccessor.java:27) at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2245) at
1.Struts1存在的缺点是什么? 答:Struts1的缺点:没有实现model的整合技术;没有与Freemark或velocity等模板技术整合的技术;由于与ServletAPI耦合严重,测试需要web服务器,比较麻烦,属于侵入式设计,系统重构困难。 2.与struts1对比,Struts2 的优点是什么? 答:Action不再与servletAPI耦合,实现B/S向C/S结构的转换;Action不需要和webWork 耦合,代码重用性好;支持更多的表现技术层;测试更加容易。 3.struts2框架的处理流程? 答:浏览器发送请求;FilterDispatcher根据用户的请求调用相应的action;Struts2的拦截器链自动对请求调用通用功能;回调Action的execute方法;Execute方法处理结果将被输出到浏览器中,对应于相应的表现技术。 4.Struts2的web应用如何自动整合其他框架? 答:structs 2框架提供一种类似esclipse的扩展方式,它允许一种“可插拔”的方式安装插件,提供类似Structs2-xxx-plugin.jar的文件,只要该文件放在web应用的WEB-INF/lib路径下,struts2框架将自动加载该框架。 5.Struts2中如何匹配后缀为.action的请求的,是否可以改成.do? 答:可以,还可以改成/*任何其他URL pattern形式 6.在struts.xml中,包含配置的作用是什么? 答:采用
Struts2 中的拦截器和servelt 中的过滤器是非常的相似的。如果学过过滤器的话,肯定能够感觉的到,尽管有些微的不同。可是struts2的拦截器到底如何使用呢,为什么会有这些配置呢?接下来一一来看。 过滤器和拦截器是非常相似的,过滤器public interface Filter 接口里面有三个方法: ?init(FilterConfig filterConfig), ?destroy(), ?doFilter(ServletRequest request, ServletResponse response, FilterChain chain), 这里面的doFilter() 方法是最重要的,在struts2 中String intercept(ActionInvocation invocation)就相当于此方法。 如何完成一个拦截器呢?在struts2 中要实现一个接口这个接口是什么呢?在哪呢?,是否在哪听说过?是webwork 是我们以前听的最多的关于拦截器的框架,struts2 用了其中一个核心的东西,这个东西在是什么呢?是xwork 。恩,有了它才可以拦截,好了我们在哪找呢?在com.opensymphony.xwork2.interceptor 中找,里面有个Interceptor 这是个接口,里面也有三个方法,有init,destroy 和intercept 三个方法,而在struts2 里面的所有的拦截器都继承这个接口!
为了看这些是怎么实现的,加入了一些打印! 将上面的配置整合起来就是:
这样就可以让Aciton 被拦截了,到此,好了,可以运行程序了: 输出结果是:启动服务器init 被打出 运行后提交action 输出intercept 这个就是初步的一个拦截器。 ======= 在此可能出现一个问题,是什么呢?如果就我们做的注册程序而言,可以想一下,有数据转换,有数据校验,以前当转换和校验不符合的时候,点击提交,会提示相关错误信息,然而,此时当转换和校验依然不符合要求时,点击提交,却不会提示错误信息,为什么呢? ==== 当然你答对了,这些功能都包含在struts2的默认拦截器中,这里没执行是被添加的拦截器myinterceptor取代了。 为了查明原因可以查看一下struts2-core-2.xx.jar中的struts-default.xml 这个文件 这里定义的很多的东西,和我们的程序相关的非常紧密 首先这里有个
一、第一个异常处理 严重: Servlet.service() for servlet jsp threw exception The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location] at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60) at org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44) at https://www.sodocs.net/doc/cd18971078.html,ponentTagSupport.doStartTag(ComponentTagSupport.java:48) at https://www.sodocs.net/doc/cd18971078.html,_005fstruts2_005flz.YpxxbQuery_jsp._jspx_meth_s_005fdebug_005f0(Ypxxb Query_jsp.java:108) at https://www.sodocs.net/doc/cd18971078.html,_005fstruts2_005flz.YpxxbQuery_jsp._jspService(YpxxbQuery_jsp.java:83) 解决的办法:(4种解决方案) 1. web.xml 中添加一个filter
理解拦截器 1.1.什么是拦截器: 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。 在Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。 谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。 1.2.拦截器的实现原理: 大部分时候,拦截器方法都是通过代理的方式来调用的。Struts 2的拦截器实现相对简单。当请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器。如下图: 2.拦截器的配置 Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以至struts2的jar包内的struts-default.xml查看关于默认的拦截器与拦截器链的配置。 Struts2(XWork)提供的拦截器的功能说明:
3.使用拦截器 一旦定义了拦截器和拦截器栈后,就可以使用这个拦截器或拦截器栈来拦截 栈将会失去作用。为了继续使用默认拦截器,所以上面配置文件中手动引入了默认拦截器。 4.自定义拦截器 作为“框架(framework)”,可扩展性是不可或缺的。虽然,Struts 2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反,在Struts 2自定义拦截器是相当容易的一件事。 4.1.实现拦截器类: 所有的Struts 2的拦截器都直接或间接实现接口 com.opensymphony.xwork2.interceptor.Interceptor。该接口提供了三个方法: 1)void init(); 在该拦截器被初始化之后,在该拦截器执行拦截之前, 系统回调该方法。对于每个拦截器而言,此方法只执行一次。 2)void destroy();该方法跟init()方法对应。在拦截器实例被销毁之前, 系统将回调该方法。
Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Interceptor ,Action去实现这个接口; Interceptor 它其中有三个方法 (init(),destroy() ,interceptor()): Init()方法:在服务器起动的时候加载一次,并且只加载一次; Destroy()方法:当拦截器销毁时执行的方法; Interceptor()方法:其中里边有一个参数invocation public String intercept(ActionInvocation invocation) throws xception { System.out.println("interceptor!!"); String result=invocation.invoke(); return result; } Invocation.invoke()是如果只有一个拦截器执行完这个方法后,会返回给视图,如果有多个拦截器,它顺序的执行完所有的拦截器,才返回给视图. 2:可以在系统初始化中给拦截器指定默认的参数(也包括了定义拦截器方式)如下:在拦截器类中把hello当做属性set/get方式注入到拦截器类中;
张钊钊