菜鸟升级-------struts2学习第四天---关于OGNL和值栈的学习---OGNL的基本用法--值栈的基本用法

ONGL的基本用法

OGNL在struts2一般都是在jsp中应用的居停句法如下

    1,引入<taglib uri="/struts-tags" prefix="s"/>prifix表示简称
    2,开始调用<s:property value="调用的对象"/>

OGNL的注意事项:1,OGNL可以调用对象的动态和静态方法,但是调用对象静态方法必须要设置一下struts2的常量struts.ognl.allowStaticMethodAccess为true

ognl还可以访问root中的数据
获取context
ognlContext context=new ognlContext()
获取root
Object root=context.getRoot()
传数据进root
new User(“aaa”,”123”)
context.setRoot(user);
获取数据
ognl.getValue(“username”.contex,root);
ognl.getValue(“password”.contex,root);

ognl还可以访问context中的数据
获取context
ognlContext context=new ognlContext()
获取root
Object root=context.getRoot()
传数据进context
context.put(“aaa”,”123”);
获取数据
ognl.getValue(“#username”.contex,root);
ognl.getValue(“#password”.contex,root);

OGNL创建表单的功能

radio功能

如果radio的value值和选项的值相同则list可以直接赋值为list集合否则就用map集合赋值
List: s:radio list= “{‘男’,’女’}” name=”sex1” lable=”开头的名字”/>
Map: s:radio list= “#{‘1’:’男’,’2’:’女’}” name=”sex1” lable=”开头的名字”/>

遍历功能iterator

1,如果是遍历的对象是list集合则用:

s;iterator var=”i”接收的对象 value=”想要遍历的对象”>
s:property value=”i”/>–/br>
/s:iterator>

2,如果遍历的是map则用:

s;iterator接收的对象 value=”{‘a’:’b’,’c’:’d’}”>
s:property value=”key”/>–/br>
/s:iterator>

OGNL的特殊符号及其作用

作用

1,获取context的数据
2,构建map集合
%作用强制解析ognl具体用法%{#request.name}

值栈的基本用法

值栈的存取

方法一利用Action中值栈的特性

1,获得值栈,值栈会一直在Action中存在。

ValueStack valuestack= ActionContext.getContext().getValueStack();

2,在Action中创建私有实体类并创建get,set方法

private User user;

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

3在jsp中调用OGNL获取值栈的相应的数据

<%@ taglib uri=”/struts-tags” prefix=”s” %>
s:debug>//调用值栈的debug可以查看值栈内部
h2>获取方式一利用Action在值栈中的特性
s:property value=”user.username”/>
s:property value=”user.password”/>

方式二,利用值栈本身的方法,默认是取离最近栈顶匹配的变量值

1创建值栈

ValueStack valuestack= ActionContext.getContext().getValueStack();

2创建实体类对象并实例化,然后将其存入值栈中

User user=new User(“aaa”,”123”);
valueStack.push(user);

3在jsp中获取数据

   h2>获取方式二利用值栈中本身的方法  ,默认取栈顶属性名相同的数据</h2>
   s:property value="username"/>
    s:property value="password"/>

1利用Action操作数据

1,创建值栈

ValueStack valuestack= ActionContext.getContext().getValueStack();

2,创建List对象并将实体类对象实体化添加进list中

List<User> list=new ArrayList<User>();
    list.add(new User("ccc","111"));
    list.add(new User("ddd","222"));
    ActionContext.getContext().getValueStack().set("list", list);

3在jsp中用获取

<h2>利用actiontext批量操作数据</h2>
    <s:property value="list[0].username"/>
    <s:property value="list[0].password"/><br/>
    <s:property value="list[1].username"/>
    <s:property value="list[1].password"/><br/>

向Context中注入数据

1向request注入

ServletActionContext.getRequest().setAttribute(“name”,”request”);//request

2向session注入

ServletActionContext.getRequest().getSession().setAttribute(“name”,”session”);//session

1向application注入

ServletActionContext.getServletContext().setAttribute(“name”, “serlvetcontext”);//application

在jsp中获取context数据

获取context中的数据需要用#号 –>

    <s:property value="#request.name"/><br/>
    <s:property value="#session.name"/><br/>
    <s:property value="#application.name"/><br/>
    <s:property value="#attr.name"/><br/>
    <s:property value="#parameters.id"/><br/>

猜你喜欢

转载自blog.csdn.net/qq_42799000/article/details/81263411