jsf中没有会话生成 [英] No session generation in jsf

查看:100
本文介绍了jsf中没有会话生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSP中,有一个属性 session 用于禁用请求中的自动生成会话.

In JSP, there is an attribute session used for disable autogeneration session in a request.

<%@page contentType="text/html" pageEncoding="UTF-8" session="false" %>

在JSF2中有什么方法可以做同样的事情吗?

Is there any way to do same in JSF2?

谢谢

为什么?因为我们有一个公共注册表单页面作为应用程序中的默认页面.这是一种非常简单的形式,每次有人(或机器人等)请求主页时都会创建会话. ManagedBean是RequestScope,但是JSF在第一个导航请求中创建一个会话.

Why? Because we have a public register form page as a default page in an application. It is a very simple form and session is created everytime people (or robots, etc.) request main page. ManagedBean is RequestScope, but JSF create one session in first navegation request.

推荐答案

仅不使用视图/会话范围的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);
    }

    // ...
}

这篇关于jsf中没有会话生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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