我用Hibernate和Struts 2在Java中使用Ajax制作了网站。基本上,当我创建时,修改任何本地内容(.java),例如将值分配给int
,在下次检查其null
。这种情况每次都会发生。
这是我的大多数页面都是这样出价struts.xml
:
<action name="showclient" class="modelClass.LogInObject" method="show">
<result name="success" >/MyAccount.jsp</result>
</action>
客户端只是一个实现了 CRUD 的 bin 类:
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;
}
}
因此,loginobject
中的show()
打印在初始化时从SQL库读取的客户端的id,我将其打印到.jsp
页面上的所有信息r,毕竟它不再存在。 即使我以任何可能的方式备份 id。我知道CRUD的含义,所以不要告诉我它的正常。
好的,你的操作需要extends ActionSupport implements ServletRequestAware
以及您的客户何时成功登录
public String login()
{
...
getServletRequest().getSession().setAttribute("idClient", client.getIdClient());
public String show()
{
System.out.println(getServletRequest().getSession().getAttribute("idClient"));