public String delete(){ Dao dao = new Dao(); HttpServletRequest request = ServletActionContext.getRequest(); ActionContext ctx = ActionContext.getContext(); Map m = ctx.getParameters(); String id = request.getParameter("id"); int ID = 0; ID = Integer.valueOf(id); dao.delete(ID); return "suc"; }
public String edit(){ Dao dao = new Dao(); HttpServletRequest request = ServletActionContext.getRequest(); ActionContext ctx = ActionContext.getContext(); Map m = ctx.getParameters(); String id = request.getParameter("id"); int ID = 0; ID = Integer.valueOf(id); sa = dao.edit(ID); return "suc"; }
public String update(){ Dao dao = new Dao(); Student st = new Student(); HttpServletRequest request = ServletActionContext.getRequest(); ActionContext ctx = ActionContext.getContext(); Map m = ctx.getParameters(); String ms = (String)m.get("saveStudent_sa_id"); int ID = 0; st.setDece(sa.getDece()); st.setId(sa.getId()); st.setLove(sa.getLove()); st.setUsername(sa.getUsername()); dao.saveOrUpdate(st); return "suc"; }
你只用Struts的MVC来设计的话,连接数据你可以用 :1.连接池;2.读取属性文件;3.通用DAO这三种方法来连接数据;,查询的SQL语句:select 表字段 from 表名 where 条件字段=“条件”,,在action中调用相应的业务层的数据对象,可以把你所查询出来的信息放到session/request当中来,跳转到显示的页面:return mapping.findforward("name");把相应的数据遍历出来就可以了喽!希望我的描述可以给你来一点的收获!
只要在action中调用就可以了
DAO:
import java.util.List;import org.hibernate.Session;
import org.hibernate.Transaction;
import org.pany.struts2.action.StudentAction;import com.pany.hibernateSessionFactory.HibernateSessionFactory;
import com.pany.po.Student;public final class Dao {//把类设置为final有助于提高效率
public void insert(Object obj){
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try {
tx.begin();
session.save(obj);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}finally{
session.close();
}
}
public List<StudentAction> find(){
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
List list = new ArrayList();
try {
tx.begin();
list = session.createQuery("from Student").list();
tx.commit();
return list;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
session.close();
}
return null;
}
}Action package org.pany.struts2.action;import java.util.List;
import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionContext;
import com.pany.dao.Dao;
import com.pany.po.Student;
import com.pany.util.DataUtil;public class StudentAction {
private HttpServletRequest request;
private HttpServletResponse response;
private Integer id;
private String dece;
private String love;
private String username;
private String lastmodifytime;
private List resultList;//
private StudentAction sa;//
public StudentAction getSa() {
return sa;
}
public void setSa(StudentAction sa) {
this.sa = sa;
}
省略部分set get
public String execute(){
Dao dao = new Dao();
Student st = new Student();
st.setDece(sa.getDece());
st.setLove(sa.getLove());
st.setUsername(sa.getUsername());
st.setLastmodifytime(DataUtil.StringToTimestamp(sa.getUsername()));
dao.insert(st);
return "suc";
}
public String find(){
ActionContext ctx = ActionContext.getContext();
Dao dao = new Dao();
try {
resultList = dao.find();
return "suc";
} catch (Exception e) {
// TODO: handle exception
return "error";
}
}
public String delete(){
Dao dao = new Dao();
HttpServletRequest request = ServletActionContext.getRequest();
ActionContext ctx = ActionContext.getContext();
Map m = ctx.getParameters();
String id = request.getParameter("id");
int ID = 0;
ID = Integer.valueOf(id);
dao.delete(ID);
return "suc";
}
public String edit(){
Dao dao = new Dao();
HttpServletRequest request = ServletActionContext.getRequest();
ActionContext ctx = ActionContext.getContext();
Map m = ctx.getParameters();
String id = request.getParameter("id");
int ID = 0;
ID = Integer.valueOf(id);
sa = dao.edit(ID);
return "suc";
}
public String update(){
Dao dao = new Dao();
Student st = new Student();
HttpServletRequest request = ServletActionContext.getRequest();
ActionContext ctx = ActionContext.getContext();
Map m = ctx.getParameters();
String ms = (String)m.get("saveStudent_sa_id");
int ID = 0;
st.setDece(sa.getDece());
st.setId(sa.getId());
st.setLove(sa.getLove());
st.setUsername(sa.getUsername());
dao.saveOrUpdate(st);
return "suc";
}
<s:form action="" method="post">
<s:iterator value="resultList" status="index" id="l">
<s:if test="${id == '18'}">
test Success
</s:if>
<s:if test="#index.odd == true">
<tr style="background-color:#FFFF00">
</s:if>
<s:else>
<tr>
</s:else>
<td>用户名</td>
<td>
<s:property value="username"/>
<s:property value="love"/>
<s:property value="lastmodifytime"/>
<a href="Delete.faces?id=<s:property id="l" value="id"/>">
删除
</a>
<a href="Edit.faces?id=<s:property id="l" value="id"/>">
编辑
</a>
</td>
</s:iterator>
</s:form>