當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。