我使用Java中的Ajax在Hibernate和Struts 2中製作了網站。基本上,當我創建時,修改任何本地內容(在.java中),如在int
的指定值,在下次檢查null
。每次都發生這種情況。Web Java變量重置
這是struts.xml
我的大部分頁面都吩咐這樣:
<action name="showclient" class="modelClass.LogInObject" method="show">
<result name="success" >/MyAccount.jsp</result>
</action>
客戶端僅僅是一個箱類CRUD實現:
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;
}
的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;
}
最後主類:
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;
}
}
所以客戶端的show()
在loginobject
打印的ID從SQL基地在初始化的時刻讀我這有打印到.jsp
頁的所有信息r和畢竟它不再存在。即使我以任何可能的方式對id做了備份。我知道CRUD的含義,所以不要告訴我它的正常。
你的意思是什麼檢查? –
例如,當我初始化和客戶端的ID,當他登錄時,他點擊編輯按鈕,並且我想通過ID從sql讀取客戶端。我以前的ID現在是空的。 –
你在哪裏存儲該ID? –