• <small id='a6OcK'></small><noframes id='a6OcK'>

    1. <i id='a6OcK'><tr id='a6OcK'><dt id='a6OcK'><q id='a6OcK'><span id='a6OcK'><b id='a6OcK'><form id='a6OcK'><ins id='a6OcK'></ins><ul id='a6OcK'></ul><sub id='a6OcK'></sub></form><legend id='a6OcK'></legend><bdo id='a6OcK'><pre id='a6OcK'><center id='a6OcK'></center></pre></bdo></b><th id='a6OcK'></th></span></q></dt></tr></i><div id='a6OcK'><tfoot id='a6OcK'></tfoot><dl id='a6OcK'><fieldset id='a6OcK'></fieldset></dl></div>

        <bdo id='a6OcK'></bdo><ul id='a6OcK'></ul>

        <legend id='a6OcK'><style id='a6OcK'><dir id='a6OcK'><q id='a6OcK'></q></dir></style></legend>
      1. <tfoot id='a6OcK'></tfoot>

        如何使用 Struts 2 和 Hibernate 维护会话?

        How to maintain the session using Struts 2 and Hibernate?(如何使用 Struts 2 和 Hibernate 维护会话?)
        • <i id='JasGi'><tr id='JasGi'><dt id='JasGi'><q id='JasGi'><span id='JasGi'><b id='JasGi'><form id='JasGi'><ins id='JasGi'></ins><ul id='JasGi'></ul><sub id='JasGi'></sub></form><legend id='JasGi'></legend><bdo id='JasGi'><pre id='JasGi'><center id='JasGi'></center></pre></bdo></b><th id='JasGi'></th></span></q></dt></tr></i><div id='JasGi'><tfoot id='JasGi'></tfoot><dl id='JasGi'><fieldset id='JasGi'></fieldset></dl></div>
        • <legend id='JasGi'><style id='JasGi'><dir id='JasGi'><q id='JasGi'></q></dir></style></legend><tfoot id='JasGi'></tfoot>

              <tbody id='JasGi'></tbody>

                <small id='JasGi'></small><noframes id='JasGi'>

                  <bdo id='JasGi'></bdo><ul id='JasGi'></ul>
                  本文介绍了如何使用 Struts 2 和 Hibernate 维护会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要知道如何使用 Struts2 维护一个表单和多个 输入 [Name, City, Country] 的会话,最后数据将使用 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.

                  这个表单有两个按钮:

                  1. 添加(存储到会话中);
                  2. 提交(存入数据库).
                  1. add (stored to session);
                  2. Submit (stored to database).

                  首先,输入表单详细信息Name, City and Country并点击添加按钮数据将存储到session.

                  First, enter the form details Name, City and Country and click add button data will store to session.

                  其次,输入相同的详细信息,然后单击添加.

                  Second, enter the details for same and now click add.

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

                  Third, enter same form details but now click submit button all details (first second & third) will stored to database using Hibernate.

                  出错了,我们的代码是:

                  Person.java :

                  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>
                  

                  推荐答案

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

                  You should map the buttons to the method in action. The default action mapper allows to use button names or method attribute to specify the method other than used by the form mapping. For example

                  <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

                  Now, in the action you implement 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(动态方法调用) 开启(默认开启)和 拦截器 堆栈 defaultStack 应用于动作配置(默认使用).

                  Now, you have separated logic via methods mapped to a corresponding button. To make working be sure you have DMI (Dynamic Method Invocation) turned on (by default is on) and interceptors stack defaultStack is applied to the action config (by default it's used).

                  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>
                  

                  SessionAware 是一个接口,如果您想使用 servlet 会话将其放入对象中,您的操作或基本操作应实现该接口.更多关于它这里.

                  SessionAware is a interface that your action or base action should implement if you want to use servlet session to put into your objects. More about it here.

                  ActionContext是action调用的容器占位符,更详细的解释是这里.

                  ActionContext is a container placeholder for the action invocation, more detailed explanation is here.

                  这篇关于如何使用 Struts 2 和 Hibernate 维护会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                    <tfoot id='Lbokp'></tfoot>

                    <small id='Lbokp'></small><noframes id='Lbokp'>

                      <bdo id='Lbokp'></bdo><ul id='Lbokp'></ul>
                          <tbody id='Lbokp'></tbody>

                          <legend id='Lbokp'><style id='Lbokp'><dir id='Lbokp'><q id='Lbokp'></q></dir></style></legend>

                            <i id='Lbokp'><tr id='Lbokp'><dt id='Lbokp'><q id='Lbokp'><span id='Lbokp'><b id='Lbokp'><form id='Lbokp'><ins id='Lbokp'></ins><ul id='Lbokp'></ul><sub id='Lbokp'></sub></form><legend id='Lbokp'></legend><bdo id='Lbokp'><pre id='Lbokp'><center id='Lbokp'></center></pre></bdo></b><th id='Lbokp'></th></span></q></dt></tr></i><div id='Lbokp'><tfoot id='Lbokp'></tfoot><dl id='Lbokp'><fieldset id='Lbokp'></fieldset></dl></div>