1

I am using Java and JSP to develop my web application.

There are many situations in which user input cause an exception on server side, in this situation user expects an error message to be shown. There are several ways that I (as a programmer) can handle the transfer of the error message from server to GUI(html) and maybe redirect to other pages and show the message there. One way to go is using session attribute, other can be ajax method calls and showing the response text. There may be other ways that I don't know. Would you tell me what is the most straight forward and standard way (if there is a standard say about this)?


The problem that made me think about this was when I was trying to show the response of an ajax call in a colorbox, and I failed, it would be appreciated if you give me a hint on this too, how can I pass the response text of an ajax method in other web page.

4

2 回答 2

1

将消息从服​​务器传递到 HTML 的一种方法是用 HTML 编写它们。HTML由服务器生成。

还有其他可能性,例如使用查询参数、cookie、ajax 调用等,但如果您没有特殊要求,您应该在输出 HTML 中编写消息。

例如,在服务器实现中,您可以使用请求属性。但这是服务器应用程序内部的通信,而不是 UI 和服务器之间的通信。

这些类型的消息通常应该是 a) 响应 POST 请求,您已重定向到错误页面,错误页面 url 中有相关信息,使页面可缓存或 b) 直接响应 GET 请求中的请求参数,所以相同的参数会导致相同的输出。

于 2013-04-25T14:18:20.517 回答
1

一种方法是使用会话属性,其他可以是 ajax

我能想到的第三个选项是 URL 重定向,新 URL 可能包含参数

IE。

yourdomain.com/page.jsp?name=Joe&error=Empty%20Field

我认为处理这些场景的标准方法是通过 AJAX,正如这个问题中的 OP 所做的那样。记下 JQuery ajax 方法中的错误部分

于 2013-04-25T14:13:02.900 回答