gpt4 book ai didi

spring - 使用@ResourceMapping 时显示带有错误消息的最后一页

转载 作者:行者123 更新时间:2023-12-01 14:45:36 26 4
gpt4 key购买 nike

我有一个只有一个按钮的页面(为简单起见)。该按钮要求服务器生成一个文件并返回它。这工作顺利。当服务器在生成文件时出现问题(无论出于何种原因),而不是返回一个空文件,什么也不会发生。这会导致页面为空白(与之前一样,它是我下载文件时所在的最后一页)。

如何从该函数将错误消息发送回上一页(暂时不使用 AJAX)?包含错误消息的空白页面不是我要找的。

controller.java

@ResourceMapping
public void getFile(ResourceRequest request, ResourceResponse response) {
String zipName = "myfile.zip";

try {
//Do something that might throw an error

response.setProperty("Content-Disposition", "attachment; filename=" + zipName);
response.setContentType("application/zip");
//Write to output stream here
} catch(Exception e) {
myLogger.error("Error from throwable: " + e.getMessage());

//Send error message to user on the last page without getting a blank screen
}

view.jsp

${lastError}
<form method="post" action="<portlet:resourceURL/>">
<input type="checkbox" name="Modify File" value="true" />
<input type="submit" value="Get File" />
</form>

最佳答案

你得到的是空白页面,因为你没有输入任何内容进行响应。

在创建 ResourceRequest 时,您的响应始终只是您放入 ResorceResponse 中的内容,您永远不会得到门户网站的页面作为响应(甚至不是它的一部分)。

所以你想做的事情,就像你现在正在尝试的那样,是不可能的。你必须改变你的方法。

最简单(不是最好)的解决方法是

<form method="post" action="<portlet:resourceURL/>" target="_blank">
<input type="checkbox" name="Modify File" value="true" />
<input type="submit" value="Get File" />
</form>

注意 target="_blank"

你的 Controller ......

@ResourceMapping
public void getFile(ResourceRequest request, ResourceResponse response) {
String zipName = "myfile.zip";

try {

response.setProperty("Content-Disposition", "attachment; filename=" + zipName);
response.setContentType("application/zip");

//Write to output stream here

} catch(Exception e) {
myLogger.error("Error from throwable: " + e.getMessage());
// write the error message to the response stream here.
response.setContentType("text/plain");
response.getWriter().print("last error");
}
}

通过这种方式,您可以在新窗口中获取文件或错误信息,同时让您的门户页面保持原样。

其他选项:AJAX 或您的响应可以是带有 javascript 的 html,它向您的门户页面发出请求,并带有额外的错误参数,...

关于spring - 使用@ResourceMapping 时显示带有错误消息的最后一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9592481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com