Struts 2.0 中登录时使用的拦截器 [英] Interceptors use in login in Struts 2.0

查看:23
本文介绍了Struts 2.0 中登录时使用的拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个基本应用程序,其中用户提供他的用户 ID 和密码,如果登录成功,他将被重定向到主页.现在进行验证,如果用户 ID 和密码不为空,我想使用拦截器.但我无法找出如何访问拦截器中请求参数的值.JSP代码

I am designing a basic application in which User provides his user id and password and if the login is successful he is redirected to home page. Now for the validation I want to use interceptors if the user id and password are not empty. But I am not able to find out how I can access the values of request parameters in Interceptors. JSP Code

<s:form action="Login.action" method="post">
       <s:textfield label="Username" name="bean.userId"/>
       <s:submit value="Login" />
</s:form>

型号

@Entity
@Table(name="login")
public class Login implements Serializable
{
public Login()
{
}
public Login(String userId1, String userPassword1) {
    userId1 = userId;
    userPassword1 = userPassword;
}
private String userId;
private String userPassword;

@Id
@Column(name="USERID", nullable=false)
public String getUserId() {
    return userId;
}
public void setUserId(String userId) {
    this.userId = userId;
}
@Column(name="USERPASSWORD", nullable=false)
public String getUserPassword() {
    return userPassword;
}
public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
}
 }

查看

public class LoginAction extends ActionSupport
{
   private Login bean;
public String login()
{
    LoginManager manager=new LoginManager();
        try
        {
            manager.add(getBean());
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
         return "success";
} 


public Login getBean() {
    return bean;
}

public void setBean(Login bean) {
    this.bean = bean;
}

拦截器

public class LoggingInterceptor implements Interceptor
{
public void destroy() 
{
    System.out.println("Destorying......");
}

public void init() {
    System.out.println("Initializing......");

}

public String intercept(ActionInvocation actionInvocation) throws Exception 
{
    ActionConfig config  = actionInvocation.getProxy().getConfig();  
    Map parameters       = config.getParams();  
    String menuId        = (String)parameters.get("userId");
    System.out.println("Got it:"+menuId);
            return actionInvocation.invoke();
}

}

推荐答案

此代码应该为您提供来自 servlet 请求的参数.假设您有一个参数值.

This code should give you parameters from the servlet request. Assume you have one value for the parameter.

public String intercept(ActionInvocation actionInvocation) throws Exception 
{
    Map<String, String[]> parameters = ServletActionContext.getRequest().getParameterMap();  
    String userId  = parameters.get("bean.userId")[0];
    System.out.println("Got it:"+userId);
    return actionInvocation.invoke();
}

这篇关于Struts 2.0 中登录时使用的拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆