アイデア:ダウンロードやSpringのWeb内の別のコントローラのアクションにエラーメッセージをリダイレクトMVC
- がリモートからファイルをダウンロードします。私は、これらのtaksの一方または両方を達成する必要があります春のWeb MVCのアクションを持っていますサーバーを開き、入力ストリームを応答出力ストリームに書き込みます。
- ダウンロードの例外をキャッチし、複数のエラーメッセージの1つを設定し、/ addressesページにリダイレクトします。アドレスページにエラーが表示されます問題
:春は、ファイルをダウンロードし、問題が発生した場合にリダイレクトすることができない - 何とかリダイレクトで迷子になるので、属性は動作しません点滅:
/アドレスの@ResponseBody
@RequestMapping(value = "/download/{fileaddress}", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "fileaddress") String fileaddress) throws Exception
{
if(fileaddress != null && fileaddress.length() > 0)
{
try
{
// Get the remove file based on the fileaddress
RemoteFile remotefile = new RemoteFile(fileaddress);
// Set the input stream
InputStream inputstream = remotefile.getInputStream();
// Write the input stream to the output stream or throw an exception
Utils.writeTo(inputstream, response.getOutputStream());
}
catch(MyExceptionA)
{
// TODO: Define error message a and pass it to /addresses
// PROBLEM: Flash attributes that contain all critical error information don't work
response.sendRedirect(request.getContextPath() + "/addresses");
}
catch(MyExceptionB)
{
// TODO: Add another error message and redirect
response.sendRedirect(request.getContextPath() + "/addresses");
}
catch(MyExceptionC)
{
// TODO: Add another error message and redirect
response.sendRedirect(request.getContextPath() + "/addresses");
}
catch(MyExceptionN)
{
// TODO: Add another error message and redirect
response.sendRedirect(request.getContextPath() + "/addresses");
}
}
else
{
// TODO: Add error message
response.sendRedirect(request.getContextPath() + "/addresses");
}
}
JSPページ:
<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<tags:index>
<jsp:attribute name="content">
<core:if test="${not empty error}">
<div class="alert alert-danger">
<p>${error}</p>
</div>
</core:if>
<p>Page under construction!</p>
</jsp:attribute>
</tags:index>
質問:私は(エラーメッセージを表示することができていますどのように単純な文字列など)は/ addressesサイトにありますか?異なるURLパラメータ(error = errora、error = errorb ...)を使用すると、複数のエラータイプがあり、GETパラメータがプロフェッショナルではなく、エンコーディングの問題の根源であるため、エラーメッセージを渡すと大きな苦痛があります。
IOUtils.toByteArray(inputstream)がファイルからメモリにすべてを読み込むので、これが問題になる可能性があります。ファイルが大きすぎる場合、その方法は機能しません。 – Kuvaldis
私は同意しますが、ファイルをストリームする方法ではなく、 '' RedirectAttributes''についての質問です。D – Babl
メモリの問題を修正するコード例を更新しました:) – Babl