Session Management in Struts2 using Session Aware Interface

Session Management in Struts2 using Session Aware Interface


Session Management in Struts2 can be done in many ways.

One of the ways is to implement the Session Aware Interface.

Let us look at a sample login application that uses the Session Aware Interface.

Step-1: Create a LoginAction class that implements a SessionAware Interface.

Session Aware interface is available as a part of – interceptor package org.apache.struts2.interceptor.SessionAware
.
Implement the setSession() method which is an abstract method of SessionAware Interface

package com.anjana.struts2;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.SessionAware;
import java.util.Map;
public class Login extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 1L;
private String userName;

private String password;
private String variableValue = "ffddf";
private Map sessionObj;
public Login() {
}


public String execute() {
sessionObj.put("userName", getUserName());
System.out.println("execute()");
return SUCCESS;
}
public void validate() {
if (getUserName().length() == 0) {
addFieldError("userName", "User Name is required");
} else if (!getUserName().equals("Anjana")) {
addFieldError("userName", "Invalid User");
}
f (getPassword().length() == 0) {
addFieldError("password", getText("password.required"));
}


}







0---------------------------00000000some code here


//implemented the abstract method


public void setSession(Map sessionObj) {


this.sessionObj = sessionObj;






}


}

Comments

  1. Gracias pero el ejemplo es muy simple y el código cuesta trabajo leerlo.

    ReplyDelete

Post a Comment

Popular posts from this blog

defining functions clojure

Integrating Struts2 with Spring Security using Custom Login Form