避免在 Jsp 中使用 Scriptlet 在页面加载时显示数据
我意识到,当您在 jsp 中提交表单时,在映射的 servlet 中,您可以获得所需的数据,将其设置在适当的范围(例如请求)中,然后将其转发到 jsp,如下所示:
request.setAttribute("myList", myList); // Store list in request scope.
request.getRequestDispatcher("/index.jsp").forward(request, response);
但是我想知道哪些页面没有有一个表单,或者换句话说,我们希望在页面加载后立即显示数据,我们如何在不使用像这样的脚本的情况下有效地加载数据,
<%= myBean.populateData("String Argument_1")%>
如果有人可以提供任何类似的建议,我们将不胜感激。
I realize that when you submit the form in a jsp, in the mapped servlet you can get the desired data, set it in the proper scope(say request) and forward it to jsp like this:
request.setAttribute("myList", myList); // Store list in request scope.
request.getRequestDispatcher("/index.jsp").forward(request, response);
However am wondering for pages which doesn't have a form or in other words we want to display data as soon as page loads, how can we efficiently load the data without using scriptlets like
<%= myBean.populateData("String Argument_1")%>
Would highly appreciate if anyone can provide any recommendations around the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请求是否来自表单这一事实不会改变任何内容。 servlet 接收请求,然后可以进行一些处理并转发到 JSP:
The fact that the request comes from a form or not doesn't change anything. The servlet receives a request, and then can do some processing and forward to a JSP:
我认为将 EL 与 JSTL 结合使用可以在最常见的情况下为您提供帮助。如果这还不够,您可以编写 EL 函数或您自己的自定义标签。
I think using EL in combination with JSTL can help you in the most common situations. If this is not enough you can write EL functions or your own custom tags.