项目规范:JSF 2.1 H2 Database Embedded Jetty Web Server Embedded
问题:我有一个支持 bean “AverageBPSCalc”,它有一个托管 bean “Frames”列表。frame 的 getter 方法检索数据库值并返回使用 dataTable 标记访问的 Frames 对象列表。框架的 getter 方法被调用三次,这不必要地浪费了连接池中的数据库连接。
代码:
支持豆
package com.tutorial;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedProperty;
public class AverageBPSCalc
{
private Date fromDt;
private Date toDt;
@ManagedProperty("#{dbConnector}")
private DbConnector dbConnector;
private List<Frames> frames;
public Date getFromDt()
{
return fromDt;
}
public void setFromDt(Date fromDt)
{
this.fromDt = fromDt;
}
public Date getToDt()
{
return toDt;
}
public void setToDt(Date toDt)
{
this.toDt = toDt;
}
public List<Frames> getFrames()
{
System.out.println("In GET avg bps method");
Connection con = null;
Statement st = null;
ResultSet rs = null;
DateFormat dateMod = new SimpleDateFormat("M/d/yyyy");
DateFormat timeMod = new SimpleDateFormat("HH:mm:ss,SSS");
try
{
List<Frames> frames = new ArrayList<Frames>();
String avgBPSquery = "select CLKRX_DATE,CLKRX_TIME,bps_avg from frames where clkrx_date between '" + dateMod.format(fromDt) + "' and '" + dateMod.format(toDt)
+ "' and CLKRX_TIME between '" + timeMod.format(fromDt) + "' and '" + timeMod.format(toDt) + "'";
con = dbConnector.getCon();
System.out.println("Active Connection :::: "+con);
st = con.createStatement();
rs = st.executeQuery(avgBPSquery);
System.out.println("Query executing : "+avgBPSquery);
while (rs.next())
{
Frames frame = new Frames();
frame.setRecDate(rs.getString(1));
frame.setRectime(rs.getString(2));
frame.setBpsAvg(rs.getDouble(3));
frames.add(frame);
}
this.frames = frames;
rs.close();
st.close();
con.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
return frames;
}
public void setFrames(List<Frames> frames)
{
System.out.println("In SET avg bps method");
this.frames = frames;
}
public DbConnector getDbConnector()
{
return dbConnector;
}
public void setDbConnector(DbConnector dbConnector)
{
this.dbConnector = dbConnector;
}
}
结果视图
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/WEB-INF/templates/basetemplate.xhtml">
<ui:define name="content">
<h:head>
<script type="text/javascript" src="raphael-min.js"></script>
<script src="g.raphael.js" type="text/javascript"></script>
<script type="text/javascript" src="g.line.js"></script>
<script type="text/javascript" charset="utf-8">
/* Graph Logic goes here. */
window.onload = function() {
var r = Raphael("holder"), txtattr = {
font : "12px 'Fontin Sans', Fontin-Sans, sans-serif"
};
r.text(160, 100, "Average BPS").attr(txtattr);
var x = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var y = new Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
/* r.linechart(160,130,100,100,x,y,{smooth: true, colors: ['#F00'], symbol: 'circle'}); */
r.linechart(160, 130, 200, 200, [ 1, 2, 3, 4, 5 ], [
[ 1, 2, 3, 4, 5 ], [ 1, 3, 9, 16, 25 ],
[ 100, 50, 25, 12, 6 ] ], {
smooth : true,
colors : [ '#F00', '#0F0', '#FF0' ],
symbol : 'circle',
axis : "0 0 1 1",
axisxstep : "10",
axisystep : "10"
});
};
</script>
<h:outputStylesheet library="css" name="css/table-style.css" />
</h:head>
<h:body>
<h:outputLabel value="Selected From Date: #{avgBn.fromDt}"></h:outputLabel>
<br></br>
<h:outputLabel value="Selected To Date: #{avgBn.toDt}"></h:outputLabel>
<br></br>
<h:dataTable value="#{avgBn.frames}" var="fm">
<h:column>
<f:facet name="header">Recorded Date</f:facet>
#{fm.recDate}</h:column>
<h:column>
<f:facet name="header">Recorded Time</f:facet>
#{fm.rectime}</h:column>
<h:column>
<f:facet name="header">Average BPS</f:facet>
#{fm.bpsAvg}</h:column>
</h:dataTable>
<h:panelGroup id="holder" layout="block">
</h:panelGroup>
</h:body>
</ui:define>
</ui:composition>
</html>
Frames Managed Bean: 包com.tutorial;
public class Frames
{
private String recDate;
private String rectime;
private Double bpsAvg;
public String getRecDate()
{
return recDate;
}
public void setRecDate(String recDate)
{
this.recDate = recDate;
}
public String getRectime()
{
return rectime;
}
public void setRectime(String rectime)
{
this.rectime = rectime;
}
public Double getBpsAvg()
{
return bpsAvg;
}
public void setBpsAvg(Double bpsAvg)
{
this.bpsAvg = bpsAvg;
}
}