`
langgufu
  • 浏览: 2292243 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

填报监听类前后使用的实际用例(转载自润乾官网)

阅读更多

填报监听类前后使用的实际用例

第25章 填报监听类前后使用的实际用例 


1. 问题概述 

A:客户要在填报监听类中校验用户是否登陆。填报监听类中校验出错了就会抛出一个异常,返回后页面填入的数据就会丢失。客户希望像普通的单元格校验一样弹出一个警告窗口而不是跳到错误页面,页面的数据不会丢失。如果校验通过则提示数据保存成功。 
B:在填报提交后处理方法里,业务处理是需要判断哪些记录的数据有修改,哪些没有修改,有修改的才需要写入日志文件。


2. 案例 
南京世纪恒捷科技信息有限公司


3. 实现原理 
A:在填报监听类中写一个servlet类用来向页面输出JS代码弹出提示窗口。在校验未通过的情况下,执行这个doGet方法,并且抛出异常中止程序继续运行。在jsp页面中,写一个<iframe>放在隐藏的div中引入报表的标签中加入属性submitTarget=”test” (test是iframe 的id号)让页面保持填写后的状态,弹出个新的窗口(成功的页面或者是错误页面)在iframe中(隐藏了)。 
B:需要调用API接口获得单元格的原值和新录入的值,并进行比较.然后生成日志文件 

4. 流程步骤 
1.发布应用,访问报表路径(不带用户名): 
http://localhost:9090/Report4Test/jsp/ceshi/3.3.jsp 
2.修改数据,点击保存,会提示:警告:校验未通过,此次提交将不能执行! 


3.后台校验出错信息打印


 

4. 访问报表路径(带用户名): 
http://localhost:9090/Report4Test/jsp/ceshi/3.3.jsp?userName=zHouHuiHui


5.数据保存之后,只对修改过的值,C盘生成日志文件: 
 

5. 程序代码 

1.填报监听类InputListener.java代码: 

package api;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.text.SimpleDateFormat;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import com.runqian.base4.util.DBTypes;

import com.runqian.report4.usermodel.DataSourceConfig;

import com.runqian.report4.usermodel.INormalCell;

import com.runqian.report4.usermodel.input.AbstractInputListener;

import com.runqian.report4.usermodel.input.InputSQL;

public class InputListener extends AbstractInputListener {

 

SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

static PrintWriter out = null;

 

/**

* 数据保存前调用的方法

* @throws Exception

*/

public void beforeSave() throws Exception {

HttpSession session = request.getSession();

String username = (String) session.getAttribute(“userName”);

if (username == null) {

Cservlet ds = new Cservlet();

ds.doGet(request, response); // 执行这个方法弹出提示框(方法在下面定义)

throw new Exception(校验未通过!); // 终止程序继续执行。

}

}

public class Cservlet extends HttpServlet {

private static final long serialVersionUID = 1L;

 

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws IOException, ServletException {

// request.getRequestDispatcher(“jsp/aa.jsp”).forward(request,response);

//跳转到其他页面,可能被当作弹出窗口拦截

res.setContentType(“text/html”);

PrintWriter out = res.getWriter();

out.println(“<body onload=javascript:alert(‘警告:校验未通过,此次提交将不能执行!‘)>”);

out.close();

}

}

/**

* 数据保存后调用方法

*/

public void afterSave() throws Exception {

// 获取用户录入的单元格值(包括非录入的隐藏格)

int colNum = report.getColCount();

int rowNum = report.getRowCount();

for (int i = 1; i < rowNum; i++) {

for (short j = 1; j < colNum; j++) {

INormalCell iCell = report.getCell(i, j);// 取得各单元格

// 只有存在填报属性对象的单元格,才有可能可写,可写,才可能填报前后的值不一样

if (iCell.getInputProperty() != null) {

Object value = iCell.getValue();

Object value2 = iCell.getInputValue();

if(!value.equals(value2)){

// InputSQL sql2 = (InputSQL)sqls.get(j);

//保存数据时生成的InputSQL对象列表,得到表名

String tableName = ((InputSQL) sqls.get(j)).getTableName();

log(日期:[" + format.format(new java.util.Date()) + "]表名:["

+ tableName + "]–[" + i + "]行第[" + j

+ "]原值是:[" + value + "]填报后的值是:[" + value2 + "]“);

}

}

}

}

// 以下代码获取系统正在使用的Connection对象,如果您使用了这个对象,请不要调用它的close方法,

// 系统会自己调用commitclose方法

this.getConnection(context.getDefDataSourceName());

}

 

/**

* 取指定的数据源

* @param 数据源名

* @return 数据源

* @throws Exception

*/

private Connection getConnection2(String dataSource) throws Exception {

if (dataSource == null || “”.equals(dataSource))

// 得到默认的系统连接

return context.getConnectionFactory(context.getDefDataSourceName())

.getConnection();

 

return context.getConnectionFactory(dataSource).getConnection();

}

 

private void setDefaultConnection2(String dataSource, Connection con) {

// 设置数据源,动态改变连接参数

context.setDefDataSourceName(dataSource);

// 设置数据连接,也可以设置连接池工厂,cxt.setConnectionFactory(sourceName,connectionFactory)

// 其中connectionFactory这个类必须implements IConnectionFactory,并且实现他的public

// java.sql.Connection getConnection() throws Exception 方法

context.setConnection(dataSource, con);

DataSourceConfig dsoc = new DataSourceConfig(DBTypes.UNKNOWN, true,

“GBK”, “GBK”, false);

context.setDataSourceConfig(dataSource, dsoc);

 

 

}

//日志保存路径

private void log(String info) throws Exception {

if (out == null) {

File logf = new File(“c:/runqianReport.log”);

if (logf.exists()) {

File lastlogf = new File(“c:/runqianReport”

+ format.format(new java.util.Date()) + “.log”);

logf.renameTo(lastlogf);

}

out = new PrintWriter(new FileOutputStream(logf));

}

out.println(info);

out.flush();

out.close();

}

 

}

2.jsp的关键性代码:

<%

String userName = request.getParameter(“userName”);//获取用户传过来的用户名

session.setAttribute(“userName”,userName);

%>

<body>

 

<report:html name=“report1″ reportFileName=“/ceshi/3.3.raq”

funcBarLocation=“top” needPageMark=“yes”

inputListener=“api.InputListener” insertRowLabel=<%=insertImage%>

appendRowLabel=<%=appendImage%>

deleteRowLabel=<%=deleteImage%>needSaveAsWord=“yes”

needSaveAsPdf=“yes” needImportExcel=“yes” wordLabel=<%=wordImage%>

pdfLabel=<%=pdfImage%>submit=<%=submitImage%>

importExcelLabel=<%=importExcelImage%>

submitTarget=“test”/>

 

<div style=“display: none”>

<iframe name=“test” id=“test”>

</iframe>

</div>

 

</body>

<br>

<font color=“red” size=“2″>保存之后日志文件生成在C:/runqianReport.log</font>

</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics