Reputation: 2967
I am working with creating PDF from XHTML with JSF 2.0 and iText. The page is simple registration form. When User enters all the data in the page and click on submit I have to get the whole HTML page source with user entered values into the bean. I used Jsoup to get the HTML but I got only HTML source not the values entered by the user.How can I do this?
My current code is
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpSession session = (HttpSession) externalContext.getSession(true);
String url = "http://localhost:8080/Pro/faces/part1.xhtml;JSESSIONID=" + session.getId();
try {
Document doc = Jsoup.connect(url).get();
Upvotes: 0
Views: 1525
Reputation: 1108802
Your Jsoup approach would only work if the managed bean holding the model data associated with the view is been stored in the session scope. Jsoup is namely firing a fresh new HTTP GET request, which thus means that it would in case of a request or view scoped bean get a brand new and entirely distinct instance, with all properties set to default.
If you want to keep your bean in the request or view scope (very reasonable), then you'd need to temporarily put the model data in the session before the Jsoup call and remove it after the Jsoup call.
Your another mistake is that you uppercased the JSESSIONID
URL path fragment. It's case sensitive and it should in fact be all lowercase: jsessionid
.
So, all with all, this should do if you want to keep your bean request or view scoped:
@ManagedBean
@ViewScoped
public class Bean {
@ManagedProperty("#{beanModel}") // Must match (unique!) session attribute name.
private Model model;
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();
session.setAttribute("beanModel", model);
Document doc = Jsoup.connect(url).get();
session.removeAttribute("beanModel");
// ...
}
}
Upvotes: 2