struts2应用程序中的会话
我创建了一个网络应用程序,我需要在其中维护会话 如果存在用户会话,那么也只有这样,它才会允许用户看到该 jsp。
我以前使用过 jsp servlet,但我对 struts2 很陌生。
这里我在我的操作类中设置用户名:
修改后的代码
private HttpSession session;
public void setSession(HttpSession session) {
// TODO Auto-generated method stub0
this.session = session;
}
public HttpSession getSession() {
return session;
}
public String getLoginStatus(){
session = request.getSession();
session.setAttribute("userName", loginBean.getUsername());
return SUCCESS;
}
现在,当我在操作后重定向到下一页时,它会显示一次会话值。之后,在每个页面上,我都在会话中找到空值。
<%
String userName = (String)session.getAttribute("userName");
System.out.println(userName);
if(userName == null || userName.equals("") ){
response.sendRedirect("login.jsp");
}
%>
我在某处读到,操作类会话的范围仅限于一页 - 我该如何解决这个问题?
任何例子都会对我很有帮助。
I have created a web application in which I need to maintain the session
if a user session is there, and then and only then will it allow the user to see the jsp.
I have worked with jsp servlets before, but I'm new to struts2.
Here I am setting username in my action class:
Revised Code
private HttpSession session;
public void setSession(HttpSession session) {
// TODO Auto-generated method stub0
this.session = session;
}
public HttpSession getSession() {
return session;
}
public String getLoginStatus(){
session = request.getSession();
session.setAttribute("userName", loginBean.getUsername());
return SUCCESS;
}
Now, when I am redirected to next page after an action, it shows the session value once. After that, on every page, I am finding null values in session.
<%
String userName = (String)session.getAttribute("userName");
System.out.println(userName);
if(userName == null || userName.equals("") ){
response.sendRedirect("login.jsp");
}
%>
I read somewhere that the scope of an action class session is limited to one page - how could I solve this problem?
Any example will be very helpful to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您当前的代码存在一些问题。
登录示例
下面是一些使用 Struts2 框架创建基本登录系统的示例代码。
需要登录
这部分是可选的,但一般来说,并非 Web 应用程序中的所有页面都需要用户登录。因此,让我们创建一个名为
LoginRequired
的接口。如果用户尚未登录,任何实现此标记接口的操作都将重定向到登录页面。注意:如果您愿意,您可以使用注释,但在本示例中我将使用该接口。
拦截器 拦截器
将处理强制用户登录以执行实现
LoginRequired
接口的任何请求的操作。您还需要一个用于显示和处理登录页面的
LoginAction
以及一个用于使会话无效或清除的LogoutAction
。配置
您需要将拦截器添加到堆栈中,并为“loginRedirect”创建全局结果映射。
There are a few problems with the code you currently have.
SessionAware
interface in your action unless you need access to the session inside of the action itself.Login Example
Below is some example code for creating a basic login system using the Struts2 framework.
Login Required
This part is optional, but in general, not all pages in a web application will require the user to be logged in. Therefore, let's create an interface called
LoginRequired
. Any action that implements this marker interface will redirect to the login page if the user is not already logged in.Note: You can use an annotation instead, if you prefer, but for this example I will use the interface.
The Interceptor
The interceptor will handle forcing the user to login for any requested action which implements the
LoginRequired
interface.You will also need a
LoginAction
which displays and processes the login page and aLogoutAction
which invalidates or clears the session.The Configuration
You will need to add the interceptor to your stack and also create a global result mapping for "loginRedirect".
要维护会话,请在操作类中使用 SessionAware 接口并实现 int
public void setSession(Map m) 它将把属性作为map中的键值对
可以从任何地方访问,只需购买并检索密钥即可。
例如 Action 类
来源:http://www.java4s.com/struts-tutorials/example-on-struts-2-sessionaware-interface/
To maintain a session use SessionAware interface in your action class and implement int
public void setSession(Map m) it will take the attribute as a key value pair in map
which can be be access from any where just buy retrieving the key.
for example Action class
SOURCE: http://www.java4s.com/struts-tutorials/example-on-struts-2-sessionaware-interface/
我发现没有理由使用这种
Map
来授权用户。如果您的要求只是针对会话验证用户,那么我建议使用Filter
而不是某些类中的某些映射,这种映射的问题是一旦会话失效,就删除会话对象,当然,您可以使用HttpSessionListener
来使其工作,但我想最好通过Filter
进行验证,而不是通过Map
进行验证。除此之外,您还可以查看许多安全框架(例如 Apache Shiro),以使您的任务更简单、更高效强壮的。
I find no reason to use this kind of
Map
to authorize user. If your requirement is only validate the user against session then I would recommend to useFilter
than some map in some class, the problem with this kind of map is to remove the session objects once session is invalidated, of course you can useHttpSessionListener
to make it work but I guess it's best to validate viaFilter
thanMap
.Apart from it you can take a look at many security framework (like Apache Shiro) to make your task simpler and more robust.
您的 Action 类已经实现了
SessionAware
接口吗?编辑:
尝试一下(这是一个 struts2 风格的解决方案):
然后使用您的 jsp 代码获取页面上的用户名。我已经在我的机器上测试了这种方法并且它有效。
编辑2:
顺便说一句,这样的登录检查可以通过 Struts2 框架提供的“Interceptor”轻松而优雅地完成。
Has your Action class already implemented the
SessionAware
interface?EDIT:
Try this(It's a struts2 style solution):
Then use your jsp code to get the userName on page. I've tested this approach on my machine and it works.
EDIT2:
BTW, such login check can be done easily and elegant with "Interceptor" provided by Struts2 Framework.
不需要 SessionAware 实现。
SessionAware implemention not required.
下面的journaldev文章在上面史蒂文回复的同一行中提供了更多详细信息 http://www.journaldev.com/2210/struts-2-interceptor-tutorial-with-custom-authentication-interceptor-example
Following journaldev article has more detail in the same line of Steven response above http://www.journaldev.com/2210/struts-2-interceptor-tutorial-with-custom-authentication-interceptor-example