如何使用Struts2和hibernate维护会话?

时间:2013-06-09 11:34:32

标签: java hibernate jsp session struts2

我需要知道如何使用Struts2维护一个表单和多个input[Name,City,Country]的会话,最后使用hibernate将数据存储到数据库。

此表单有两个按钮:

  1. add(存储到会话中);
  2. Submit(存储到数据库)。
  3. 首先,输入表单详细信息[name city and country],然后点击添加按钮数据将存储到session

    其次,输入相同的详细信息,然后点击add

    第三,输入相同的表单详细信息但现在单击提交按钮所有详细信息(第一个和第三个)将使用hibernate存储到数据库。

    请帮助我解决问题...

    错误:我们的代码是:
    Person.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;
    
          }
    
        } 
    

    Database.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> 
    

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

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

1 个答案:

答案 0 :(得分:1)

您应该将按钮映射到正在运行的方法。默认action mapper允许使用按钮名称或method属性来指定表单映射使用的方法。例如

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

现在,在您执行SessionAware

的操作中
public class PersonAction extends ActionSupport implements SessionAware {

  private Person person = new Person();

  public Person getPerson() {
    return person;
  }

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

  private Map<String, Object> session;

  public 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)
     getPersonService().save(p); // save to db
    //clear the list
    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);
    }
    personList.add(person);
    return SUCCESS;

  }

} 

现在,您通过映射到相应按钮的方法分离了逻辑。要确保您已启用DMI (Dynamic Method Invocation)(默认情况下已启用),并且interceptors堆栈defaultStack已应用于操作配置(默认情况下已使用)。< / p>

<强> struts.xml中:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
  <constant name="struts.devMode" value="false"/>

  <package name="default" extends="struts-default">
    <action name="person" class="PersonAction">
      <result>/person.jsp</result>
    </action>
  </package>
</struts>
如果要将servlet会话放入对象中,

SessionAware是您的操作或基本操作应实现的接口。关于它的更多信息here

ActionContext是操作调用的容器占位符,更详细的解释是here