根据标志在jsp页面中显示通知?

时间:2010-05-24 14:22:36

标签: jsp jstl

我在jsp页面中有HTML代码,我希望它只在用户提交数据并重定向到此页面后显示。所以我必须在链接中设置成功标志作为参数,如:

http://site.com/page?success=true

不发送请求,因为自重定向后它将丢失 我不想把它作为参数发送到链接中,我想的是另一种方式 和想法如何发送?所以链接将是:

http://site.com/page

我可以显示HTML代码吗?

1 个答案:

答案 0 :(得分:1)

只需将其设置为请求attribtue。

if (/* form is succesfully submitted */) {
    request.setAttribute("success", true);
}
request.getRequestDispatcher("page.jsp").forward(request, response);

使用JSTL c:if标记根据EL表达式有条件地呈现内容。

<c:if test="${success}">
    Your notification here.
</c:if>

如果你想在重定向中存活下来,那么你需要将它设置为session attribtue:

if (/* form is succesfully submitted */) {
    request.getSession().setAttribute("success", true);
}
response.sendRedirect("page.jsp");

首次显示后使用c:remove将其删除,以便后续请求不会受到影响:

<c:if test="${success}">
    <c:remove var="success" scope="session" />
    Your notification here.
</c:if>