大家好!由于工作需要,需要设计一个web查询,类似于输入出版社名或者输入书的类别(计算机或者英语什么的)显示检索到的书目,需要用struts2,现在自己刚接触这方面知识,还没有太清晰的概念,再加上工期紧,所以一边查资料一边在csdn提问,希望大家多指点。能提供struts2连接数据库进行查询的类似代码就更好了。感谢!

解决方案 »

  1.   

    struts2和连接数据库没啥直接关系吧
      

  2.   

    struts2和连接数据库没啥直接关系吧
      

  3.   

    连接数据库查询是数据访问层和业务逻辑层做的
    只要在action中调用就可以了
      

  4.   

    自己写个dao,用jdbc,hibernate都可以阿通过struts2展示
      

  5.   


    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";
    }
      

  6.   

    jsp
      <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>
      

  7.   

    謝謝各位了,我表述的不是很清楚。我知道struts2是表現层的,这次开发想利用MVC,所以选择用struts2,如何操作呢?
      

  8.   

    你只用Struts的MVC来设计的话,连接数据你可以用 :1.连接池;2.读取属性文件;3.通用DAO这三种方法来连接数据;,查询的SQL语句:select 表字段 from 表名 where 条件字段=“条件”,,在action中调用相应的业务层的数据对象,可以把你所查询出来的信息放到session/request当中来,跳转到显示的页面:return mapping.findforward("name");把相应的数据遍历出来就可以了喽!希望我的描述可以给你来一点的收获!