在jsp页面中从bean调用值的正确方法是什么?
我正在开发一个 java MVC 项目。目前我有一个 login.jsp
、profile.jsp、``CheckLogin
servlet、User.java
bean 类和 UserDAO< /code> dao 类(bean 和 jsp 以及所有其他内容也要添加)。 我目前在这里所做的,每当用户成功登录时,它就会被重定向到显示用户所有详细信息的个人资料页面。 我的问题是:
在整个session
对象中为特定userID
设置一个User
类型对象是否更好< code>session 并直接以 ${sessionScope.user.property}
形式检索所有值?
还是
设置更好只是一个userID
session
对象在整个 session
中占主导地位,并使用以下代码行检索所有页面中的值: ${user.property}
我希望我的问题对您来说很清楚。 请建议我。
I am working on a java MVC project. Currently i've a login.jsp
, profile.jsp, ``CheckLogin
servlet, User.java
bean class and UserDAO
dao class (the beans and jsp's and all other things are to be added also).
What i am currently doing here whenever the user gets logged in successfully it is redirected to the profile page which shows all the details of user.
My question here is:
Is it better to set a User
type object for particular userID
in the session
object that prevails in whole session
and retrieve all values directly as ${sessionScope.user.property}
everywhere?
or
Is it better to set just a userID
in the session
object that prevails in whole session
and retrieve the values in all pages using these lines of code :<jsp:useBean id="userDAO" scope="page" type="com.project.dao.UserDAO" />
<c:set var="user" value="<%= userDAO.getUser(%>${sessionScope.userID}<%) %>" />
${user.property}
I hope i am clear to you in my question.
Please suggest me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一条规则:避免 scriptlet。事实上,除了 scriptlet 之外没有其他解决方案,这表明存在设计问题。控制器应该为视图准备模型。视图不应直接调用 DAO。这是控制器的责任。
现在,将用户存储在会话中或在每个请求(来自控制器)时加载用户是性能和陈旧数据的问题。
如果用户仅包含在整个会话中不会更改的数据(或者可以更改,但仅从此会话中更改,允许在需要时刷新数据),则将其存储在会话中。这就是它的目标:存储具有会话范围的数据。
如果可能有一些外部会话或进程修改用户的数据,并且您希望确保始终显示最新的数据,请在每次请求时重新加载它。
First rule: avoid scriptlets. The fact that you have no other solution than scriptlets shows a design problem. The controller should prepare the model for the view. The view should not call DAOs directly. That's the responsibility of the controller.
Now, storing the user in the session or loading it at each request (from the controller) is a matter of performance and stale data.
If the user only contains data that won't change through the whole session (or that could change, but only from this session, allowing to refresh the data when needed), then store it in the session. That's its goal: store data that have a session scope.
If there might be some external session or process modifying the data of the user, and you want to make sure you always display the freshest data, then reload it at every request.