/** 測試類*/
public class TestInfoActionCreateSHTML extends MockStrutsTestCase{
private static ApplicationContext context;
@Override
public void setUp() throws Exception {
super.setUp();
System.out.println("執行 setUp()");
String[] locations = { "/WEB-INF/applicationContext.xml",
"/WEB-INF/actionServlets.xml" };
context = new ClassPathXmlApplicationContext(locations);
setConfigFile("teacher", "/WEB-INF/applicationContext.xml");
}
@Override
public void tearDown() {
System.out.println("執行 teardown()");
}
@Test
public void testCreateShtml() throws IOException, TemplateException{
/** 要測試InfoAction中的createSHTML(mapping, form, request, response);
方法,那參數怎麼傳遞?
注:InfoAction 有多個方法,配置路徑:“/info”,createSHTML()方法的返回值為null,主要測試該方法是否成功生成shtml文件
*/
}
}
/** action 類*/
public ActionForward createAllSHTML(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws TemplateModelException, IOException {
Map<String, Object> data = new HashMap<String, Object>();
JSONObject jsonObj = new JSONObject();
IInfoBIZ infoBIZ = (IInfoBIZ) getBean("infoBIZ");
int lxid = request.getParameter("lxid") == null ? 0 : Integer
.parseInt(request.getParameter("lxid").toString());
IInfoTypeBIZ infoTypeBIZ = (IInfoTypeBIZ) getBean("infoTypeBIZ");
Txxlx txxlx = infoTypeBIZ.getInfoByID(lxid);
List<Map<String, String>> pathList = new ArrayList<Map<String, String>>();
getWebPath(txxlx, infoTypeBIZ, pathList);
List list = infoBIZ.findAll3(lxid);
for (int i = 0; i < list.size(); i++) {
Txxfb txxfb = (Txxfb) list.get(i);
InfoForm infoForm = (InfoForm) this.getFvHelper().convertV2F(txxfb);
//設置參數
data.put("infoForm" + txxfb.getXxid(), infoForm);
data.put("pathList", pathList);//網頁路徑
// 設置生成SHTML路徑
String shtmlPath0=request.getRealPath("")+"/";
String shtmlPath =shtmlPath0 + txxlx.getLxml()
+ "/info/infoname" + txxfb.getXxid() + ".shtml";
// 設置模板路徑
String templatePath = "templat/data.html";
try {
FreeMarkerUtil.createSHTML(request.getSession()
.getServletContext(), data, templatePath, shtmlPath);
} catch (TemplateException e) {
e.printStackTrace();
}
}
jsonObj.put("validateFlag", "1"); // 用這個判斷是否要刷新
jsonObj.put("message", "成功生成" + list.size() + "個SHTML頁面");
try {
response.getWriter().print(jsonObj);
} catch (IOException e1) {
System.out.println(e1.getMessage());
} catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
http://www.cnblogs.com/ITEagle/archive/2010/04/17/1714118.html