Search code examples
htmljsf-2parameters

How to get current view HTML source and pass it JSF ManagedBean as String


I want to get current view HTML source and pass it to JSF managed bean as a parameter. Is there anyway in JSF I can do this?


Solution

  • You can't do it in JSF as it doesn't run in the webbrowser at all.

    Your best bet is using JavaScript. The below example sets a hidden input value with the HTML source code when the submit button is clicked:

    <h:form id="form">
        <h:inputHidden id="source" value="#{bean.source}" />
        <h:commandButton value="submit" action="#{bean.submit}" 
            onclick="document.getElementById('form:source').value=document.getElementsByTagName('html')[0].outerHTML"
        />
    </h:form>
    

    In the action method, it's in the particular example then just available by the source property.