使用mvc方式编写一个模块,但有点不明白的地方!

03-01-27 salx

我的模块是不需要提交表单信息的,只要求显示数据库的内容。我做的几个文件:

DLForm.java(Form bean)

package classlib;

import org.apache.struts.action.ActionForm;

public class DLForm extends ActionForm{

private String depid;

private String depname;

public void setDepid(String depid){

this.depid=depid;

}

public String getDepid(){

return this.depid;

}

public void setDepname(String depname){

this.depname=depname;

}

public String getDepname(){

return this.depname;

}

}

我不知道action该如何做?我做了一个indexAction.java目的是取得datasource(datasource在struts-config.xml里已经声明)

package classlib;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import javax.servlet.ServletException;

import org.apache.struts.action.*;

import javax.sql.DataSource;

import java.sql.*;

public class IndexAction extends Action{

public IndexAction(){

}

private Statement init(){

try

{

DataSource dataSource=null;

Connection Conn=null;

dataSource=(DataSource)(getServlet().getServletContext().getAttribute(Action.DATA_SOURCE_KEY));

Conn=dataSource.getConnection();

Statement Stat=Conn.createStatement();

return Stat;

}

catch(Exception e)

{

return null;

}

}

public ResultSet Query(){

try{

ResultSet Rst=null;

Statement Stat=init();

Rst=Stat.executeQuery("select * from t_dep");

return Rst;

}

catch(Exception e){

return null;

}

}

public ActionForward execute(ActionMapping mapping){

return (mapping.findForward("ok"));

}

}

怎样利用这个datasource 在我的jsp页面中显示我的内容呢(按表单格式) 是不是要利用logic标签,请简单介绍一下!

jxb8901
2003-02-08 18:33

首先要实现Action类的execute方法:

    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws java.lang.Exception
    {
            ...
            DLForm fm = (DLForm)form;
            fm.setXxx = ...
            return mapping.findForward("success");
    }
<p>

然后配置struts-config.xml