当前位置: 首页>>代码示例>>Java>>正文


Java ServletActionContext.getServletContext方法代码示例

本文整理汇总了Java中org.apache.struts2.ServletActionContext.getServletContext方法的典型用法代码示例。如果您正苦于以下问题:Java ServletActionContext.getServletContext方法的具体用法?Java ServletActionContext.getServletContext怎么用?Java ServletActionContext.getServletContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.struts2.ServletActionContext的用法示例。


在下文中一共展示了ServletActionContext.getServletContext方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: buildSurveyHtml

import org.apache.struts2.ServletActionContext; //导入方法依赖的package包/类
private void buildSurveyHtml() throws Exception{
		HttpServletRequest request=Struts2Utils.getRequest();
		HttpServletResponse response=Struts2Utils.getResponse();
		String url = "";
		String name = "";
		ServletContext sc = ServletActionContext.getServletContext();

		String file_name = request.getParameter("file_name");
		url = "/design/my-collect.action?surveyId=402880ea4675ac62014675ac7b3a0000";
		// 这是生成的html文件名,如index.htm.
		name = "/survey.htm";
		name = sc.getRealPath(name);
		
		RequestDispatcher rd = sc.getRequestDispatcher(url);
		final ByteArrayOutputStream os = new ByteArrayOutputStream();

		final ServletOutputStream stream = new ServletOutputStream() {
			public void write(byte[] data, int offset, int length) {
				os.write(data, offset, length);
			}

			public void write(int b) throws IOException {
				os.write(b);
			}
		};
		
		final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"utf-8"));

		HttpServletResponse rep = new HttpServletResponseWrapper(response) {
			public ServletOutputStream getOutputStream() {
				return stream;
			}

			public PrintWriter getWriter() {
				return pw;
			}
		};

//		rd.include(request, rep);
		rd.forward(request,rep);
		pw.flush();
		
		// 把jsp输出的内容写到xxx.htm
		File file = new File(name);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream fos = new FileOutputStream(file);
		
		os.writeTo(fos);
		fos.close();
	}
 
开发者ID:wkeyuan,项目名称:DWSurvey,代码行数:53,代码来源:MySurveyDesignAction.java

示例2: jspWriteToHtml

import org.apache.struts2.ServletActionContext; //导入方法依赖的package包/类
public void jspWriteToHtml(String url, String filePath,String fileName) throws Exception {
	HttpServletRequest request = Struts2Utils.getRequest();
	HttpServletResponse response = Struts2Utils.getResponse();
	ServletContext sc = ServletActionContext.getServletContext();
	url = "/my-survey-design!previewDev.action?surveyId=402880ea4675ac62014675ac7b3a0000";
	// 这是生成的html文件名,如index.htm
	filePath = filePath.replace("/", File.separator);
	filePath = filePath.replace("\\", File.separator);
	String fileRealPath = sc.getRealPath("/") + filePath;
	RequestDispatcher rd = sc.getRequestDispatcher(url);
	final ByteArrayOutputStream os = new ByteArrayOutputStream();

	final ServletOutputStream stream = new ServletOutputStream() {
		public void write(byte[] data, int offset, int length) {
			os.write(data, offset, length);
		}

		public void write(int b) throws IOException {
			os.write(b);
		}
	};

	final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,
			"UTF-8"));

	HttpServletResponse rep = new HttpServletResponseWrapper(response) {
		public ServletOutputStream getOutputStream() {
			return stream;
		}

		public PrintWriter getWriter() {
			return pw;
		}
	};

	rd.forward(request, response);
	pw.flush();

	File file2 = new File(fileRealPath);
	if (!file2.exists() || !file2.isDirectory()) {
		file2.mkdirs();
	}
	File file = new File(fileRealPath + fileName);
	if (!file.exists()) {
		file.createNewFile();
	}
	FileOutputStream fos = new FileOutputStream(file);
	os.writeTo(fos);
	fos.close();
}
 
开发者ID:wkeyuan,项目名称:DWSurvey,代码行数:51,代码来源:JspToHtml.java


注:本文中的org.apache.struts2.ServletActionContext.getServletContext方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。