Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment:

Update formatting

To display an image represented as an array of bytes

  • Reference an Action for the image src attribute
  • Provide a helper method on the Action to return an array of bytes
  • Provide a Result Type that renders the array to the response

Reference an Action

MyAction.jspWith the html as follows:

Code Block
xml
xml
   <img src="/myWebAppContext/myAction!default.actiondo" />

xwork.xml could be as follows:

Provide Helper Methods

MyAction.java

Code Block

the action could be as follows:
{code:java}
public class MyAction extends ActionSupport {
  public String doDefault() {
    return "myImageResult";
  }
  
  public byte[] getMyImageInBytes() { .... }

  public String getMyContentType() { ... }
  public String getMyContentDisposition() { ... }
  public int getMyContentLength() { .... }
  public int getMyBufferSize() { ... }

}

Provide a Custom Result Type

action.xml

Code Block
xml
xml
<xwork>

    ...
    <result-types>
        <result-type name="myBytesResult" class="foo.bar.MyBytesResult" />
    </result-types>
    ...

    <action name="myAction" class="...MyAction">
        <result name="myImageResult" type="myBytesResult">
            <param name="contentType">${myContentType}</param>
            <param name="contentDisposition">${myContentDisposition}</param>
            <param name="contentLength">${myContentLength}</param>
            <param name="bufferSize">${myBufferSize}</param>
        <result>
    </action>

    ...

</xwork>

the action could be as follows:

MyBytesResult.java

Code Block
javajava

public class MyAction extends ActionSupport {
   public String doDefault() {
        return "myImageResult";
    }
   public byte[] getMyImageInBytes() { .... }

   public String getMyContentType() { ... }
   public String getMyContentDisposition() { ... }
   public int getMyContentLength() { .... }
   public int getMyBufferSize() { ... }
}

Code Block
java
java
public class MyBytesResult implements Result {

	public void execute(ActionInvocation invocation) throws Exception {

		MyAction action = (MyAction) invocation.getAction();
		HttpServletResponse response = ServletActionContext.getResponse();
		
                response.setContentType(action.getContentType());
		response.setContentLength(action.getContentLength());
		
		response.getOutputStream().write(action.getImageInBytes());
		response.getOutputStream().flush();
	}

}