import org.apache.struts2.ServletActionContext;
时间: 2024-01-17 09:04:46 浏览: 30
这段代码是使用 Struts2 框架时常用的一行代码,它的作用是获取当前请求的上下文对象。具体来说,它返回一个 ServletActionContext 对象,该对象包含了当前请求的 HttpServletRequest、HttpServletResponse 和 ServletContext 等信息。通过该对象,我们可以方便地获取请求参数、设置响应头等操作。
相关问题
翻译代码:package com.action; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.struts2.ServletActionContext; import com.dao.TGonggaoDAO; import com.model.TAdmin; import com.model.TGonggao; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class gonggaoAction extends ActionSupport { private Integer id; private String biaoti; private String neirong; private String shijian; private String message; private String path; private TGonggaoDAO gonggaoDAO; public String gonggaoAdd() { TGonggao gonggao=new TGonggao(); gonggao.setBiaoti(biaoti); gonggao.setNeirong(neirong); gonggao.setShijian(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())); gonggaoDAO.save(gonggao); this.setMessage("公告添加完毕"); this.setPath("gonggaoMana.action"); return "succeed"; } public String gonggaoMana() { List gonggaoList =gonggaoDAO.findAll(); Map request=(Map)ServletActionContext.getContext().get("request"); request.put("gonggaoList", gonggaoList); return ActionSupport.SUCCESS; } public String gonggaoDel() { TGonggao gonggao=gonggaoDAO.findById(id); gonggaoDAO.delete(gonggao); this.setMessage("公告删除完毕"); this.setPath("gonggaoMana.action"); return "succeed"; } public String gonggaoDetailHou() { TGonggao gonggao=gonggaoDAO.findById(id); Map request=(Map)ServletActionContext.getContext().get("request"); request.put("gonggao", gonggao); return ActionSupport.SUCCESS; } public String gonggaoAll() { List gonggaoList =gonggaoDAO.findAll(); Map request=(Map)ServletActionContext.getContext().get("request"); request.put("gonggaoList", gonggaoList); return ActionSupport.SUCCESS; } public String gonggaoDetailQian() { TGonggao gonggao=gonggaoDAO.findById(id); Map request=(Map)ServletActionContext.getContext().get("request"); request.put("gonggao", gonggao); return ActionSupport.SUCCESS; } public String getBiaoti() { return biaoti; } public void setBiaoti(String biaoti) { this.biaoti = biaoti; } public TGonggaoDAO getGonggaoDAO() { return gonggaoDAO; } public void setGonggaoDAO(TGonggaoDAO gonggaoDAO) { this.gonggaoDAO = gonggaoDAO; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getNeirong() { return neirong; } public void setNeirong(String neirong) { this.neirong = neirong; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getShijian() { return shijian; } public void setShijian(String shijian) { this.shijian = shijian; } }
这是一个 Java 的 Struts2 框架的 Action 类,包含了公告的增删改查等方法。具体代码如下:
```
package com.action;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.dao.TGonggaoDAO;
import com.model.TAdmin;
import com.model.TGonggao;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class gonggaoAction extends ActionSupport {
private Integer id;
private String biaoti;
private String neirong;
private String shijian;
private String message;
private String path;
private TGonggaoDAO gonggaoDAO;
public String gonggaoAdd() {
TGonggao gonggao=new TGonggao();
gonggao.setBiaoti(biaoti);
gonggao.setNeirong(neirong);
gonggao.setShijian(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
gonggaoDAO.save(gonggao);
this.setMessage("公告添加完毕");
this.setPath("gonggaoMana.action");
return "succeed";
}
public String gonggaoMana() {
List gonggaoList =gonggaoDAO.findAll();
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("gonggaoList", gonggaoList);
return ActionSupport.SUCCESS;
}
public String gonggaoDel() {
TGonggao gonggao=gonggaoDAO.findById(id);
gonggaoDAO.delete(gonggao);
this.setMessage("公告删除完毕");
this.setPath("gonggaoMana.action");
return "succeed";
}
public String gonggaoDetailHou() {
TGonggao gonggao=gonggaoDAO.findById(id);
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("gonggao", gonggao);
return ActionSupport.SUCCESS;
}
public String gonggaoAll() {
List gonggaoList =gonggaoDAO.findAll();
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("gonggaoList", gonggaoList);
return ActionSupport.SUCCESS;
}
public String gonggaoDetailQian() {
TGonggao gonggao=gonggaoDAO.findById(id);
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("gonggao", gonggao);
return ActionSupport.SUCCESS;
}
public String getBiaoti() {
return biaoti;
}
public void setBiaoti(String biaoti) {
this.biaoti = biaoti;
}
public TGonggaoDAO getGonggaoDAO() {
return gonggaoDAO;
}
public void setGonggaoDAO(TGonggaoDAO gonggaoDAO) {
this.gonggaoDAO = gonggaoDAO;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getNeirong() {
return neirong;
}
public void setNeirong(String neirong) {
this.neirong = neirong;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getShijian() {
return shijian;
}
public void setShijian(String shijian) {
this.shijian = shijian;
}
}
```
The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from required type org.apache.struts2.ServletActionContext
如果您在使用Apache Struts2时遇到了"The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from required type org.apache.struts2.ServletActionContext"的问题,这通常是由于缺少Servlet API依赖项引起的。以下是解决此问题的步骤:
1. 打开您的项目的构建路径设置。
2. 单击"Add Library"按钮,并选择"Server Runtime"库。
3. 选择您正在使用的服务器,这将添加Servlet API到您的项目中。
4. 如果仍然存在问题,请确保Servlet API库的版本与您正在使用的服务器版本相匹配。
5. 如果您使用的是Maven项目,请确保在您的pom.xml文件中添加了Servlet API依赖项,例如:
```xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
```
这应该解决您的问题。如果仍然存在问题,请确保您的项目中没有其他与Servlet API冲突的库。