使用struts2和hibernate维护会话?

时间:2013-06-13 15:23:50

标签: hibernate session struts2

我需要知道如何在Struts 2中维护会话我有一个表单和两个按钮,如添加和更新...如果你添加更多时间相同的表单,数据将存储到会话,我在会话中删除了一个表单细节不是数据库,最后提交表格,所有表格的详细信息都会存储到数据库中......任何人都知道怎么做,请帮我解决这个问题 我的代码是:

    public class LogingEx extends ActionSupport implements SessionAware{    

    private int id;

    private String stuname,country;
    private int stumarks,stuage;
    private static final String HELLO_COUNT = "helloCount";
    private Map userSession;;

    @Id
    @GenericGenerator(name = "mygen1", strategy = "increment")
    @GeneratedValue(generator = "mygen1")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getStuname() {
        return stuname;
    }
    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public int getStuage() {
        return stuage;
    }
    public void setStuage(int stuage) {
        this.stuage = stuage;
    }

    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

    public int getStumarks() {
        return stumarks;
    }
    public void setStumarks(int stumarks) {
        this.stumarks = stumarks;
    }


    public String execute() throws IOException
    {   

        userSession.put("a",stuname);
        userSession.put("b", stuage);
        userSession.put("c",stumarks);
        userSession.put("d",country);

        Database.saveData(this);
        return SUCCESS;
    }
     public void setSession(Map session) {
         userSession = session ;
        }  
}

数据库:

public class Database {
    public static int saveData(LogingEx e){
    SessionFactory sf=new AnnotationConfiguration().configure().buildSessionFactory();
    Session session=sf.openSession();
    Transaction tran=session.beginTransaction();

    int i=(Integer)session.save(e);
    tran.commit();
    session.close();
    return i;

}
}

STRUTS:

 <struts>
        <package name="default" extends="struts-default">
             <action name="verify" class="org.LogingEx">
                <result name="success">/index.jsp</result>           
            </action>
        </package>
    </struts>

JSP:     
    

    <s:form action="verify">

        <s:textfield name="stuname" label="Enter Username" /><br>
        <s:textfield name="stuage" label="Enter Age" /><br>
        <s:textfield name="stumarks" label="Enter Marks" /><br>
        <s:textfield name="country" label="Enter Country" /><br>
        <s:submit value="Click" id="submit" align="center" /> 
    </s:form>

    Name:<s:property value="#session.a" /><br>
    Age:<s:property value="#session.b" /> <br>
    Marks:<s:property value="#session.c" /><br>
    Country:<s:property value="#session.d" />
    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

首次提交表单数据时,请将表单数据放入数组中,并在提交新表单时重复该步骤。

将数组放入会话中,以便在重复表单提交期间保留此数组。

完成表单提交部分后,您可以将数组中的表单数据存储到数据库中。