Ale*_*ama 5 java jsf selectonemenu dynamic-content primefaces
我在尝试制作我的selectOneMenu
内容时遇到了麻烦,这取决于另一个选择的值。第一个的内容来自我的数据库中的一个表并且运行良好,但第二个应该来自另一个表,但我无法使其工作。这是我的index.html
,我只是想证明这是如何工作的:
<h:outputLabel value="Estado" styleClass="requiredLbl"/>
<p:selectOneMenu id="Estado" value="#{beanInscripcion.id_estado}" valueChangeListener="#{beanInscripcion.buscarMunicipios(event)}" >
<f:selectItem itemLabel="Elegir Estado" itemValue="" />
<f:selectItems value="#{beanInscripcion.estados}"
var="edo" itemLabel="#{edo.nombre_estado}" itemValue="#{edo.id_estado}" />
<p:ajax update="Municipio" listener="#{beanInscripcion.buscarMunicipios(event)}" />
</p:selectOneMenu>
<p:separator />
<h:outputLabel value="Municipio" styleClass="requiredLbl"/>
<p:selectOneMenu id="Municipio" value="municipio">
<f:selectItems value="#{beanInscripcion.municipios}"
var="mun" itemLabel="#{mun.nombre_municipio}" itemValue="#{mun.nombre_municipio}" />
</p:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
这是我的 Bean 部分,我应该在其中获取第二个菜单的内容:
@ManagedBean(name = "beanInscripcion")
@ViewScoped
public class BeanInscripcion implements Serializable {
static String strURL;
private List<Estado> estados;
private List<Municipio> municipios;
private int id_estado;
public BeanInscripcion() throws SQLException{
estados = new ArrayList<Estado>();
buscarEstados();
}
public void buscarEstados() throws SQLException {
Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("SELECT * FROM estado");
result.beforeFirst();
while (result.next()) {
Estado estado = new Estado();
estado.setId_estado(result.getInt("id_estado"));
estado.setNombre_estado(result.getString("nombre_estado"));
estados.add(estado);
}
}
public void buscarMunicipios() throws SQLException {
Connection connection = getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("SELECT id_municipio, nombre_municipio FROM municipio WHERE Estado_id_estado = '" + id_estado + "'");
result.beforeFirst();
while (result.next()) {
Municipio municipio = new Municipio();
municipio.setId_municipio(result.getInt("id_municipio"));
municipio.setNombre_municipio(result.getString("nombre_municipio"));
municipios.add(municipio);
}
}
public Connection getConnection() {
try {
strURL = "jdbc:mysql://localhost:3306/mydb";
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(strURL, "root", "root");
} catch (SQLException ex) {
return null;
} catch (ClassNotFoundException ex) {
return null;
}
}
public List<Estado> getEstados() {
return estados;
}
public void setEstados(List<Estado> estados) {
this.estados = estados;
}
public List<Municipio> getMunicipios() {
return municipios;
}
public void setMunicipios(List<Municipio> municipios) {
this.municipios = municipios;
}
public int getId_estado() {
return id_estado;
}
public void setId_estado(int id_estado) {
this.id_estado = id_estado;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经为此工作了几个小时,但仍然一无所获,我真的很赶时间,所以如果你在这里给我一些帮助,我将不胜感激。非常感谢您的关注:D
value="municipio"
in<p:selectOneMenu id="Municipio" value="municipio">
意味着该下拉列表中的值永远不会改变,因为您已经有效地将该字段上的值硬编码为始终为municipio
(甚至会导致转换失败)。该value
属性应绑定到支持 bean 变量,如
<p:selectOneMenu id="Municipio" value="#{beanInscripcion.municipio}" >
<f:selectItems value="#{beanInscripcion.municipios}"
var="mun" itemLabel="#{mun.nombre_municipio}" itemValue="#{mun.nombre_municipio}" />
</p:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
在你的支持 bean 中,有
Municipio municipio;
//getter and setter
Run Code Online (Sandbox Code Playgroud)删除参数event
从<p:ajax update="Municipio" listener="#{beanInscripcion.buscarMunicipios(event)}" />
。它应该是
<p:ajax update="Municipio" listener="#{beanInscripcion.buscarMunicipios}" />
Run Code Online (Sandbox Code Playgroud)删除valueChangeListener="#{beanInscripcion.buscarMunicipios(event)}"
。这是不必要的,因为您已经<p:ajax/>
定义了一个事件
您最终会在提交该表单时遇到问题,因为您尚未为该Municipio
自定义类型创建 JSF 转换器。如果您String
在选择的组件中使用类型以外的任何内容,则这是强制性的。在此处查看转换器/转换的简短介绍
我正在使用 Primefaces,这很简单。我有 2 个 seleceOneMenus,一个用于主题(父项),另一个用于主题(子项)。
<h:selectOneMenu id="subject" value="#{QuestionsMB.selectedSubjectId}" var="selectedSubject">
<f:selectItem itemLabel="Select Subject" noSelectionOption="true" />
<f:selectItems value="#{SubjectsMB.subjectsList}" var="selectedSubject" itemValue="#{selectedSubject.id}" itemLabel="#{selectedSubject.subjectName}" />
<p:ajax update="topic" />
</h:selectOneMenu>
<h:selectOneMenu id="topic" value="#{QuestionsMB.selectedTopicId}" var="selectedTopic">
<f:selectItem itemLabel="Select Topic" noSelectionOption="true" />
<f:selectItems value="#{TopicsMB.getTopicsListBySubjectId(QuestionsMB.selectedSubjectId)}" var="selectedTopic" itemValue="#{selectedTopic.id}" itemLabel="#{selectedTopic.topicName}" />
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
请注意,当主题选择菜单更改时,主题菜单会根据主题 ID 更改。通过编辑 hibernate getList() 函数,我在 Topics Managed Bean(子 selectonemenu 的托管 bean)中创建了一个简单的函数,如下所示:
public List<Topics> getTopicsListBySubjectId(String subjectID)
{
Topics topic = new Topics();
List<Topics> TopicsList = new ArrayList<Topics>();
if(subjectID.length() != 0)
{
topic.setSubjectId(Integer.parseInt(subjectID));
TopicsList = getTopicsService().getTopicsBySubjectId(topic);
}
else
{
TopicsList.addAll(getTopicsService().getTopics());
}
return TopicsList;
}
Run Code Online (Sandbox Code Playgroud)
事情就像一个魅力...... :)
归档时间: |
|
查看次数: |
25913 次 |
最近记录: |