webwork2+FreeMarker 製作數據列表顯示簡單實例

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- webwork2+FreeMarker 製作數據列表顯示簡單實例 (http://www.webasp.net/article/23/22126.htm)
-- 作者:未知
-- 發佈日期: 2005-04-30

剛剛接觸了FreeMarker覺得不錯

做了個小例子

首先建立個數據表裡面有username,password兩個字段自己隨便加幾條數據建立一個webwork  action    TestAction.java在xwork.xml裡加入如下內容----------------------------------------     <action name="test" class="com.action.TestAction">      <result name="success" type="freemarker">/test.flt</result>    </action> ----------------------------------------TestAction.java代碼如下--------------------------------------------------package com.action;

import com.opensymphony.xwork.ActionSupport;import java.util.*;import com.ResultGather;

public class TestAction extends ActionSupport { private List lis; private ResultGather rs; public List getLis() {  return this.lis; } public String execute() throws Exception  {  rs = new ResultGather();  lis = rs.selectRS("select * from userbasedatum");//數據結果我是用List,HashMap封裝的,HashMap存的是單條記錄  return SUCCESS; }}

-------------------------------------------------------------------------------------------------------------------ResultGather.java代碼如下----------------------------------------------------------package com;

import conn.DBConnManager;//數據庫連接池import java.sql.*;import java.util.*;

public class ResultGather { private String sql; public ResultGather() {  } public ResultGather(String sqlcom) {  this.sql=sqlcom; } public List selectRS(String sqlStr) {  this.sql = sqlStr;  return selectRS();  } public List selectRS() //數據結果在這裡裝入List和Map {  List rsall = new ArrayList();  DBConnManager conn = null;  Connection  con = null;  Statement stmt = null;  ResultSet rs = null;  Map rsTree;  try{   conn = DBConnManager.getInstance();   con = conn.getConnection("mssql");   stmt = con.createStatement();   rs = stmt.executeQuery(sql);   ResultSetMetaData rsmd = rs.getMetaData();   int numberOfColumns = rsmd.getColumnCount();   while(rs.next())   {   rsTree = new HashMap(numberOfColumns);    for(int r=1;r<numberOfColumns+1;r++)    {     rsTree.put(rsmd.getColumnName(r),rs.getObject(r));          }    rsall.add(rsTree);   }     }catch(java.lang.Exception ex){   ex.printStackTrace();  }finally{   try{    if(rs!=null)     rs.close();    if(stmt!=null)     stmt.close();    if(conn!=null)     conn.releaseConnection("mssql",con);   }catch(Exception e){    System.out.println(e);   }   }  return rsall; } }-----------------------------------------------------------------------------------------------test.flt代碼如下-----------------------------------------------------------------------------------------<#list lis as x>${x.username},${x.password}<br></#list>     //freemarker用法網上有相關介紹-------------------------------------------------------------------------------------

OK 完成了 沒有用到jsp<%.....%>和jsptag用這個例子做一點修改就可以具有分頁用能了

webasp.net