将 Servlet 转换为 Struts2 操作类
我正在将一个动态 Web 项目移植到 Struts2,并且必须将许多 servlet 转换为 Struts2 操作。我想使用 ServletRequestAware、ServletResponseAware 和 SessionAware 接口并保持大部分代码不变。请看一下我通过 GOOGLin' 找到的 BaseAction.java 类。我想使用它,以便我可以执行其他操作来简单地扩展 BaseAction。
BaseAction.java
public abstract class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware,ServletResponseAware, ServletContextAware{
private static final long serialVersionUID = 1L;
protected Map session;
protected HttpServletRequest request;
protected HttpServletResponse response;
public String execute() throws Exception {
return doExecute();
}
protected abstract String doExecute() throws Exception;
public Map getSession() {
return session;
}
public void setSession(Map sess) {
this.session = sess;
}
public HttpServletRequest getServletRequest() {
return request;
}
public void setServletRequest(HttpServletRequest req){
this.request = req;
}
public HttpServletResponse getServletResponse() {
return response;
}
public void setServletResponse(HttpServletResponse resp) {
this.response = resp;
}
}
现在假设我有一个如下所示的 servlet: Servlet.java
public class Servlet extends HttpServlet
{
private static Logger log = Logger.getLogger(Servlet.class);
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException
{
HttpSession session=request.getSession();
Bean bean= new Bean();
bean.setName(request.getParameter("xxx"));
session.setAttribute("bean");
response.sendRedirect("login.jsp");
}
}
有人可以编写一个相当于 Servlet 的扩展 BaseAction 的操作类吗? 提前致谢。
I'm porting a dynamic web project to Struts2 and I gotta convert a many servlets to Struts2 actions. I want to use the ServletRequestAware, ServletResponseAware and SessionAware interfaces and keep most of the code unchanged. Please take a look at BaseAction.java class which I found by GOOGLin'. I want to use this so that i can make other actions to simply extend the BaseAction.
BaseAction.java
public abstract class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware,ServletResponseAware, ServletContextAware{
private static final long serialVersionUID = 1L;
protected Map session;
protected HttpServletRequest request;
protected HttpServletResponse response;
public String execute() throws Exception {
return doExecute();
}
protected abstract String doExecute() throws Exception;
public Map getSession() {
return session;
}
public void setSession(Map sess) {
this.session = sess;
}
public HttpServletRequest getServletRequest() {
return request;
}
public void setServletRequest(HttpServletRequest req){
this.request = req;
}
public HttpServletResponse getServletResponse() {
return response;
}
public void setServletResponse(HttpServletResponse resp) {
this.response = resp;
}
}
Now Suppose I have a servlet like following:
Servlet.java
public class Servlet extends HttpServlet
{
private static Logger log = Logger.getLogger(Servlet.class);
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException
{
HttpSession session=request.getSession();
Bean bean= new Bean();
bean.setName(request.getParameter("xxx"));
session.setAttribute("bean");
response.sendRedirect("login.jsp");
}
}
Can someone write an action class extending the BaseAction equivalent to the Servlet?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是 Struts2,我建议您充分利用该框架提供的所有功能,而不是尝试破解现有的 servlet 代码。 struts2 框架可以轻松处理获取和设置请求参数和会话的 http 内容:
您的 struts.xml 应该包含该操作的条目:
Since you're using Struts2, I recommend taking advantage of everything the framework offers instead of trying to hack existing servlet code. The http stuff of getting and setting request parameters and sessions is handled easily by the struts2 framework:
Your struts.xml should contain an entry for the action:
我希望下面的示例能帮助你..
i hope below sample will help u..