如何使用Struts2和hibernate维护会话? [英] How to maintain the session using Struts2 and 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.
此表格有两个按钮:
-
add
(存储到会话); -
提交
(存储到数据库)。
add
(stored to session);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.
请帮助我解决问题......
pls help me to solve dis...
得到一个错误:我们的代码是:
Person.java:
Got an Error: our code is:
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:
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 :
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.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:
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:
person.jsp:
<body>
<s:property value="#session.name"/>
</body>
推荐答案
您应该将按钮映射到实际的方法。默认的动作映射器允许使用按钮名称或方法
属性,用于指定表单映射使用的方法。例如
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
是动作调用的容器占位符,更详细的解释是这里。
ActionContext
is a container placeholder for the action invocation, more detailed explanation is here.
这篇关于如何使用Struts2和hibernate维护会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!