webwork2+FreeMarker 製作資料列表顯示簡單一實例

來源:互聯網
上載者:User
剛剛接觸了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用這個例子做一點修改就可以具有分頁用能了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.