sturts分页处理

来源:互联网 发布:unity3d中UI text是 编辑:程序博客网 时间:2024/05/01 12:02
<%@ page contentType="text/html;charset=GB2312" language="java" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>


<html:html >

<head>

<title>

</title>

<META http-equiv=Content-Type content="text/html; charset=gb2312">


</head>

<body bgColor=white>

<html:base /> <html:form method="post" action="/dbListAction.do?action=insert&search=search&expression=NULL">

<table cellPadding=2 width="80%" border=2><tr><td>序号</td><td><html:text property='序号' /></td></tr>

<tr><td>姓名</td><td><html:text property='姓名' /></td></tr>

<tr><td>备注</td><td><html:text property='备注' /></td></tr>

</table>

<html:submit value="添加新记录" /> <html:reset value="重写" /> </html:form >

</body>

</html:html >



dbListDAO.java文件:

  这里定义一个DAO类,用于实现数据加的查找、删除、更新、添加及生成分页字符串等功能。getPagestr(int ipage)函数用于生成分页字符串,int ipage参数用于转递当前页码。findSQL(String sql,int ipage)函数用于查找数据库数据,String sql参数用于转递SQL查询语句字符串,int ipage参数用于转递当前页码。

。。。。。。。。。。(省略)

public class dbListDAO {


与记录添加无关的代码省略。

public void create(dbList m_dbList) throws SQLException {

PreparedStatement ps = null;

//<createSQL>

String sql = "INSERT INTO dbo.Test VALUES (?,?,?)";

//</createSQL>

try {

if (con.isClosed()) {

throw new IllegalStateException("error.unexpected");

}

//用SQL Server请加入下行:

con.setAutoCommit(true);

//SQL Server

ps = con.prepareStatement(sql);

//<create>

ps.setInt(1,m_dbList.get序号());

ps.setString(2,m_dbList.get姓名());

ps.setString(3,m_dbList.get备注());

//</create>


if (ps.executeUpdate() != 1) {

throw new SQLException ("error.create.dbList");

}

} catch (SQLException e) {


e.printStackTrace();

throw new RuntimeException("error.unexpected");


} finally {

try {

if (ps != null)

ps.close();

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException("error.unexpected");

}

}

}



省略

}


dbListAction.java文件:

。。。。。。。。。。(省略)

public final class dbListAction extends Action

{

// 变量定义:

private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");


private ConnectionPool pool;


public dbListAction() {

pool = ConnectionPool.getInstance();

}



//函数定义:

public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception

{

// Extract attributes and parameters we will need

Locale locale = getLocale(request);

MessageResources messages = getResources(request);

HttpSession session = request.getSession();

dbListForm m_dbListForm = (dbListForm) form;


Connection con = null;


try

{

con = pool.getConnection();

dbListDAO m_dbListDAO = new dbListDAO(con); //定义DAO对象,用于实现数据库的各种操作


String action=request.getParameter("action"); //动作类型

String search=request.getParameter("search"); //数据库查找方式

String expression=request.getParameter("expression"); //参数,这里用于保存记录(行)的ID数据


if(action==null)action="find";

if(search==null)search="UNsearch";

if(expression==null)expression="";

expression=toChinese(expression); //处理中文问题,实现编码转换


if("delete".equals(action)) //删除记录操作

{

m_dbListDAO.removeID(expression);

}

////////////

if("update".equals(action)||"insert".equals(action)) //更新、添加记录操作

{

dbList m_dbList= new dbList();

//<update>

m_dbList.set序号(m_dbListForm.get序号());

m_dbList.set姓名(m_dbListForm.get姓名());

m_dbList.set备注(m_dbListForm.get备注());

//</update>

if("update".equals(action)) //调用DAO对象更新记录

m_dbListDAO.update(m_dbList,expression);

if("insert".equals(action)) //调用DAO对象添加记录

m_dbListDAO.create(m_dbList);

}

///////////////查找数据库

//<sqlstr>

String sql="SELECT * FROM dbo.Test";

//</sqlstr>


////SQL字符处理

if("search".equals(search))

{

//<search>

sql+=" where ";

sql+="序号";

sql+="=";

if("NULL".equals(expression)||"".equals(expression))

{

sql+=m_dbListForm .get序号();

}

else

{

sql+=expression;

}


sql+="";

//</search>

}


//<分页>

m_dbListDAO.setLength(10);//设置每页显示记录数

int ipage;//当前页

try

{

String page=request.getParameter("page");//分页参数,读取请求的当前页

ipage=java.lang.Integer.parseInt(page,10);

}

catch (Exception e)

{

ipage=m_dbListForm.getPage();

}

if(ipage<1)ipage=1;

Collection col = m_dbListDAO.findSQL(sql,ipage);//通过DAO对象查询数据

request.setAttribute("dbList",col); //保存数据


String pagestr=m_dbListDAO.getPagestr(ipage);//分页字符串,分页内容

String s_find,str;

if(!"search".equals(search)) //查找和全部显示两个不同的操作,其分页字符串不同,在此进行处理。如果是全部显示则去掉"search=search&"

{

s_find="search=search&";

while(pagestr.indexOf(s_find)!=-1)

{

str=pagestr.substring(0,pagestr.indexOf(s_find));

str+=pagestr.substring(pagestr.indexOf(s_find)+s_find.length(),pagestr.length());

pagestr=str;

}

}

m_dbListForm.setPagestr(pagestr);

m_dbListForm.setAction(action);

request.setAttribute("dbListForm",m_dbListForm);

//</分页>



return mapping.findForward("success");

}

catch (SQLException e)

{

e.printStackTrace();

throw new RuntimeException("Unable to get connection.");

}

finally

{

try

{

if (con != null)

con.close();

}

catch (SQLException e)

{

throw new RuntimeException(e.getMessage());

}

}


}


 
原创粉丝点击