Issue
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.
This form have two buttons:
- add (stored to session);
- Submit (stored to database).
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.
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 :
@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>
Solution
First of all read Best Practices When Using SessionAware
I will give you hints, Struts2 follow MVC pattern
(or a variation of this pattern). So you have your Presentation layer, there you send a event to your Controller (PersonAction) and here you have to communicate with your model (Business Layer DataBase,Person) , model returns or not something to the controller and the controller decide what to show.
Your problem is that you are sending PersonAction
to your DataBase
, you have to send Person
that is mapped with hibernate :).
And try not using static methods for this things cause if you are in a concurrent application many users will access to that method saveData
This is bad
for (Person p : personList)
Database.saveData(this);
You have to do something like this
Database.saveData(personList);
And in Database class define a method
public static int saveData(List<Person> person)
Also in your Person
class you have to map name property
NOTE I don`t recommend using static method for this kind of things
Answered By - nachokk
Answer Checked By - Cary Denson (JavaFixing Admin)