获取当前XHTML页面的URL,其中包含填写在表单中的用户数据到JSF Managed bean中 [英] Get the URL of current XHTML page which has user data filled in the form into the JSF Managed bean

查看:78
本文介绍了获取当前XHTML页面的URL,其中包含填写在表单中的用户数据到JSF Managed bean中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2.0和iText从XHTML创建PDF。该页面是简单的注册表格。当用户输入页面中的所有数据并单击提交时,我必须将带有用户输入值的整个HTML页面源添加到bean中。我使用Jsoup来获取HTML,但我只得到HTML源而不是用户输入的值。我怎么能这样做?

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?

我当前的代码是

        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();


推荐答案

您的Jsoup方法只有在托管bean持有时才有效与视图关联的模型数据已存储在会话范围中。 Jsoup即发出一个全新的HTTP GET请求,这意味着在请求或视图范围bean的情况下,它将获得一个全新且完全不同的实例,所有属性都设置为默认值。

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.

如果你想将bean保存在请求或视图范围内(非常合理),那么你需要在Jsoup调用之前暂时将模型数据放入会话中,并在Jsoup调用之后将其删除。

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.

您的另一个错误是您大写了 JSESSIONID URL路径片段。它区分大小写,实际上它应该全部小写: jsessionid

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.

所以,所有这一切,这应该是如果你想保留你的bean请求或查看范围:

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");

        // ...
    }

}

这篇关于获取当前XHTML页面的URL,其中包含填写在表单中的用户数据到JSF Managed bean中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆