Java 如何使用Struts 2和Hibernate维护会话?
我需要知道如何使用Struts2维护一个表单和多个表单的会话Java 如何使用Struts 2和Hibernate维护会话?,java,hibernate,jsp,session,struts2,Java,Hibernate,Jsp,Session,Struts2,我需要知道如何使用Struts2维护一个表单和多个表单的会话输入[姓名、城市、国家],最后使用Hibernate将数据存储到数据库中 此表单有两个按钮: 添加(存储到会话) 提交(存储到数据库) 首先,输入表单详细信息姓名、城市和国家,然后单击添加按钮数据将存储到会话 其次,输入相同的详细信息,然后单击添加 第三,输入相同的表单详细信息,但现在单击提交按钮,所有详细信息(第一、第二和第三)将使用Hibernate存储到数据库中 出现错误,我们的代码是: @Entity public c
输入[姓名、城市、国家]
,最后使用Hibernate将数据存储到数据库中
此表单有两个按钮:
添加
(存储到会话)李>
提交
(存储到数据库)姓名、城市和国家
,然后单击添加按钮数据将存储到会话
其次,输入相同的详细信息,然后单击添加
第三,输入相同的表单详细信息,但现在单击提交按钮,所有详细信息(第一、第二和第三)将使用Hibernate存储到数据库中
出现错误,我们的代码是:
@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;
}
}
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;
}
}
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;
}
}
<body>
<s:property value="#session.name"/>
</body>
<?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>
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;
}
}
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;
}
}
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;
}
}
<body>
<s:property value="#session.name"/>
</body>
<?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>
PersonAction.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;
}
}
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;
}
}
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;
}
}
<body>
<s:property value="#session.name"/>
</body>
<?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>
struts.xml
:
@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;
}
}
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;
}
}
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;
}
}
<body>
<s:property value="#session.name"/>
</body>
<?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>
/person.jsp
/person.jsp
index.jsp
:
@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;
}
}
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;
}
}
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;
}
}
<body>
<s:property value="#session.name"/>
</body>
<?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>
person.jsp
:
@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;
}
}
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;
}
}
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;
}
}
<body>
<s:property value="#session.name"/>
</body>
<?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>
您应该将按钮映射到操作中的方法。默认情况下,允许使用按钮名称或
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>
现在,您已经通过映射到相应按钮的方法分离了逻辑。要确保工作正常,请确保已打开(默认情况下为打开),并且堆栈defaultStack
应用于操作配置(默认情况下使用)
struts.xml:
@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;
}
}
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;
}
}
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;
}
}
<body>
<s:property value="#session.name"/>
</body>
<?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>
ActionContext
是操作调用的容器占位符,更详细的解释是。您应该将按钮映射到操作中的方法。默认情况下,允许使用按钮名称或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>
现在,您已经通过映射到相应按钮的方法分离了逻辑。要确保工作正常,请确保已打开(默认情况下为打开),并且堆栈defaultStack
应用于操作配置(默认情况下使用)
struts.xml:
@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;
}
}
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;
}
}
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;
}
}
<body>
<s:property value="#session.name"/>
</body>
<?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>
ActionContext
是操作调用的容器占位符,更详细的解释是。为什么要在会话中存储一个表单,但不同的数据…如果我们输入数据3次,最后所有数据都将保存到数据库中。。。请帮我解决dis:)是的…我使用sessionAware和HttpSession,但我不知道如何解决dis:(-ty PSRare u同时存储所有名称或…?不…首先我单击将注册表数据添加到会话中..工作正常..第二次单击将注册表数据添加到会话中现在我只有dis数据…我不知道如何检索第一次添加的数据为什么要存储在会话中这是我的任务一个表单,但不同ent数据…如果我们输入数据3次,最后所有数据将保存到数据库…请帮助我解决dis:)是的…我使用sessionAware和HttpSession,但我不知道如何进行dis:(-ty PSRare u同时存储所有名称或…?不…首先我单击将注册表数据添加到会话中..工作正常..第二次单击将注册表数据添加到会话中,现在我只有dis数据…我不知道如何检索第一个添加的数据感谢Roman它对我的帮助很大:):)…嘿,sessionAware接口和ActionContext有什么区别?嗨,罗曼:我理解逻辑,但对struts.xml有点困惑。你能给我看一下struts.xml文件中的dis代码吗?谢谢你罗曼:出错,请看da更新,谢谢你重播我:)对于错误,您需要随stacktrace代码一起发布,并包括错误的根本原因。我在这段代码中犯了一些错误。。我找不到解决方案,我发布了所有代码:(谢谢你,这对我帮助很大:):)…嘿,sessionAware接口和ActionContext有什么区别吗?嗨,罗曼:我理解逻辑,但对struts.xml有点困惑。你能给我看看struts.xml文件中的dis代码吗?谢谢你罗曼:有个错误,请看da更新,谢谢你重播我:)对于错误,你需要随代码一起发布stacktrace并包括错误的根本原因。我在这段代码中犯了一些错误。。我找不到解决方案,因为我发布了所有代码:(