js可以通过OGNL表达式从struts的值栈中取值吗?

js可以通过OGNL表达式从struts的值栈中取值吗?,第1张

是可以实现的,具体如下:

值栈中存放着一些OGNL可以访问的数据,如下:

      a:action的实例,这样就可以通过OGNL来访问Action实例中的属性的值了。

      b:OGNL表达式运算的值,可以设置到值栈中,可主动访问值栈对象,强行设置。

      c:OGNL表达式产生的中间变量,比如使用Struts2标签的时候,使用循环标签,自然会有循环的变量,这些都放在值栈中。

例子,修改用户输入的参数信息,如下图所示,

    图:用户输入了aa的username

   图:用户提交后发现username属性的值发生了变化

实现:

首先定义一个实现PreResultListener接口的类:MyPreResultListener

import com.opensymphony.xwork2.ActionInvocation

import com.opensymphony.xwork2.interceptor.PreResultListener

public class MyPreResultListener implements PreResultListener {

@Override

public void beforeResult(ActionInvocation invocation, String resultCode) {

System.out.println("现在处理Result执行前的功能, result=" + resultCode)

//在Result处理之前修改value stack里面的username对应的值

   invocation.getInvocationContext().getValueStack().setValue("username", "被修改了")

}

}

然后在相应的Action中进行注册:

import com.capinfotech.listener.MyPreResultListener

import com.opensymphony.xwork2.ActionContext

import com.opensymphony.xwork2.ActionSupport

public class PreResultAction 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

}

public String execute() {

System.out.println("用户输入的参数为,username:" + username + ", password:" + password)

ActionContext context = ActionContext.getContext()

MyPreResultListener preListener = new MyPreResultListener()

context.getActionInvocation().addPreResultListener(preListener)

return "success"

}

}

EL表达式和Ognl表达式,取数据的区别11.1、一般的EL表达式:${}查询变量的顺序为:1,当前域,即作用域2,page,request,session,application(从小到大,没有返回null${}可以写在jsp页面的任何位置,包括字符串中11.2、使用了Struts2后,在JSP中写的EL表达式(${})查询变量的顺序为:1,作用域,当前域2,值栈,ValueStack3,ActionContext中的map集合:包括:request,session,application,parameters,attr集合,还有ActionContext本身的集合。这里面的查询顺序我就不知道${}可以在jsp中,也可以在xml文件中(如在struts.xml文件里)11.3、Strut2中,jsp,ognl表达式必须使用#key来取数据,且必须指定根对象,如#session[‘age’],表示到ActionContext持有的Session域中取数据不加#号,默认是到值栈中去取。如:,意思是:#valuestack.name可详细参考Struts2笔记中的ognl表达式Ognl表达式遇到字符串,需要使用%{}转义11.4、Ognl表达式取的数据都在ActionContext这个map集合中去Ognl表达式必须使用#key来取数据。这个#就表示ActionContext这个map集合


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6736618.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存