[Java] Failed to write EL expression in onclick attribute of h:button tag

1 minute read

-Environment -CentOS Linux release 7.8.2003 (Core) -Eclipse IDE for Enterprise Java Developers.Version: 2020-03 (4.15.0) -openjdk version “11.0.7” 2020-04-14 LTS -JSF 2.3.9

Phenomenon: HTTPS status is 500 when the page is displayed

Eclipse console log

2020-08-10T15:17:17.299+0900|WARNING: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.application.ViewExpiredException: viewId:/base.jsf-the view /base.jsf could not be restored.
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:194)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:109)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
    at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)

Cause: EL expression is written in onclick attribute of h:button tag


<h:button value="Upload" onclick="#{uploadBean.onClick}"/>


 * Get the JavaScript code to output for onClick attribute.
 * @return JavaScript code.
public String getOnClick() {
    StringBuilder builder = new StringBuilder();
    // ... omitted ...
    return builder.toString();

I’m not sure why writing EL expressions doesn’t work… ・Is it because I don’t know what the document says? I want someone to explain it in an easy-to-understand manner.

The entire target URL string must be processed by a call to the encodeResourceURL() method of the ExternalContext.The name of the UIParameter goes on the left hand side, and the value of the UIParameter on the right hand side.The name and the value must be URLEncoded. Each UIParameter instance is separeted by an ampersand, as dictated in the URL spec. The final encoded result will be written out to the onclick attribute of the button as “window.location.href = ``“.If the developer has specified a custom onlclick the window.location.href name/value pair will be appended at the end of the developer specified script. button(JSF 2.1 View Declaration Language: Facelets Variant)

(Translated roughly) The target URL must be processed by a call to the externalContext’s encodeResourceURL method. The UIParameter name is displayed on the left and the UIParameter value is displayed on the right. The name and value must be URLEEncoded. Each UIParameter instance is separated by ampersands as directed by the URL specification. The final encoded result is written to the button’s onclick attribute as “window.location.href =’’”. If the developer specifies onlclick, the window.location.href name-value pair is added to the end of the script.

Support: change to type=”button” of input tag


<input type="button" value="Upload" onclick="#{uploadBean.onClick}" />

Question: Writing an EL expression in the onclick attribute of the h:button tag sometimes works.


<h:button value="OK" onclick="submit('#{uploadBean.key}');" />


    public String getKey() {
        return "formId:file";


<input type="button" onclick="submit('formId:file');window.location.href='/tryJsf/upload.jsf'; return false;" value="OK">

Tags: ,