我的理解是,MVC模式中jsp只是用于显示,业务逻辑,包括数据库操作放在Servlet中,至于如何将数据库中的信息显示,则可以在servlet中处理后,将所得的内容通过session或者request,传递给jsp.

解决方案 »

  1.   

    你可以为你的数据写一个formBean,里面全是set get 方法就可以了比如你得到一个RecordSet
    然后你把得到的纪录全部转换成formBean对象,那样就是一个formBean数组
    再把他方到session里面或是request里面
    你就可以在jsp中用struts标签 <logic:iterate id="fromBean" name="my" type="jp.go.maff.interactive.domain.form.FormBean" indexId="index" offset="1">
                <tr class="odd1" height="20"> 
                  <td width="40" align="center">
      <logic:notMatch name="fromBean" property="statusName" value="承認">  
      <a href="javaScript:setAction('LinkFormAction.do','<bean:write name="fromBean" property="id" filter="true"/>')"><bean:write name="index" /></a>
      </logic:notMatch>   <logic:match name="fromBean" property="statusName" value="承認">
    <bean:write name="index" />
      </logic:match>
      </td>
                  <td width="180"><bean:write name="fromBean" property="title" filter="true"/></td>
                  <td width="150"><bean:write name="fromBean" property="deptName" filter="true"/></td>
                  <td width="200"><bean:write name="fromBean" property="statusName" filter="true"/></td>
                  <td width="50" align="center">
      <logic:notMatch name="fromBean" property="statusName" value="承認">
                    <input type="button" value="削除" name="" onClick="javaScript:ProcDelete('1','DeleteFormAction.do','<bean:write name="fromBean" property="id" filter="true"/>')">
    </logic:notMatch>
                  </td>
                </tr>
    </logic:iterate>
      

  2.   

    有道理!
    我的form bean已经写好了,那又如何把RecordSet转换成form bean呢,这是个关键所在,请指教!谢。我会给多多的分。:)
      

  3.   

    你能不能看一下 struts 的example???
    那里面说得很明白.不过还是给你一个我自己的吧.这是一个标准的actionpackage com.boesoft.newscentre;import java.util.Locale;import javax.servlet.http.*;import org.apache.struts.action.*;
    import org.apache.struts.util.MessageResources;
    public class ForwardAction extends Action {
        public ActionForward perform(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {
            Locale locale = this.getLocale(request);
            MessageResources messages = this.getResources();
    ActionErrors errors = new ActionErrors();
            System.err.println(request.getQueryString());
            String page = ((ForwardForm) form).getPage();
            String forward = ((ForwardForm) form).getForward();
    System.err.println(page);
            HttpSession session = request.getSession();        if (mapping.getAttribute() != null) {
                if ("request".equals(mapping.getScope()))
                    request.removeAttribute(mapping.getAttribute());
                else
                    session.removeAttribute(mapping.getAttribute());
            }        if(null != page)
                return (new ActionForward(page));
            else if(null != forward)
                return mapping.findForward(forward);        return mapping.findForward("login");
        }
    }
      

  4.   

    得到datasource就在action里得就可以了.DataSource dataSource = (DataSource)servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
    HttpSession session = request.getSession();
    Connection connection = null;
      

  5.   

    其实你的那些业务逻辑是不应该写在action当中的
    关于转换的问题
    FormBean fb = new FormBean(); 
    fb.setNo(rs.getString(0));
    fb.setName(rs.getString(1));
    ....
      

  6.   

    sandyen:我的目的只是取出recordset还想再做个action去显示recordset
      

  7.   

    我决定如此作:
    DLForm.javapackage classlib;import org.apache.struts.action.ActionForm;
    import java.sql.*;
    import java.io.*;public class DLForm  implements Serializable{
    private String depid;
    private String depname;
    public void setDepid(String depid){
    this.depid=depid;
    }
    public String getDepid(){
    return depid;
    }
    public void setDepname(String depname){
    this.depname=depname;
    }
    public String getDepname(){
    return depname;
    }public static DLForm load(ResultSet rs) throws SQLException{
    DLForm dlform=new DLForm();
    try{
    String strValue=null; strValue=rs.getString(0);
    if (strValue!=null)
    dlform.setDepid(strValue);
    strValue=rs.getString(1);
    if (strValue!=null)
    dlform.setDepname(strValue);
    return dlform;
    }
    catch(SQLException e){
    return null;
    }
    }
    }IndexAction.javapackage classlib;import javax.sql.DataSource;
    import java.sql.*;
    import java.util.*;public class IndexAction{
    Connection conn;
    public void setConn(Connection conn){
    this.conn=conn;
    }
    public LinkedList getDlist() throws SQLException{
    LinkedList dlist;
    if (conn==null)
    throw new SQLException("No Connection in DListAction"); PreparedStatement pstmt=null;
    ResultSet rs=null;
    dlist=new LinkedList();
    try{
    pstmt=conn.prepareStatement("select * from t_dep");
    rs=(ResultSet)pstmt.executeQuery();
    while (rs.next())
    {
    dlist.add(DLForm.load(rs));
    }
    }
    finally{
    if (rs!=null)
    rs.close();
    if (pstmt!=null)
    pstmt.close();
    }
    return dlist;
    }
    }再做一个action
    package classlib; 
    import classlib.IndexAction;
    import java.io.IOException;
    import java.util.LinkedList;
    import java.util.Locale;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionError;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionServlet;
    import org.apache.struts.util.MessageResources;
    import java.sql.Connection;
    import java.sql.SQLException;
    import javax.sql.DataSource;public class DListAction extends Action {
    Connection connection;LinkedList dlist;public ActionForward perform(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException{
    ActionErrors errors = new ActionErrors();
    try {
    DataSource dataSource =servlet.findDataSource(null);
    connection =dataSource.getConnection();dlist=new LinkedList();
    IndexAction indexaction=new IndexAction();
    indexaction.setConn(connection);
    dlist=indexaction.getDlist();if (dlist==null) {
    saveErrors(request, errors);
    return (new ActionForward("No dlist in DListAction"));
    }HttpSession session = request.getSession();
    session.setAttribute("dlisttop",dlist);} catch (SQLException sqle) {
    getServlet().log("Connection.process", sqle);
    }finally {
    if(connection!=null)
    try {
    connection.close();
    } catch (SQLException e) {
    getServlet().log("Connection.close", e);
    }
    }return (mapping.findForward("success"));
    }
    }
    但在编译IndexAction.java时出错:
    IndexAction.java:25: cannot resolve symbol
    symbol:variable DLForm
    location:class classlib.IndexAction
                               dlist.add(DLForm.load(rs));
    1  error
    不明白这是怎么回事呢?其他的xml文件基本没有问题了.只差这个编译了!
    请大侠们出手!谢
      

  8.   

    我没有具体看你的程序,不过给你一些建议.第一,config里面写好你的action和与之相对应的form Bean 的对应关系.
    一个在<action-mappings>里面
    一个在<form-beans>的描述里面另外,一个事务bean获得数据可以这样.
    在action里面调用form set给它.userBean.setForm(form)    //in your action
      

  9.   

    salx(大地翔) :现在我的程序里也有cannot resolve symbol的错误。你找出原因了吗?我也很急啊。而且我的几乎每行程序都是这个错误。一直没有解决。
      

  10.   

    现在还没有时间做,我想可能是因为没有IMPORT进DLFORM引起的,大概实例化一个dlform后应该可以解决。现在忙呀!你成功了告诉我一声原因和解决方法哦!
      

  11.   

    action里面是这样得到form的,如果你的form是注册的.(你的form的名字)form比如((DLFORM)form).getXXXXX()
    getXXXXX()是dlform里面的方法.你的每个action的perform方法开始不是这样的吗?    public ActionForward perform(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {就是在把这个form强制转换的.
    如果你自己实列一个form,那么,就是formbean而是你自己的事务bean,formbean和私有的事务bean的区别在于一个注册有对应的action bean,
    另一个则没有.