1

In my Facelets page I have a <div> and that contains some input text boxes for user to enter values to submit. I will generate the PDF using this HTML source.

<div id="wrapper">
    <h:form prependId="false">
        <h:inputHidden id="source" value="#{bean.source}" />
        <h:inputText id="fName" value="#{bean.firstName}"/>
        <h:inputText id="lName" value="#{bean.lastName}"/>
        <h:inputText id="age" value="#{bean.age}"/>
    </h:form>
</div>

Like this I have other form fields too. When user enters all the data in the form and click on submit button I am getting the source inside this <div> using JavaScript.

function getHtml() {
    document.getElementById('source').value = document.getElementById('wrapper').innerHTML;
}

And passing to bean on click of submit.

<h:commandButton type="submit" id="appl-submit" action="#{bean.submit}"
                 value="Submit" onclick="javascript:getHtml();"/>

In the backing bean class

@ManagedBean
@SessionScoped
public class Bean{

    private String firstName;
    private String lastName;
    private int age;
    private String source;

    // Getters and setters for all properties.
}

But I am only getting the form with empty input fields instead of HTML source along with user entered values. How can I get the HTML source along with form values?

Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
SRy
  • 2,901
  • 8
  • 36
  • 57

1 Answers1

1

But I am only getting the form with empty input fields instead of HTML source along with user entered values.

That's expected behaviour. This approach works only if you've already submitted the form and are thus redisplaying the submitted form (as kind of confirmation or so).


How can I get the HTML source along with form values?

That's already answered in your previous question which you never gave feedback on: Get the URL of current XHTML page which has user data filled in the form into the JSF Managed bean. In your particular case, that can even be done simpler as you've apparently a session scoped bean already (which is however still bad design as the same page in multiple browser windows/tabs in the same session may interfere with each other):

public void submit() throws IOException {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    HttpSession session = (HttpSession) externalContext.getSession(true);
    String url = request.getRequestURL().append(";jsessionid=").append(session.getId()).toString();

    Document doc = Jsoup.connect(url).get();
    String html = doc.select("#wrapper").html();
    // ...
}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC..Sorry that I didn't give feedback.The thing was the approach I posted in OP was working earlier.Suddenly it stopped working these days.I don't what is the problem is .Anyway I will implement this approach.And What if the bean is `RequestedScoped`.Do I need to use the same approach? – SRy Nov 28 '12 at 18:49
  • Then just use the approach as shown in my answer to your previous question. The approach in the current answer isn't much different, it is just relying on the data *already* being present in the session without any necessary pre/post processing. However, keeping this kind of data in the session all the time has other implications which you would probably like to avoid (as already explained in the answer). – BalusC Nov 28 '12 at 18:52
  • If I use the above code snippet for `SessionScoped` bean I am getting `Read Timout Exception`.For this `Model` which import I have to Use. I have three different import for this.1.`com.sun.tools.xjc.model.Model`.2. `org.apache.poi.hssf.model.Model` and another is from Spring Framework?What exactly that `Model`do there? – SRy Nov 28 '12 at 19:07
  • @BalusC...Man You are always life saver...Awesome working like charm.Hey I want to send you something as my thankfulness to you.what Can I? – SRy Nov 28 '12 at 19:27
  • You're welcome. Check [my profile](http://stackoverflow.com/users/157882/balusc). There's a donate link. True, somewhat impersonal, but something like an Amazon.com wishlist just doesn't work for me here on a small island in the Caribbean as they will only send books to here and nothing else. – BalusC Nov 28 '12 at 21:07
  • Hey I didn't see any way to send a book to you.So, I simply donated. – SRy Dec 20 '12 at 07:57