Action代码:
public class IndividualAccount extends ActionSupport {
private DayFinanceInfo this_month = null;
private DayFinanceInfo last_month = null;
private List<DayFinanceInfo> this_list = null;
private List<DayFinanceInfo> last_list = null;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
String[] time = new String[2];
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
String month = format.format(new Date());
Integer a = Integer.valueOf(month);
String last_month = String.valueOf(a-1);
time[0] = month;
time[1] = last_month;
get(time);
return SUCCESS;
}
public void get(String[] time){
this_list = new ArrayList<DayFinanceInfo>();
last_list = new ArrayList<DayFinanceInfo>();
//本月
query("this",time[0]);
//上月
query("last", time[1]);
}
/**
* 数据库执行查询操作
* @param time 月份
* */
private void query(String which,String time){
User user = null;
Map<String, Object> session = null;
String username = null;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
//获取用户信息
session = ActionContext.getContext().getSession();
user = (User) session.get("user");
username = user.getUsername();
String query = "select * from day_finance where username='" + username
+ "' and month_time='" + time + "';";
try {
connection = CreateConnection.getConnection();
statement = CreateConnection.getStatement(connection);
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
if(which.equals("this")){
this_month = new DayFinanceInfo();
this_month.setTime(resultSet.getString("datetime"));
this_month.setType(resultSet.getString("class"));
this_month.setMoney(resultSet.getString("cost"));
this_month.setContent(resultSet.getString("title"));
this_month.setRemark(resultSet.getString("remark"));
this_list.add(this_month);
}else{
last_month = new DayFinanceInfo();
last_month.setTime(resultSet.getString("datetime"));
last_month.setType(resultSet.getString("class"));
last_month.setMoney(resultSet.getString("cost"));
last_month.setContent(resultSet.getString("title"));
last_month.setRemark(resultSet.getString("remark"));
last_list.add(last_month);
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public DayFinanceInfo getThis_month() {
return this_month;
}
public void setThis_month(DayFinanceInfo this_month) {
this.this_month = this_month;
}
public List<DayFinanceInfo> getThis_list() {
return this_list;
}
public void setThis_list(List<DayFinanceInfo> this_list) {
this.this_list = this_list;
}
public List<DayFinanceInfo> getLast_list() {
return last_list;
}
public void setLast_list(List<DayFinanceInfo> last_list) {
this.last_list = last_list;
}
public DayFinanceInfo getLast_month() {
return last_month;
}
public void setLast_month(DayFinanceInfo last_month) {
this.last_month = last_month;
}
}
struts.xml配置:
/Page/Finance/zhanghu.jsp
jsp代码:
页面一加载便发送请求
var current = <%=month%>; current--; function month(){ $.post('month_account.action',{ month:<%=month%>, lastmonth:current },function(info){ }) } month();jsp界面中遍历代码:
/s:iterator