在Struts2應用程序中,我嘗試使用我的Custome結果類型。但沒有效果,我的基於JSP頁面圖像的動作沒有被調用。並沒有異常也越來越。請糾正我在哪裏做錯了。 HTTPFox說404,但沒有得到任何東西在JAVA控制檯。Struts2圖像的結果類型
HTML:
<img src=" <s:url action='ExternalImageAction' />" />
XML:
<package name="externalImage_package" extends="struts-default">
<result-types>
<result-type name="myBytesResult" class="leo.struts.CustomeImageResult" />
</result-types>
<action name="ExternalImageAction" class="leo.struts.ExternalImageAction">
<result name="myImageResult" type="myBytesResult">
</result>
</action>
</package>
的HttpFox:
00:18:06.762 0.044 432 1258 GET 404 text/html (NS_ERROR_FAILURE) http://localhost:8888/Struts2Whole/%3Cs:url%20action=%27ExternalImageAction%27%20/%3E
CustomeImageResult:
公共無效執行(ActionInvocation調用)拋出異常{
ExternalImageAction action = (ExternalImageAction) invocation.getAction();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType(action.getContentType());
response.getOutputStream().write(action.getImageInBytes());
response.getOutputStream().flush();
}
ExternalImageAction:
public String execute()
{
System.out.println("execute of the ExternalImageAction...........");
setContentType("jpg");
setImageInBytes(getFileBytes("C:/Users/Joseph.M/Desktop/ocwcd5.jpg"));
return "myImageResult";
}
public static byte[] getFileBytes(String filePath)
{
File file = new File(filePath);
System.out.println("file : "+file.getName());
byte[] b = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(b);
for (int i = 0; i < b.length; i++) {
System.out.print((char)b[i]);
}
fileInputStream.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
catch (IOException e1) {
System.out.println("Error Reading The File.");
e1.printStackTrace();
}
System.out.println("byes of image size : "+b.length);
return b;
}
發佈'CustomeImageResult'的代碼。 –
嗨@RomanC請檢查更新的code.Thnaks的答覆。 – sunleo
在使用struts標記之前是否使用了JSP taglib僞指令? –