} else {
log.error("No file name specified.");
}
// Store the File contents and name in the Request
request.setAttribute("fileName", fileName);
request.setAttribute("fileContents", fileContents.toString());
return mapping.findForward("success");
}
}
分析:
String fileName = mapping.getParameter();
其中mapping 是ActionMapping 对象,是ActionConfig的子对象。 其中ActionConfig封装了Struts-config.xml 中的配置信息。
inputStream input = servlet.getServletContext().getResourceAsStream(fileName);
每个Web应用程序都是一个独立的Servlet容器,每个Web应用程序分别用一个ServletContext对象。ServletContext对象包含在ServletConfig对象中,
调用ServletConfig.getServletContext()方法获取ServletContext对象。
1、 getResourcePath 返回一个包含该目录和文件路径名称的Set集合
2、 getResource 返回映射到资源上的URL对象。
3、 getResourceAsStream 返回连接到某资源上的InputStream对象
InputStreamReader inputReader = new InputStreamReader(input);
需要重新包装成字符处理。
【2】配置文件
type="org.apache.struts.webapp.validator.ShowFileAction"
scope="request"
parameter="/WEB-INF/validator/validation.xml">
通过传递不同的parameter,读取不同的文件。
【3】在JSP页面 读取信息
type="org.apache.struts.webapp.validator.ShowFileAction"
scope="request"
parameter="/WEB-INF/validator/validation.xml">
bean:write 标签 有个filter属性。如果为true 的话,则表示
将把输出内容中的特殊HTML符号作为普通字符串来显示;如果filter属性为false,则不会把输出内容中的HTML符号转化为普通字符串.
考试大温馨提示:本内容来源于网络,仅代表作者个人观点,与本站立场无关,仅供您学习交流使用。其中可能有部分文章经过多次转载而造成文章内容缺失、错误或文章作者不详等问题,请您谅解。如有侵犯您的权利,请联系我们,本站会立即予以处理。