I want to fetch a selected value from a drop down 'select' list of 'form' in a jsp page into a variable defined in the action class of the form, where 'select' drop down list is itself fetched dynamically from a column 'name' of database table 'Category' with the list 'categoryList' which is defined in some another action class.
After fetching the selected value (that is a name of Category) i want to fetch the primary key 'cid' of the table 'Category'. columns of Category are : id, name
After Retrieving the 'cid' of the category i want to fill this cid in the column 'cid' of another table 'Question'.
I am using struts2 and hibernate.
My column is 'name' and table is 'Category' I have made the mapping configuration and bean classes.
My code of action class where the list is generated :
public class FindCategory extends ActionSupport {
private List<Category> categoryList = new ArrayList<Category>();
@Override
public String execute() throws Exception {
Session session = null;
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
this.categoryList = (List<Category>) session.createQuery("from Category").list();
if (this.categoryList.isEmpty()) {
this.addActionError("Sorry.. No category Available. Try again Later.!");
return ERROR;
}
session.getTransaction().commit();
} catch (Exception e) {
this.addActionError("Oops. An Error Encountered...!");
return ERROR;
}
return SUCCESS;
}
public List<Category> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<Category> categoryList) {
this.categoryList = categoryList;
}
}
code in a 'form' of a jsp page :
<s:form action="okadddqs" method="post" cssClass="text">
<input type="hidden" name="email" value="goods.ramesh@gmail.com"/>
<s:select label="Select Category :" name="name" list="categoryList" listkey="name" listValue="name"/> //Here the list is generated
<s:textarea label="Your Question " cols="40" rows="5" name="body"/>
<s:textfield name="op1" label="Option 1 :"/>
<s:textfield name="op2" label="Option 2 :"/>
<s:textfield name="op3" label="Option 3 :"/>
<s:textfield name="op4" label="Option 4 :"/>
<s:textfield name="op5" label="Option 5 :"/>
<s:select label="Correct Option :"
name="opc"
list="#@java.util.LinkedHashMap@{'1':'One',
'2':'Two','3':'Three','4':'Four','5':'Five'}"/>
<s:submit value="Update Daily Question"/>
</s:form>
My action to submit a new question class :
package com.rambo.action;
import beans.Category;
import beans.Question;
import beans.Users;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hibernate.Session;
/**
*
* @author ROMO
*/
@ManagedBean
@SessionScoped
public class NewQuestion extends ActionSupport {
private String cname;
private List<Category> cl = new ArrayList<Category>();
public List<Category> getCl() {
return cl;
}
public void setCl(List<Category> cl) {
this.cl = cl;
}
@Override
public String execute() throws Exception {
Session session = null;
int c;
//c store the cid of the selected Category name from drop down list.
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
cl = (List<Category>) session.createQuery("from Category c where c.name = '" + getName() + "'");
if (!cl.isEmpty()) {
c = cl.get(0).getCid();
} else {
this.addActionError("Oops. Sorry No Category Available.");
session.close();
return ERROR;
}
u = new Question();
u.setCid(c);
u.setCname(getName());
session.save(u);
session.getTransaction().commit();
} catch (Exception e) {
this.addActionError("Oops. An Error Encountered...! Email address already registered. Try with your new email address.");
session.close();
return ERROR;
}
return SUCCESS;
}
@Override
public void validate() {
if ("".equals(getEmail()) || getEmail() == null ) {
this.addActionError("All Fields are Compulsory to input..!");
} else if (getEmail().indexOf("@") < 0 || getEmail().indexOf(",") > 0 || getEmail().indexOf(".") < 0) {
this.addActionError("Please Input a valid email address.");
}
}
}
Mapping in Category.hbm.xml :
<property name="name" type="string">
<column name="NAME" length="20" not-null="true" />
</property>
Getter and setter of the bean "Category.java":
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
My glassfish server shows error as :
org.apache.jasper.JasperException: tag 'select', field 'list', name 'cname': The requested list key 'categoryList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
root cause tag 'select', field 'list', name 'cname': The requested list key 'categoryList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
Can some one please point out what may b the error..? thanks in advance.
As we discussed in comments,
categoryList
should be of typeCategory
with getter/setterthen in your jsp
Now declare a hidden field in your form to submit
cname
also, withcid
jQuery code(as requested by you) to set
cname
You need to declare
cid
&cname
variables(with getter/setter) in yourNewQuestion
actionThe root cause of your exception is coming from 'categoryList' code as the exception says.
see Find the error in Struts2 dropdown list program? for more details . I am pretty sure you are on the same problem.
If not please post some more code, preferably the code that is suspected of having the problem (categoryList variable and its getters and setters)