简体   繁体   English

拦截器在Struts 2.0中用于登录

[英]Interceptors use in login in Struts 2.0

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. 现在,如果用户ID和密码不为空,我想使用拦截器。 But I am not able to find out how I can access the values of request parameters in Interceptors. 但是我无法找出如何访问拦截器中请求参数的值。 JSP Code JSP代码

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

Model 模型

@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;
}
 }

View 视图

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;
}

Interceptor 拦截器

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();
}

} }

This code should give you parameters from the servlet request. 此代码应为您提供来自servlet请求的参数。 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM