Java Code Examples for org.apache.struts2.ServletActionContext#getServletContext()

The following examples show how to use org.apache.struts2.ServletActionContext#getServletContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Form2Action.java    From Project with Apache License 2.0 6 votes vote down vote up
/**
 *  在 action 中操作域对象
 * @return
 * @throws Exception
 */
@Override
public String execute() throws Exception {
    // request 域
    HttpServletRequest request = ServletActionContext.getRequest();
    request.setAttribute("req", "reqValue");

    // session 域
    HttpSession session = request.getSession();
    session.setAttribute("sess", "sessValue");

    // ServletContext 域
    ServletContext servletContext = ServletActionContext.getServletContext();
    session.setAttribute("contextname", "contextValue");
    return NONE;
}
 
Example 2
Source File: MySurveyDesignAction.java    From DWSurvey with GNU Affero General Public License v3.0 4 votes vote down vote up
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();
	}
 
Example 3
Source File: JspToHtml.java    From DWSurvey with GNU Affero General Public License v3.0 4 votes vote down vote up
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();
}
 
Example 4
Source File: BaseAction.java    From csustRepo with MIT License 4 votes vote down vote up
public ServletContext getServletContext(){   
    return ServletActionContext.getServletContext();   
}
 
Example 5
Source File: BaseAction.java    From hrms with Apache License 2.0 2 votes vote down vote up
/**
 *
 * 
 * @return
 */
public ServletContext getServletContext() {
	return ServletActionContext.getServletContext();
}