SessionAware 不注入会话对象
我再次提出了 s2 的问题,我是一个初学者,在寻找示例时遇到了一些麻烦。
我正在构建一个依赖于登录的菜单服务,该登录是用户在起始页面输入时给出的。 登录完成后,我通过执行以下操作将用户对象存储到会话中:
@Override
public String intercept(ActionInvocation invocation) throws Exception {
....
// verifica se o parametro do CPF veio no get e tenta logar o usuario
if (!StringUtils.isBlank(request.getParameter(USER_CPF_REQUEST))) {
if ( doLogin(request, session) ) {
return Action.SUCCESS;
}
然后是 doLogin 方法
Usuario usuario = getServico().buscar( Long.valueOf(cpf) );
//Caso o usuário exista, guarda na session
if (usuario != null){
session.setAttribute(USER_HANDLE, usuario);
return true;
}
现在问题来了,我通过 ApplicationContext.xml 上的以下 xml 片段在 MenuAction 上注入了一个 MenuBean
<bean id="menuService" class="br.com.autenticis.renacon.ejb.MenuBean" />
<bean id="menuAction" scope="prototype" class="br.com.autenticis.renacon.actions.MenuAction">
<constructor-arg ref="menuService" />
</bean>
以及 menuAction声明如下:
public class MenuAction extends ActionSupport implements Preparable, SessionAware
通过这样做,我需要使用私有成员实现会话集
private Map<String, Object> session;
....
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
下面所有方法的失败部分,会话对象在调试时为空:
public String execute() {
if ( session.containsKey( LoginInterceptor.USER_HANDLE) ){
Usuario u = (Usuario) session.get( LoginInterceptor.USER_HANDLE);
setMenu( servico.getMenuPerfil( u.getPerfil() ) );
return Action.SUCCESS;
}
return input();
}
有谁知道为什么? 或者如何实施? 查看上面的代码,我需要来自登录用户的“Perfil”,如果会话包含用户对象的密钥,我会获取它,然后使用 perfil 通过设置器填充菜单并返回成功,否则它'将返回 INPUT,这将导致登录屏幕。
Once more here I come with a question of s2, I'm a beginner at it and got some trouble finding examples.
I'm building a menu service which depends upon a login, which is give when the user enters at start page. Once the login has been made, I store the user object into the session by doing the follow:
@Override
public String intercept(ActionInvocation invocation) throws Exception {
....
// verifica se o parametro do CPF veio no get e tenta logar o usuario
if (!StringUtils.isBlank(request.getParameter(USER_CPF_REQUEST))) {
if ( doLogin(request, session) ) {
return Action.SUCCESS;
}
and then the doLogin method
Usuario usuario = getServico().buscar( Long.valueOf(cpf) );
//Caso o usuário exista, guarda na session
if (usuario != null){
session.setAttribute(USER_HANDLE, usuario);
return true;
}
Now comes the problem, I have a MenuBean injected on the MenuAction by the following xml piece on ApplicationContext.xml
<bean id="menuService" class="br.com.autenticis.renacon.ejb.MenuBean" />
<bean id="menuAction" scope="prototype" class="br.com.autenticis.renacon.actions.MenuAction">
<constructor-arg ref="menuService" />
</bean>
And the menuAction is declared as follow:
public class MenuAction extends ActionSupport implements Preparable, SessionAware
By doing so I need to implement the session set with a private member
private Map<String, Object> session;
....
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
The failing part of all its the method bellow, the session object is null at debugging:
public String execute() {
if ( session.containsKey( LoginInterceptor.USER_HANDLE) ){
Usuario u = (Usuario) session.get( LoginInterceptor.USER_HANDLE);
setMenu( servico.getMenuPerfil( u.getPerfil() ) );
return Action.SUCCESS;
}
return input();
}
Does anyone know why? or how to implement it? Looking at the code above, I need 'Perfil' from the user which is logged on, if the session contains the key to the user object I get it and then use the perfil to populate the Menu through the setter and return SUCESS, otherwise it'll return INPUT which will lead to the login screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过我们的一些努力,我设法发现了错误。 它不在程序中,而是在 struts.xml 中,我为应用程序编写了一个自定义堆栈:
并且有点忘记包含与会话相关的项目,将 defaultLoginStack 更改为
我现在可以正确使用会话注入。
After some ours I managed to discover the error. wich lies not in the program but in the struts.xml, I've written a custom stack for the application:
And somewhat forgot to include session-related items, with the defaultLoginStack changed to
I'm now able to use session injected correctly.