简体   繁体   English

使用会话中的信息呈现JSF页面

[英]Rendering JSF pages using information from sessions

I have the following JSF page: 我有以下JSF页面:

<h:head>
    <title>Admin Page</title>        
</h:head>
<h:body>

    <h1>Users List</h1>

    <h:dataTable value="#{adminBean.linkArr}" var="o" >

        <h:column>
            <f:facet name="header">User name</f:facet>
            #{o.username}
        </h:column>

    </h:dataTable>        

</h:body>

And a managed bean like this: 像这样的托管bean:

@ManagedBean(name="adminBean")
@SessionScoped
public class AdminBean {

private static UserLink[] linkArr;

public AdminBean() {
    FacesContext context = FacesContext.getCurrentInstance();  
    HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();  
    HttpSession session = request.getSession();

    String values[] = (String[])request.getSession().getAttribute("userList");

    int userNos = values.length;
    linkArr = new UserLink[userNos];

    for(int i = 0; i < userNos; ++i) {
        String tmp = values[i];
        linkArr[i] = new UserLink(tmp);
    }                
}
public UserLink[] getLinkArr() {
    return linkArr;
}

public static class UserLink {

    public String username;

    public UserLink(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }
}
}

What I am actually trying to do is: 我实际上要做的是:

Save some data from a previously called servlet by creating a session. 通过创建会话来保存先前调用的servlet中的一些数据。 Assuming that a managed session bean is only created when its associated XHTML file is called, I am getting the attributes from the session object and using then to populate a JSF table. 假设只在调用其关联的XHTML文件时创建托管会话bean,我从会话对象获取属性,然后使用填充JSF表。

However, instead of a JSF table, I am getting the following output: 但是,我得到以下输出,而不是JSF表:

Users List

User name #{o.username}

Am I assuming a wrong lifecycle for a managed session bean or there is some problem in my JSF page? 我是否为托管会话bean假设生命周期错误,或者我的JSF页面存在问题?

So, EL isn't evaluated at all? 那么,EL根本没有评估? That can only mean that the current request URL (as you see in browser's address bar) didn't match the <url-pattern> of the FacesServlet as configured in webapp's web.xml . 这只能意味着当前请求URL(如您在浏览器的地址栏中看到的)与Web应用程序web.xml配置的FacesServlet<url-pattern>不匹配。 It's namely the one responsible for parsing the XHTML file into a JSF component tree and letting it to evaluate all EL expressions and generate valid HTML code. 它是负责将XHTML文件解析为JSF组件树并让它评估所有EL表达式并生成有效HTML代码的人。 If you have done rightclick, View Source in the webbrowser, you should also have noticed that: all JSF tags are left unparsed instead of that their HTML representation is been generated. 如果你已经在webbrowser中右击了View Source ,你也应该注意到:所有的JSF标签都是未解析的,而不是生成它们的HTML表示。

So, to fix this problem, you need to make sure that the current request URL matches the URL pattern of the faces servlet. 因此,要解决此问题,您需要确保当前请求URL与faces servlet的URL模式匹配。 So, if you have mapped it on *.jsf , then you should open the page by page.jsf instead of by page.xhtml . 所以,如果你映射了它*.jsf ,那么你应该打开网页page.jsf而不是由page.xhtml Alternatively, you can also just change the URL pattern of the faces servlet in web.xml to be *.xhtml , so that you never need to worry about virtual URLs. 或者,您也可以将web.xml faces servlet的URL模式更改为*.xhtml ,这样您就不必担心虚拟URL。

Please note that this problem is unrelated to the session management. 请注意,此问题会话管理无关 You might perhaps run into problems related to that sooner or later given the strange design, but that's thus a different problem. 考虑到奇怪的设计,你可能迟早会遇到与之相关的问题,但这是一个不同的问题。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM