Struts 2 accessing HttpServletResponse object in Controller


        All Actions that want to have access to the servlet response object must implement ServletResponseAware interface.
        And Override void setServletResponse(javax.servlet.http.HttpServletResponse response)
        The method setServletResponse will set all the HTTP response object in implementing classes.

Code

package com;

import java.util.Locale;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport implements ServletResponseAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String msg;
	private HttpServletResponse response;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public String execute() throws Exception {
		Locale local = response.getLocale();
		setMsg("Locale information " + local.getCountry() + "\t"
				+ local.getLanguage());
		return SUCCESS;
	}
}

Screenshot
            
            

Download
    Struts 2 servletresponseaware war
    Struts 2 servletresponseaware maven zip


Related Post

Comments


©candidjava.com