簡體   English   中英

session 維護使用 struts2 和 hibernate?

[英]session maintain using struts2 and hibernate?

我需要知道如何使用 Struts2 維護 session 的一個表單和多個輸入[Name,City,Country],最后數據將使用 hibernate 存儲到數據庫中。

此表單有兩個按鈕:

  • 添加(存儲到會話);
  • 提交(存儲到數據庫)。
  • 首先,輸入表單詳細信息[名稱城市和國家],然后單擊添加按鈕數據將存儲到 session。

  • 其次,輸入相同的詳細信息,然后單擊添加。

  • 第三,輸入相同的表單詳細信息,但現在單擊提交按鈕,所有詳細信息(第一個第二個和第三個)將使用 hibernate 存儲到數據庫中。

請幫我解決問題...

人.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:

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;

      }

    } 

數據庫.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>
    <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> 

索引.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> 

人.jsp:

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

首先閱讀使用 SessionAware 時的最佳實踐

我會給你提示,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 .

您的問題是您將PersonAction發送到您的DataBase ,您必須發送與 hibernate 映射的Person :)。 並盡量不要使用 static 方法來解決這個問題,因為如果您在並發應用程序中,許多用戶將訪問該方法saveData

這是不好的

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

你必須做這樣的事情

Database.saveData(personList);

並在數據庫 class 中定義一個方法

public static int saveData(List<Person> person)

同樣在你的Person class 你必須 map 名稱屬性

注意我不建議對這類事情使用 static 方法

在動作 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();
    }

你可以在執行方法中使用這段代碼

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

如下構建您的 SessionFactory。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM