简体   繁体   English

session 维护使用 struts2 和 hibernate?

[英]session maintain using struts2 and hibernate?

I need to know how to maintain session for one form and multiple input[Name,City,Country] using Struts2 and finally data will stored to database using hibernate.我需要知道如何使用 Struts2 维护 session 的一个表单和多个输入[Name,City,Country],最后数据将使用 hibernate 存储到数据库中。

This form have two buttons:此表单有两个按钮:

  • add (stored to session);添加(存储到会话);
  • Submit (stored to database).提交(存储到数据库)。
  • First, enter the form details [name city and country] and click add button data will store to session.首先,输入表单详细信息[名称城市和国家],然后单击添加按钮数据将存储到 session。

  • Second, enter the details for same and now click add.其次,输入相同的详细信息,然后单击添加。

  • Third, enter same form details but now click submit button all details (first second & third) will stored to database using hibernate.第三,输入相同的表单详细信息,但现在单击提交按钮,所有详细信息(第一个第二个和第三个)将使用 hibernate 存储到数据库中。

pls help me to solve dis...请帮我解决问题...

Person.java:人.java:

 @Entity
    public class Person {
        @Id
        @GeneratedValue
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }  

PersonAction.java: PersonAction.java:

public class PersonAction extends ActionSupport implements SessionAware {

      private Person person = new Person();
     // Database base=new Database();

      public Person getPerson() {
        return person;
      }

      public void setPerson(Person person){
        this.person = person;
      }

      private Map<String, Object> session;

      public void setSession(Map<String, Object> session){
        this.session = session;
      }

      public String execute() { //Create persons
        List<Person> personList = (List<Person>) session.get("personList");
        for (Person p : personList)
        Database.saveData(this);
        personList.clear();
        return SUCCESS;
      }

      public String add() { //Add person
        List<Person> personList = (List<Person>) session.get("personList");
        if (personList == null) {
          personList = new ArrayList<Person>();
          session.put("personList", personList);
          System.out.println("Successfully added");
        }
        personList.add(person);
        return SUCCESS;

      }

    } 

Database.java:数据库.java:

public class Database {
public static int saveData(PersonAction personAction){
        SessionFactory sf=new AnnotationConfiguration().configure().buildSessionFactory();
        Session session=sf.openSession();
        Transaction tran=session.beginTransaction();
    int i=(Integer)session.save(personAction);
    tran.commit();
    session.close();
    return i;

    }
}   

struts.xml: struts.xml:

<struts>
    <package name="default" extends="struts-default">
        <action name="person" class="org.PersonAction">
            <result>/person.jsp</result>
        </action>
        <action name="person" class="org.PersonAction" method="add">
            <result>/person.jsp</result>
        </action>
    </package>
</struts> 

index.jsp:索引.jsp:

<s:form action="person">
    <s:textfield label="Enter your name" name="name"/>
    <s:submit value="Add person" method="add"/>
    <s:submit value="Create persons"/>
</s:form> 

person.jsp:人.jsp:

<body>
<s:property value="#session.name"/>
</body>

First of all read Best Practices When Using SessionAware首先阅读使用 SessionAware 时的最佳实践

I will give you hints, Struts2 follow MVC pattern (or a variation of this pattern).我会给你提示,Struts2 遵循MVC pattern (或这种模式的变体)。 So you have your Presentation layer, there you send a event to your Controller (PersonAction) and here you have to communicate with your model (Business Layer DataBase,Person), model returns or not something to the controller and the controller decide what to show. So you have your Presentation layer, there you send a event to your Controller (PersonAction) and here you have to communicate with your model (Business Layer DataBase,Person), model returns or not something to the controller and the controller decide what to show .

Your problem is that you are sending PersonAction to your DataBase , you have to send Person that is mapped with hibernate:).您的问题是您将PersonAction发送到您的DataBase ,您必须发送与 hibernate 映射的Person :)。 And try not using static methods for this things cause if you are in a concurrent application many users will access to that method saveData并尽量不要使用 static 方法来解决这个问题,因为如果您在并发应用程序中,许多用户将访问该方法saveData

This is bad这是不好的

 for (Person p : personList)
            Database.saveData(this);

You have to do something like this你必须做这样的事情

Database.saveData(personList);

And in Database class define a method并在数据库 class 中定义一个方法

public static int saveData(List<Person> person)

Also in your Person class you have to map name property同样在你的Person class 你必须 map 名称属性

NOTE I don`t recommend using static method for this kind of things注意我不建议对这类事情使用 static 方法

use the following methods in the action class在动作 class 中使用以下方法

/**
     * Convenience method to get the request.
     * @return current request
     */
    protected HttpServletRequest getRequest() {
    return ServletActionContext.getRequest();
    }

    /**
     * Convenience method to get the response.
     * @return current response.
     */
    protected HttpServletResponse getResponse() {
    return ServletActionContext.getResponse();
    }

    /**
     * Convenience method to get the session. This will create a session if one
     * doesn't exist.
     * @return the session from the request (request.getSession()).
     */
    protected HttpSession getSession() {
    return getRequest().getSession();
    }

and u can use this code inside execute method你可以在执行方法中使用这段代码

 getRequest().getSession().setAttribute("projectData", scheduleData);

Build your SessionFactory as follows.如下构建您的 SessionFactory。

sessionFactory = new Configuration().configure().buildSessionFactory();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM