I made website with Hibernate and Struts 2 with using of Ajax in Java. Basically, when I create, modify any local content (in .java), like assign value to int
, at next inspection its null
. This happens every time.
This is the struts.xml
most of my pages are bidden this way:
<action name="showclient" class="modelClass.LogInObject" method="show">
<result name="success" >/MyAccount.jsp</result>
</action>
Client is just a bin class with CRUD implemented:
public class Client extends ActionSupport {
private static final long serialVersionUID = 1L;
private Long idClient;
private String login;
private String password;
private String nom;
private String prenom;
private String adresse;
private String ville;
private int cPostal;
private String email;
private Set<Commande> panier = new HashSet<Commande>(0);
// private static final Logger logger = Logger.getLogger(Client.class.getName());
public static Client logIn(String login, String password)
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String qstr = "SELECT * FROM client";
Query query = session.createSQLQuery(qstr).addEntity(Client.class);
List<Client> list = (List<Client>)query.list();
Iterator<Client> it = list.iterator();
while (it.hasNext())
{
Client client = it.next();
if (client.getLogin().compareTo(login) == 0 &&
client.getPassword().compareTo(password) == 0)
return client;
}
return null;
}
The LogInObject
:
public class LogInObject extends ActionSupport {
private static final long serialVersionUID = 1L;
private Long loggedInId;
private String login;
private String password;
private String confirmPassword;
private String error;
public Client client;
public String login()
{
Client client = null;
if ((client = Client.read(Client.logIn(this.login, this.password).getIdClient())) != null)
{
this.client = client;
return SUCCESS;
}
this.login = "";
this.password = "";
this.error = "Bad login or password";
return ERROR;
}
public String show()
{
System.out.println(this.getClient().getIdClient());
return SUCCESS;
}
And finally main class:
public class MainActionObject extends ActionSupport {
private static final long serialVersionUID = 1L;
private boolean logged;
private String search;
List<Produit> listProduit;
LogInObject loginObject;
public String login()
{
if (this.loginObject.login() == SUCCESS)
{
this.setLogged(true);
return SUCCESS;
}
return ERROR;
}
}
So the show()
in loginobject
prints id of client that was read from SQL base at the moment of initialization I print it to the .jsp
page all info r there and after all it no longer exist. even if I make a backup of id in any possible way. I know the meaning of CRUD, so don't tell me its normal.
Ok, your action needs to extends ActionSupport implements ServletRequestAware
and when your client logged in successfully
public String login()
{
...
getServletRequest().getSession().setAttribute("idClient", client.getIdClient());
public String show()
{
System.out.println(getServletRequest().getSession().getAttribute("idClient"));