Struts2 动态方法调用
01.Struts 2基本结构
使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问ServletAPI时实现用户会话跟踪,其简单的程序运行流程图如下
Struts2框架是基于MVC模式。基于MVC模式框架的核心就是控制器对所有请求进行统一处理.Struts2的控制器StrutsPrepareAndExecuteFilter由ServletAPI中的Filter充当,当web容器的接收到登录请求后,将请求交由在web.xml中配置的过滤器StrutsPrepareAndExecuteFilter.
1.web.xml
Struts2框架需要在web.xml中配置其核心控制器——StrutsPrepareAndExecuteFilter,用于对框架进行初始化,以及处理所有的请求.
如何搜索xml文件加载顺序?
1.找到项目的web.xml找到核心过滤器
2.找到init方法
Action中动态方法调用<Dynamic Method Invocation> DMI
第一种方式:
自定义DMIAction类,使它继承ActionSupport类,该类无需手动重写execute(),底层有默认实现。因此我们也可以自定义方法list。
struts.xml中的action元素植入method调用前台返回的方法list
若一个类中有多个方法,在struts.xml中需植入多个action元素,因此该方法的安全性低
第二种方式:
在struts.xml中开启动态方法调用,即可使用一个action,并通过在Action的名称中使用感叹号(!)来标识要调用的方法名称
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/* * 添加图书 */ public String add() throws Exception { System.out.println( "======add====" ); return "add" ; } /* * 删除图书 */ public String del() throws Exception { System.out.println( "======del====" ); return "del" ; } /* * 修改图书 */ public String edit() throws Exception { System.out.println( "======edit====" ); return "edit" ; } |
执行效果:
四、Action中通配符的使用
通配符用星号(*)表示,用于配置0个或多个字符串,在配置Action时,可以在action元素的name属性中使用星号来匹配任意的字符串
实现效果:
最新文章
- [LeetCode] Fraction to Recurring Decimal 分数转循环小数
- GConf error:Failed to contact configuration server
- 第 21 章 CSS3 文本效果
- Tomcat配置并启用HTTPS
- http://highscalability.com/blog/2015/5/18/how-mysql-is-able-to-scale-to-200-million-qps-mysql-cluster.html
- windows环境下MySQL重启的命令行说明
- Intel项目Java小记
- JBPM4.4部署到tomcat6异常解决办法
- Linux RAR 安装和使用详细说明
- Slf4j的包冲突
- ASP.NET MVC页面报错System.InvalidOperationException The view found at &#39;~/Views/Home/Index.cshtml&#39; was not created.
- markdown特殊符号语法
- JVM原理分析
- FFmpeg configure: rename cuda to ffnvcodec 2018-03-06
- vue+vux scrollTop无法实现定位到具体dom
- phpstorm官方教程
- [UE4]位与字节
- Json中不支持任何形式的注释,那我们要怎么解决呢
- 搭建私有yum仓库
- ASP 三十二条精华代码 (1)