package cn.itcast.ognl;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
public class OgnlDemo1 {
public static void main(String[] args) throws OgnlException {
// ognl可以通过对象调用方法
// System.out.println("aaa".length());
// 使用ognl来完成上面操作
// 1.创建一个ognl上下文
OgnlContext context = new OgnlContext();
Object obj1 = Ognl.getValue("'aaa'.length()", context.getRoot());
System.out.println(obj1);
}
}
package cn.itcast.ognl;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
public class OgnlDemo2 {
public static void main(String[] args) throws OgnlException {
// ognl可以通过类调用静态方法
System.out.println(Math.max(10, 20));
System.out.println(Math.PI);
// 使用ognl来完成上面操作
// 1.创建一个ognl上下文
OgnlContext context = new OgnlContext();
Object obj1 = Ognl.getValue("@java.lang.Math@max(10,20)",
context.getRoot());
System.out.println(obj1);
Object obj2 = Ognl.getValue("@java.lang.Math@PI", context.getRoot());
System.out.println(obj2);
}
}
package cn.itcast.ognl;
public class Person {
private String name;
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
package cn.itcast.ognl;
public class Dog {
private String name;
private Person p;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person getP() {
return p;
}
public void setP(Person p) {
this.p = p;
}
}
package cn.itcast.ognl;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
public class OgnlDemo3 {
public static void main(String[] args) throws OgnlException {
// 创建一个ognl上下文
OgnlContext context = new OgnlContext();// 本质上是一个Map集合
Person p = new Person();
p.setName("张三");
Dog dog = new Dog();
dog.setName("lucy");
p.setDog(dog); // 张三有条狗叫lucy
context.setRoot(p);
Dog dog1 = new Dog();
dog1.setName("豆豆");
Person pp = new Person();
pp.setName("jams");
dog1.setP(pp);
context.put("dog", dog1);
context.put("name", "tom");
// 使用ognl来获取根中数据,不需要加#
Object name1 = Ognl.getValue("name", context, context.getRoot());
System.out.println(name1);
// 使用ognl来获取非根中的数据,获取非根中的数据 需要使用#
Object name2 = Ognl.getValue("#name", context, context.getRoot());
System.out.println(name2);
// 获取张三的狗的名称
Object name3 = Ognl.getValue("dog.name", context, context.getRoot());
System.out.println(name3);
// 获取豆豆的主人名称
Object name4 = Ognl.getValue("#dog.p.name", context, context.getRoot());
System.out.println(name4);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:使用ognl来通过对象调用方法
Struts会把下面这些映射压入ContextMap中
package cn.test.domain;
public class Product {
private String name;
private double price;
private int count;
public String getName() {
return name;
}
public Product() {
super();
}
public Product(String name, double price, int count) {
super();
this.name = name;
this.price = price;
this.count = count;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
package cn.test.domain;
public class User {
public User() {
super();
}
public User(String username, String password, int age, String sex) {
super();
this.username = username;
this.password = password;
this.age = age;
this.sex = sex;
}
private String username;
private String password;
private int age;
private String sex;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
struts.xml
/ognl2.jsp
/ognl3.jsp
/ognl4.jsp
/ognl5.jsp
/ognl6.jsp
my.properties
package cn.test.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class OgnlDemo1Action extends ActionSupport {
@Override
public String execute() throws Exception {
// 获取ValueStack
// 1.通过request获取
ValueStack vs = (ValueStack) ServletActionContext.getRequest()
.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
System.out.println(vs);
// 2.通过ActionContext获取
ValueStack vs2 = ActionContext.getContext().getValueStack();
System.out.println(vs2);
return null;
}
}
package cn.test.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class OgnlDemo2Action extends ActionSupport {
@Override
public String execute() throws Exception {
// 向valueStack中存入数据
ValueStack vs=ActionContext.getContext().getValueStack();
vs.set("username", "tom");
vs.push("hello world");
return SUCCESS;
}
}
ognl2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:使用ognl来获取valueStack数据
1.获取栈顶的map
2.获得栈顶非Map集合数据
3.获得OgnlContext中数据
<%
request.setAttribute("rname", "rvalue");
session.setAttribute("sname", "svalue");
application.setAttribute("aname", "avalue");
%>
package cn.test.action;
import java.util.ArrayList;
import java.util.List;
import cn.test.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class OgnlDemo3Action extends ActionSupport {
@Override
public String execute() throws Exception {
// 向valueStack中存入数据(root)
ValueStack vs = ActionContext.getContext().getValueStack();
List users = new ArrayList();
users.add(new User("tom", "123", 20, "男"));
users.add(new User("jams", "456", 21, "男"));
users.add(new User("fox", "789", 26, "男"));
// vs.push(users);
vs.set("users", users);
return SUCCESS;
}
}
ognl3.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:使用ognl来获取valueStack中复杂数据
1.使用push存储数据存取
username:
password:
username:
password:
2.使用set存储数据
username:
password:
username:
password:
package cn.test.action;
import java.util.ArrayList;
import java.util.List;
import cn.test.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
public class OgnlDemo4Action extends ActionSupport implements ModelDriven {
private List users;
private User user = new User("tom", "123", 20, "男");
public User getModel() {
return user;
}
public List getUsers() {
return users;
}
public void setUsers(List users) {
this.users = users;
}
public String getHello() {
return "hello world";
}
@Override
public String execute() throws Exception {
ValueStack vs = ActionContext.getContext().getValueStack();
users = new ArrayList();
users.add(new User("tom", "123", 20, "男"));
users.add(new User("jams", "456", 21, "男"));
users.add(new User("fox", "789", 26, "男"));
user = new User("fox", "789", 26, "男");
return SUCCESS;
}
}
ognl4.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:使用ognl来获取valueStack中复杂数据
package cn.test.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class OgnlDemo5Action extends ActionSupport {
@Override
public String execute() throws Exception {
ValueStack vs = ActionContext.getContext().getValueStack();
vs.set("username", "张三"); // 向ValueStack压入数据
return SUCCESS;
}
}
ognl5.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:使用el获取ValueStack中数据
Ognl获取:
el获取:${username }
package cn.test.action;
import java.util.ArrayList;
import java.util.List;
import cn.test.domain.Product;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class OgnlDemo6Action extends ActionSupport {
private List ps;
public List getPs() {
return ps;
}
public void setPs(List ps) {
this.ps = ps;
}
@Override
public String execute() throws Exception {
// 对ps进行赋值
ps = new ArrayList();
ps.add(new Product("电视机", 1800, 100));
ps.add(new Product("洗衣机", 800, 10));
ps.add(new Product("空调", 3800, 100));
return SUCCESS;
}
}
ognl6.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:进行投影操作
1.使用iterator进行遍历
名称:
价格:
数量:
2.对集合进行投影,只得到指定的属性
3.将ps中价格大于1000的商品得到
名称:
价格:
数量:
4.将ps中价格大于1000的商品名称得到
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:进行投影操作
1.可以使用#来构建一个Map集合
----
2.可以构建List集合
3.手动创建一个集合,在Struts2中是结合表单标签使用的
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:%号的使用
<%
request.setAttribute("username", "tom");
%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
演示:$用法
<%
request.setAttribute("username", "张三");
%>
在国际化文件中使用:
save
package cn.itcast.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegistServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String token = request.getParameter("token");
String _token = (String) request.getSession().getAttribute("token");
request.getSession().removeAttribute("token");
if (token.equals(_token)) {
System.out.println("将" + username + " 与" + password
+ "存储到数据库中,完成注册");
} else {
response.getWriter().write("不允许表单重复提交");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
My JSP 'index.jsp' starting page
<%
//生成一个令牌
String token=UUID.randomUUID().toString();
session.setAttribute("token", token);
%>
/token.jsp
RegistAction
package cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class RegistAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
System.out.println("将" + username + " 与" + password + "存储到数据库中,完成注册");
return null;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
My JSP 'index.jsp' starting page
token.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
My JSP 'index.jsp' starting page
package cn.test.domain;
import org.apache.struts2.json.annotations.JSON;
public class Product {
private int id;
private String name;
private double price;
private int count;
public Product() {
super();
}
public Product(int id, String name, double price, int count) {
super();
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
// @JSON(serialize=false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Ajax1Action
package cn.test.action;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Ajax1Action extends ActionSupport {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String execute() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
response.getWriter().write("hello " + msg);
response.getWriter().close();
return null;
}
}
Ajax2Action
package cn.test.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import cn.test.domain.Product;
import com.opensymphony.xwork2.ActionSupport;
public class Ajax2Action extends ActionSupport {
private List ps;
private Product p;
public Product getP() {
return p;
}
public void setP(Product p) {
this.p = p;
}
public List getPs() {
return ps;
}
public void setPs(List ps) {
this.ps = ps;
}
@Override
public String execute() throws Exception {
ps = new ArrayList();
ps.add(new Product(1, "洗衣机", 1800, 10));
ps.add(new Product(2, "电视机", 2800, 20));
ps.add(new Product(3, "缝纫机", 800, 50));
p = new Product(3, "缝纫机", 800, 50);
return SUCCESS;
}
}
my.js
function getXmlHttpRequest() {
var xmlhttp=null;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest(); //针对于现在的浏览器包括IE7以上的版本
} else if(window.ActiveXObject){
// 针对于IE5,IE6版本
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
struts.xml
ps\[\d+\]\.name,ps\[d+\]\.price,ps\[\d+\]\.count
ajax1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
第一种Struts2处理异步请求
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
第一种Struts2处理异步请求