1

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>

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

}

Roman C
  • 49,761
  • 33
  • 66
  • 176
Pulkit
  • 3,953
  • 6
  • 31
  • 55

1 Answers1

1

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();
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • It worked man!!!!!! Thanx a lot @RomanC :D. But still one thing I am not able to get, I tried this approach of getting all the parameters in Map but I invoked on ActionInvocation object and I was getting value of userId but they were not in readable form, I even tried using toString() method. How this thing worked using ServletActionContext?? – Pulkit Nov 28 '13 at 19:55
  • 2
    It used servlet request to get its parameters using direct servlet API. ServletActionContext is a helper class which uses static methods to access servlet staff in interceptors. – Roman C Nov 28 '13 at 20:05