PWC6228: #{…} not allowed in a template text body

2019-01-12 12:29发布

问题:

I have a JS script which is called when a submit button action is fired successfully:

<h:panelGroup rendered="#{user$webreports$webfilteroverview.submitted}">
    <f:verbatim>
    <script  type="text/javascript">alert('Done!');</script>
    </f:verbatim>
</h:panelGroup>

the above code works perfect. What I want to do is to get the alert box text from resource bundle:

<script  type="text/javascript">alert('#{msg.report_alert_text}');</script>

but I get error:

PWC6228: #{...} not allowed in a template text body.

I did this:

<h:commandbutton onClick="alert('#{msg.report_alert_text}');"/> 

and it was working fine. I don't understand why the above code doesn't work. Is it possible to do this? If yes, what is wrong with the above code? Thanks in advance.

回答1:

PWC6228: #{...} not allowed in a template text body.

You're apparently using the legacy JSP(X) instead of its successor Facelets. Deferred EL #{} in template text is not supported by JSP(X). It only supports standard EL ${} in template text (template text means outside tags / JSF components):

<script type="text/javascript">alert('${msg.report_alert_text}');</script>

If that doesn't work because ${msg} is not been prepared (the #{} will namely autocreate it if it does not exist yet at that point of the view), then you need <h:outputText> instead:

<script type="text/javascript">alert('<h:outputText value="#{msg.report_alert_text}" />');</script>

You'll only need to remove that <f:verbatim> tag in order to get JSF components to run there. The <f:verbatim> is a leftover from JSF 1.0/1.1 and not necessary anymore since JSF 1.2 and deprecated since JSF 2.1.

This problem has nothing to do with JavaScript. You got the error from the webserver, not from the webbrowser.