Java 如何使用 ActionContext 中存在的参数、请求和会话对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19538970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use parameters, request and session objects present in ActionContext?
提问by Siddharth Trikha
Here in this code I am using ActionContext to get Session and ServletActionContext from Request object. I feel this is bad practice, as one must use ActionContext only for Request object.
在这段代码中,我使用 ActionContext 从 Request 对象获取 Session 和 ServletActionContext 。我觉得这是不好的做法,因为必须仅对 Request 对象使用 ActionContext。
Is ActionContext's Request object equivalent to the Request object in Servlets ? If yes, how to get request parameters using it ?
ActionContext 的 Request 对象是否等同于 Servlets 中的 Request 对象?如果是,如何使用它获取请求参数?
Map session = (Map) ActionContext.getContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest();
String operatorId = request.getParameter("operatorId");
session.put("OperatorId", operatorId);
// getting hashmap from Bean
analysisNames= slsLoginDetailsRemote.getAnalysisNamesIdMap();
// sending map for multiselect
session.put("AnalysisNames",analysisNames);
采纳答案by Andrea Ligios
In Struts2, Session Map and Request Map are wrappers for the underlying HttpServletRequest and Session objects.
在 Struts2 中,Session Map 和 Request Map 是底层 HttpServletRequest 和 Session 对象的包装器。
If you only need to access attributes, use the wrappers.
如果您只需要访问属性,请使用包装器。
Use ActionContext to get them (both the wrappers and the underlying HTTP objects) onlyif you are inside an Interceptor
or a POJO
.
使用ActionContext中,让他们(无论是包装和底层HTTP对象),只有当你是内部的Interceptor
或POJO
。
If you are inside an Action
, the best practice is to implement an Interface that will automatically populate your Action's object:
如果您在 内Action
,最佳实践是实现一个接口,该接口将自动填充您的 Action 对象:
To get the Request Map wrapper, use RequestAware
要获取请求映射包装器,请使用RequestAware
public class MyAction implements RequestAware {
private Map<String,Object> request;
public void setRequest(Map<String,Object> request){
this.request = request;
}
}
To get the Session Map wrapper, use SessionAware
要获取会话映射包装器,请使用SessionAware
public class MyAction implements SessionAware {
private Map<String,Object> session;
public void setSession(Map<String,Object> session){
this.session = session;
}
}
To get the underlying HttpServletRequestand HttpSessionobjects, use ServletRequestAware
:
要获取底层HttpServletRequest和HttpSession对象,请使用ServletRequestAware
:
public class MyAction implements ServletRequestAware {
private javax.servlet.http.HttpServletRequest request;
public void setServletRequest(javax.servlet.http.HttpServletRequest request){
this.request = request;
}
public HttpSession getSession(){
return request.getSession();
}
}
That said, the standard data flow between JSP pages and Actions, or Actions and Actions, is obtained through Accessors / Mutators, better known as Getters and Setters. Don't reinvent the wheel.
也就是说,JSP 页面和操作或操作和操作之间的标准数据流是通过访问器/修改器(更广为人知的 Getter 和 Setter)获得的。不要重新发明轮子。
回答by DarkHorse
First
第一的
ActionContext's Request object is equivalent to the Request object in Servlets
Second
第二
If you are using framework like struts. It is a bad practice. You need not create HttpServletRequest objects from ServletActionContext for getting request parameters. Just declare request parametersin action class and write getters and setters for them will get you values in them.
如果您使用的是像 struts 这样的框架。这是一种不好的做法。您不需要从 ServletActionContext 创建 HttpServletRequest 对象来获取请求参数。只需在 action 类中声明请求参数并为它们编写 getter 和 setter 即可获得其中的值。
UPDATE
更新
If you wanted your request parameter in action class you can do it like this:
如果您想在操作类中使用请求参数,您可以这样做:
public class MyAction extends ActionSupport implements SessionAware{
private String operatorId;
private Map<String, Object> session;
//Getter and setters
public String getOperatorId() {
return operatorId;
}
public void setOperatorId(String operatorId) {
this.operatorId = operatorId;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
So now if I wanted to use operatorId
anywhere all I will do is getOperatorId()
or use operatorId
directly. :)
所以现在如果我想在operatorId
任何地方使用,我会做的就是直接getOperatorId()
使用operatorId
。:)
If find implementing SessionAware
in Action class more reasonable as I can directly access session objects like @Andrea has mentioned.. So now I can directly use session.put("OperatorId", operatorId);
and session.put("AnalysisNames",analysisNames);
如果发现SessionAware
在 Action 类中实现更合理,因为我可以直接访问 @Andrea 提到的会话对象。所以现在我可以直接使用session.put("OperatorId", operatorId);
和session.put("AnalysisNames",analysisNames);