jsp页面参数稳定性
我正在开发 JSP-servlet 应用程序,现在正在编写一个编辑有关注册商信息的页面。
情况是,当我加载页面时,我将表单 servlet 发送到 JSP 页面。 ArrayList
包含注册商所属组的信息,ArrayList
是执行多个SQL语句后的结果。当用户尝试编辑某些字段并将一个必填字段设为空并提交表单时,Servlet 会进行验证并向编辑页面返回错误。
我面临的问题是,我第一次发送的所有组都在天上飞。因此,我必须再次连接到数据库并进行多个查询以再次获取组并将其发送回 JSP 页面。 还有另一种简单的方法可以使 arrayList 在 JSP 页面中稳定吗?
编辑 这是我在会话中设置 ArrayList 范围的代码。
<c:set var="userGroups" value="${userGroups}" scope="session"></c:set>
I am working on JSP-servlet application and now I am programming a page which edit information about registrars.
The senario is that there's an ArrayList
I send form servlet to JSP page when I load the page. The ArrayList
contains information about groups the registrar belongs, the ArrayList
is resulted after making multiple SQL statements. When user try to edit some fields and make one required field empty and submit the form, the servlet makes validation and return error to edit page.
The problem I face is that all the groups I sent in the first time fly in the sky. So I have to make connection to DB again and make multiple queries to get the groups again and send it back to JSP page.
Is there's another simple way to make arrayList stable in JSP page ?
EDIT
Here's the code which I make the scope of the ArrayList
in the session.
<c:set var="userGroups" value="${userGroups}" scope="session"></c:set>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要么将其存储在会话中,要么就接受它。我真的不认为这有什么问题。如果具体问题是您必须再次复制粘贴相同的代码,或者整个代码在 Servlet 类中很难看,那么只需将其重构/隐藏到可用的 DAO 类中,然后在 Servlet 中导入/调用/重用该类即可通常的Java方式。
更新:根据您的更新,这没有意义。您只需将 servlet 代码从 更改为
您
不需要
。Either store it in the session or just live with it. I really don't see any issues with that. If the concrete problem is that you have to copypaste the same code again or that the whole code is ugly to have in a Servlet class, then just refactor/hide that into an useable DAO class which you import/call/reuse in the Servlet the usual Java way.
Update: as per your update, this doesn't make sense. You just need to change your servlet code from
to
You don't need
<c:set>
for this.