2010-11-04 55 views
9

在JSP中,有一个属性会话用于在请求中禁用自动生成会话。在jsf中没有会话生成

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

有没有办法在JSF2中做同样的事情?

谢谢

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

+0

@BalusC增加了为什么有问题。 – angelcervera 2010-11-04 13:02:56

回答

7

只要不使用视图/会话范围豆(所以使用唯一的请求或应用程序作用域bean),并在web.xml设置以下环境参数保存到client的(默认值),而不是server设置状态。

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

那么JSF不会创建会话,并且将视图状态在隐藏的输入字段的名称javax.faces.ViewState形式存放在有需要时。

创建和管理会话的成本是相当可以忽略的。此外,在使用客户端视图状态保存时,您仍然必须权衡(de)序列化视图状态和网络带宽的成本。


更新按您的评论:

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

啊对了。对不起,我没有在JSF/Facelets中看到任何好的方法来禁用会话,或者在每个请求的基础上更改视图状态保存。我会考虑使用普通的HTML <form>而不是JSF <h:form>,让它提交到另一个JSF页面并在与JSF页面关联的bean中使用@ManagedProperty。例如。

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

    // ... 
} 
2

从BalusC其实答案并不因为钻嘴鱼科2.1.19/2.2.0正确的。你可以在他的博客here中阅读关于这个。 现在完全够用只设置:

<f:view transient="true"> 
    Your regular content 
</f:view> 

正如提到的职位是说:

不被创建的视图状态,因此会议也没有产生的时候不会被创建。 [...]请记住将相关的托管bean放在请求范围内,而不是视图/会话范围内,否则您只是击败了“无状态”一词的含义。

接着代码段存在于com.sun.faces.application.view.FaceletViewHandlingStrategy类:

 /* 
     * Make sure we have a session here if we are using server state 
     * saving. The WriteBehindStateWriter needs an active session when 
     * it writes out state to a server session. 
     * 
     * Note if you flag a view as transient then we won't acquire the 
     * session as you are stating it does not need one. 
     */ 
     if (isServerStateSaving() && !viewToRender.isTransient()) { 
      getSession(ctx); 
     }  

另外要注意,一些第三方组件库,例如ICEfaces或其他框架,例如Apache Shiro可以为了某些目的自己创建会话。