Struts2进阶学习3
Struts2进阶学习3
OGNL表达式与Struts2的整合

核心配置文件与页面
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="show" namespace="/" extends="struts-default" >
<action name="ShowAction" class="com.struts2.action.ShowAction" method="execute" >
<result name="success" type="dispatcher" >/show.jsp</result>
</action> <action name="ShowAction2" class="com.struts2.action.ShowAction" method="getParam" >
<result name="success" type="dispatcher" >/form.jsp</result>
</action> <action name="redirect" class="com.struts2.action.RedirectAction" method="redirect" >
<result name="success" type="redirectAction" >
<param name="namespace">/</param>
<param name="actionName">ShowAction</param>
<!-- 路径携带参数;以及利用OGNL表达式动态绑定参数 -->
<param name="name">${name}</param>
</result>
</action>
</package> </struts>
struts.xml
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/ShowAction">
name:<input type="text" name="name"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
form
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:debug></s:debug>
</body>
</html>
show
package com.struts2.pojo; /**
* @author: 肖德子裕
* @date: 2018/11/20 21:05
* @description:
*/
public class User {
private String name;
private Integer age; /**
* 回音方法:传什么值返回什么值,一般用于测试
* @param o
* @return
*/
public static Object test(Object o){
return o;
} public User() {
} public User(String name, Integer age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}
User
OGNL表达式基本语法
package com.struts2.ognl; import com.struts2.pojo.User;
import ognl.Ognl;
import ognl.OgnlContext;
import org.junit.Test; import java.util.HashMap;
import java.util.Map; /**
* @author: 肖德子裕
* @date: 2018/11/20 21:03
* @description: 测试OGNL表达式(对象视图导航语言),支持比EL表达式更丰富的语法
*/
public class Demo {
/**
* OGNL基本语法
* @throws Exception
*/
@Test
public void test() throws Exception{
//准备Root(可放置任意对象)
User rootUser=new User("xdzy",18);
//准备Context(放置map)
Map<String,User> context=new HashMap<String,User>();
context.put("user1",new User("jack",17));
context.put("user2",new User("rose",27));
//准备OGNLContext
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context); //书写OGNL
//获取rootUser的name和age(第一个参数就是OGNL的语法)
String rootName = (String) Ognl.getValue("name", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
System.out.println(rootName);
System.out.println(age); //获取context的name与age
String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
System.out.println(name); //对rootUser属性进行赋值
String rootName1 = (String) Ognl.getValue("name='张三'", oc, oc.getRoot());
System.out.println(rootName1); //对context属性进行赋值
String name1 = (String) Ognl.getValue("#user1.name='李四'", oc, oc.getRoot());
System.out.println(name1); //调用rootUser的属性方法
Ognl.getValue("setName('王五')", oc, oc.getRoot());
String rootName2 = (String) Ognl.getValue("getName()", oc, oc.getRoot());
System.out.println(rootName2); //调用context的属性方法
String name2 = (String) Ognl.getValue("#user1.setName('赵六'),#user1.getName()", oc, oc.getRoot());
System.out.println(name2); //调用静态方法;访问静态属性
String name3 = (String) Ognl.getValue("@com.struts2.pojo.User@test('hello')", oc, oc.getRoot());
Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
System.out.println(name3);
System.out.println(pi);
} /**
* OGNL基本语法2
* @throws Exception
*/
@Test
public void test2() throws Exception{
//准备Root(可放置任意对象)
User rootUser=new User("xdzy",18);
//准备Context(放置map)
Map<String,User> context=new HashMap<String,User>();
context.put("user1",new User("jack",17));
context.put("user2",new User("rose",27));
//准备OGNLContext
OgnlContext oc=new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context); //书写OGNL
//通过OGNL创建list
Integer size = (Integer) Ognl.getValue("{'肖','德','子','裕'}.size()", oc, oc.getRoot());
System.out.println(size);
//通过OGNL创建map
Integer size1 = (Integer) Ognl.getValue("#{'name':'xdzy','age':12}.size()", oc, oc.getRoot());
String name = (String) Ognl.getValue("#{'name':'xdzy','age':12}['name']", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("#{'name':'xdzy','age':12}.get('age')", oc, oc.getRoot());
System.out.println(size1);
System.out.println(name);
System.out.println(age);
}
}
Demo
测试整合使用
package com.struts2.action; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.util.ValueStack;
import com.struts2.pojo.User; /**
* @author: 肖德子裕
* @date: 2018/11/21 9:40
* @description: 测试OGNL表达式与struts2结合
* OGNL表达式的root在struts2中为栈,栈中保存的是当前访问的action
* 队列:先进先出;栈:先进后出(list)
* OGNL表达式的root在struts2中为ActionContext
* implements Preparable:在参数赋值之前实现
*/
public class ShowAction extends ActionSupport implements ModelDriven<User> {
/**
* 重写默认方法
* @return
* @throws Exception
*/
@Override
public String execute() throws Exception {
System.out.println("hello action");
return SUCCESS;
} private User user=new User(); /**
* 如果在参数赋值之前压栈,将无法获取参数值
* @return
*/
public String getParam(){
System.out.println(user);
return SUCCESS;
} /**
* 该拦截器在参数赋值之前实现,所以可以获取参数值
* @throws Exception
*/
/*@Override
public void prepare() throws Exception {
//获取值栈
ValueStack stack = ActionContext.getContext().getValueStack();
//压入栈顶
stack.push(user);
}*/ @Override
public User getModel() {
return user;
}
}
ShowAction
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; /**
* @author: 肖德子裕
* @date: 2018/11/21 10:40
* @description: 重定向时携带参数
*/
public class RedirectAction extends ActionSupport {
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String redirect(){
name="xdzy";
return SUCCESS;
}
}
RedirectAction
Struts2进阶学习3的更多相关文章
- Struts2进阶学习4
Struts2进阶学习4 自定义拦截器的使用 核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <! ...
- Struts2进阶(一)运行原理及搭建步骤
Struts2进阶(一)运行原理 Struts2框架 Struts2框架搭建步骤 致力于web服务,不可避免的涉及到编程实现部分功能.考虑使用到SSH框架中的Struts2.本篇文章只为深入理解Str ...
- PHP程序员进阶学习书籍参考指南
PHP程序员进阶学习书籍参考指南 @heiyeluren lastmodify: 2016/2/18 [初阶](基础知识及入门) 01. <PHP与MySQL程序设计(第4版)> ...
- Matlab 进阶学习记录
最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal = proposal_config('image_means', ...
- struts2源代码学习之初始化(一)
看struts2源代码已有一段时日,从今天開始,就做一个总结吧. 首先,先看看怎么调试struts2源代码吧,主要是下面步骤: 使用Myeclipse创建一个webproject 导入struts2须 ...
- Struts2框架学习(三) 数据处理
Struts2框架学习(三) 数据处理 Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:Value ...
- Struts2框架学习(二) Action
Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...
- Struts2框架学习(一)
Struts2框架学习(一) 1,Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发.框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2 ...
- zuul进阶学习(二)
1. zuul进阶学习(二) 1.1. zuul对接apollo 1.1.1. Netflix Archaius 1.1.2. 定期拉 1.2. zuul生产管理实践 1.2.1. zuul网关参考部 ...
随机推荐
- oracle学习篇六:子查询
-- 1.查询比7654工资要高的员工 select * from emp where sal>(select sal from emp where empno=7654); ---2.查询最低 ...
- PHP环境配置解释
PHP中注释:#,//,/* */ 一.修改Apache配置 DocumentRoot "G:\PHP" //修改完需要重启Apache //以下二选一 ----- ...
- html+css定位篇
position absolute相对于父元素移动,不在父元素范围内时,可能和其他元素重叠 relative相对于初始位置来进行移动 fixed相对于浏览器进行定位,无论滑轮如何滚动,始终出现在浏览器 ...
- linux注册服务教程
该说明是项目完成很久之后,整理资料时的偶然发现,当时所操作的linux为中标麒麟,需要对项目进行开机自启,对llinux还不熟悉,找不到linux中的服务自启设置.辗转多次才找到了解决方案.记录以供参 ...
- Fidder详解之get和post请求
前言 本文会对Fidder这款工具的一些重要功能,进行详细讲解,带大家进入Fidder的世界,本文会让你明白,Fidder不仅是一个抓包分析工具,也是一个请求发送工具,更加可以当作为Mock Serv ...
- python 后台运行命令
nohup python a.py > a.log 2>&1 & 在窗口中单开虚拟session: tmux new -s "name" 推出虚拟窗口 ...
- 如何将使用托管磁盘虚拟机的 OS 盘挂载到其他虚拟机上
适用场景 当出现虚拟机无法启动等情况时,需要将虚拟机的 OS 磁盘挂载到其他虚拟机上进行问题诊断或者数据恢复.使用托管磁盘的虚拟机无法通过存储浏览器等工具进行管理,只能通过 PowerShell 来操 ...
- IFrame安全问题解决办法(跨框架脚本(XFS)漏洞)
最近项目要交付了,对方安全测试的时候检测出高危险漏洞,由于刚参加工作不久,经验不足,未涉及过此方面的东西.经过一番查询和探索,最终解决了这个问题,记录一下. 发现的漏洞为缺少跨框架脚本保护.跨框架脚本 ...
- Browser进程和浏览器内核(Renderer进程)的通信过程
看到这里,首先,应该对浏览器内的进程和线程都有一定理解了,那么接下来,再谈谈浏览器的Browser进程(控制进程)是如何和内核通信的, 这点也理解后,就可以将这部分的知识串联起来,从头到尾有一个完整的 ...
- C#并行编程 z
目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C#并行编程-线程同步原语 C#并行编程-PLINQ:声明式数据并行 背景 基于任务的程序 ...