I'm trying to build a web-based GUI tool for our DB using Struts2.
I managed to render and populate all fields in the web-form but now have a problem saving the input into the DB.
Here is a reduced example of the problem (of course this is not the real code, but enough to understand the dilemma:
Animal.java:
public class Animal{
private final static long serialVersionUID = 2L;
protected String id;
protected String comment;
protected String name;
protected BaseAnimal subanimal;
protected Date undotted;
// All getters+setters also exist.
public String getAnimalType(){
return subanimal.getClass().getSimpleName();
}
Cat.java:
public class Cat extends BaseAnimal{
private final static long serialVersionUID = 2L;
protected Gender gender;
protected int age;
protected Color color;
protected Voice voice;
//All getters+setters also exist
}
Turtle.java:
public class Turtle extends BaseAnimal{
private final static long serialVersionUID = 2L;
protected Gender gender;
protected int circlesOnBack;
protected int speed;
//All getters+setters also exist
}
DoAction.java:
public class DoItem extends ActionSupport implements ModelDriven<Animal>{
private static final long serialVersionUID = 2L;
protected String idForm;
protected Animal animal;
@Override
public String execute(){
if (idForm != null && !idForm.equals("")){
showAnimal(session); //loads animal from DB
}
return SUCCESS;
}
public void saveAnimal(Map<String,Object> session){
// Stuck here, help?
}
public Animal getModel() {
return animal;
}
public void setModel(Object animal) {
this.animal = (Animal) animal;
}
public String getIdForm() {
return idForm;
}
public void setIdForm(String idForm) {
this.idForm = idForm;
}
public Animal getAnimal() {
return animal;
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
}
animal.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<s:form method="post" theme="simple">
<table>
<tr>
<td><s:textfield key="name"/></td>
<td><s:textarea key="comment"/></td>
<td class='show4cat show4Turtle'>
<s:select key="animal.subanimal.gender" list='getMap("Gender")'/>
</td>
<td class='show4cat'>
<s:select key="animal.subanimal.color" list='getMap("Color")'/>
</td>
<td class='show4turtle'>
<s:select key="animal.subanimal.speed" list='%{#{'1':'Fast','2':'Ultra Fast'}}'/>
</td>
</tr>
<tr>
<td class='show4turtle'>
<s:submit action='saveTurtle' name='submitAnimal' value='save'/>
</td>
<td class='show4cat'>
<s:submit action='saveCat' name='submitAnimal' value='save'/>
</td>
</tr>
</table>
</s:form>
</body>
</html>
I now want to submit this form and save the animals to the DB. How do I manage this save action? My main concern is creating an object of the appropriate child class.
NOTE: I can not change the Data Model design! but can change the JSPs or action classes.
Thanks in advance!