JSF <h:selectOneMenu> valueChangeListener does not work

flyasfish picture flyasfish · Jul 27, 2012 · Viewed 10.1k times · Source

Hi I have a jsp page like this

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>title</title>
</head>
<body>
<f:view>
    <f:loadBundle basename="amdocs.gcreport.messages" var="msg" />
    <h:panelGrid columns="1">
        <h:outputLabel value="#{msg.environmens}"></h:outputLabel>
    </h:panelGrid>
    <h:panelGrid columns="1">
        <h:selectOneMenu id="selectEnv" value="#{gCAnalyzerController.envName}" onchange="submit();" valueChangeListener="#{gCAnalyzerController.envValueChange}" >
                <f:selectItems value="#{gCAnalyzerController.envs}" var="env" itemValue="#{env.name}"/>
        </h:selectOneMenu>  
    </h:panelGrid>
.
.
.

whenever I change value in this dropdown listbox, the function envValueChange should be called in the bean class. However when run this piece of code in IE I got exception "Error: Object expected", when trying to debug in google chrome, I got the below exception.

Uncaught ReferenceError: submit is not defined GCAnalyzerView.jsp:15 onchange

It looks like the javascript submit() is not defined anywhere so the page can not load it, can anyone tell me where and how should I implement this javascript method?

Answer

Cœur picture Cœur · May 12, 2018

Solution by OP.

The code should be included in a form tag for submit.

<h:form>
    <h:panelGrid columns="1">
        <h:outputLabel value="#{msg.environmens}"></h:outputLabel>
    </h:panelGrid>
    <h:panelGrid columns="1">
        <h:selectOneMenu id="selectEnv" value="#{gCAnalyzerController.envName}" onchange="submit();" valueChangeListener="#{gCAnalyzerController.envValueChange}" >
                <f:selectItems value="#{gCAnalyzerController.envs}" var="env" itemValue="#{env.name}"/>
        </h:selectOneMenu>  
    </h:panelGrid>
</h:form>