我想從一個jsp頁面中'form'的下拉'選擇'列表中獲取一個選定的值到表單的action類中定義的變量中,其中'select'下拉列表本身是從數據庫表'Category'的列'name'中動態獲取的,列表'categoryList'是在另一個操作類中定義的。從下拉列表中選擇一個值時出錯選擇
獲取選定的值(這是類別的名稱)後,我想要獲取表'分類'的主鍵'cid'。類別的列是:ID,名稱
在檢索類別的'cid'後,我想在另一個表'Question'的列'cid'中填寫此cid。
我正在使用struts2和hibernate。
我的專欄是'名稱'和表'是'類別 我已經做了映射配置和bean類。
我的動作類的代碼,其中生成的列表:
<s:form action="okadddqs" method="post" cssClass="text">
<input type="hidden" name="email" value="[email protected]"/>
<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="#@[email protected]{'1':'One',
'2':'Two','3':'Three','4':'Four','5':'Five'}"/>
<s:submit value="Update Daily Question"/>
</s:form>
我的行動來提交新問題類:
在JSP頁面中的「形式」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;
}
}
代碼
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.");
}
}
}
映射在Category.hbm.xml:
豆 「Category.java」 的<property name="name" type="string">
<column name="NAME" length="20" not-null="true" />
</property>
getter和setter:
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
我的GlassFish服務器會顯示錯誤爲:
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]
能有人請指出什麼可以b是出錯誤。 。? 在此先感謝。
你爲什麼要從你的選擇列表中提交'cname'爲什麼不直接提交'cid'。這將減少您的操作類中不必要的代碼。還提到的錯誤是你的方式youu popu;在列表中不是在這個類別 – anu 2012-07-13 06:29:18
其實我需要同時提交'cname'和相應的 – codeofnode 2012-07-13 07:29:55
但是你在你的班級只使用'cname'來取回相應的'cid' from同一個表'Category'.So,爲什麼不直接從jsp那裏做並保存1個查詢。你是否將'canme'用於其他目的? – anu 2012-07-13 07:37:43