且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jsf中没有会话生成

更新时间:2023-12-04 18:28:52

仅不使用视图/会话范围的bean(因此仅使用请求或应用程序范围的bean)并将状态保存设置为client而不是(默认)通过在web.xml中设置以下上下文参数来实现server.

Just don't use view/session scoped beans (so use only request or application scoped beans) and set state saving to client instead of (default) server by setting the following context parameter in web.xml.

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

然后,JSF将不会创建会话,并在必要时将视图状态存储在表单中名称为javax.faces.ViewState的隐藏输入字段中.

Then JSF won't create the session and will store the view state in a hidden input field with the name javax.faces.ViewState in the form whenever necessary.

但是,创建和管理会话的成本几乎可以忽略不计.另外,在使用客户端视图状态保存功能时,您仍然必须权衡将视图状态(反序列化)和网络带宽的费用.

The cost of creating and managing the sessions is however pretty negligible. Also, you still have to tradeoff the cost of (de)serializing the view state and the network bandwidth when using client side view state saving.

更新:

@BalusC是的,这可能是一个全局解决方案.但是我只需要在此公共页面上使用此方法.在其他页面中,我需要服务器端状态保存方法.

对.抱歉,在JSF/Facelets中,我看不到任何禁用会话或更改每个请求的视图状态保存的好方法.我考虑使用纯HTML <form>代替JSF <h:form>,让它提交到另一个JSF页面,并在与JSF页面关联的bean中使用@ManagedProperty.例如

Ah right. Sorry, I don't see any nice ways in JSF/Facelets to disable the session or change the view state saving on a per-request basis. I'd consider to use a plain HTML <form> instead of a JSF <h:form>, let it submit to another JSF page and make use of @ManagedProperty in the bean associated with the JSF page. E.g.

<form action="register.jsf" method="post">
    <input type="text" name="username" />
    <input type="password" name="password" />
    <input type="submit" />
</form>

使用

@ManagedBean
@RequestScoped
public class Register {

    @ManagedProperty(value="#{param.username}")
    private String username;

    @ManagedProperty(value="#{param.password}")
    private String password;

    @PostConstruct
    public void init() {
        // Do your thing here.
        System.out.println("Submitted username/password: " + username + "/" + password);
    }

    // ...
}