2

我用 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>

Client 只是一个实现了 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;
}
}

因此,在初始化时从 SQL 库读取的客户端的show()in prints id 我将它打印到页面上的所有信息 r 那里,毕竟它不再存在。即使我以任何可能的方式备份 id 。我知道 CRUD 的含义,所以不要告诉我它是正常的。loginobject.jsp

4

1 回答 1

0

好的,您的操作需要extends ActionSupport implements ServletRequestAware

当您的客户成功登录时

public  String login()
{
  ...
  getServletRequest().getSession().setAttribute("idClient", client.getIdClient()); 

public  String  show()
{
  System.out.println(getServletRequest().getSession().getAttribute("idClient"));
于 2012-11-24T21:13:16.943 回答