Linux 拨号vps windows公众号手机端

Struct2中request.setAttribute()怎么使用

lewis 7年前 (2018-07-25) 阅读数 8 #程序编程

在Struct2中使用request.setAttribute()方法,首先需要获取request对象,然后调用其setAttribute()方法来设置属性值。

例如,在一个Action类中使用request.setAttribute()方法:

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class MyAction extends ActionSupport {
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("myAttribute", "Hello World");
return SUCCESS;
}
}

在上面的例子中,首先通过ServletActionContext.getRequest()方法获取到HttpServletRequest对象,然后调用其setAttribute()方法来设置属性值。这里将"Hello World"赋值给名为"myAttribute"的属性。

注意,使用request.setAttribute()方法设置的属性值只在当前请求中有效,如果想要在跳转的页面中获取这个属性值,可以使用Struts2的标签或表达式语言来获取。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门