xuanfeng6060 2015-04-24 02:14 采纳率: 100%
浏览 1835
已采纳

求struts1 中测试action类中的方法?

/** 测试类*/
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;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2015-04-24 02:45
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图