添加后,我在一个非常简单的JSF 2页面中面临以下异常<h:form>:
java.lang.IllegalStateException: Cannot create a session after the response has been committed
at org.apache.catalina.connector.Request.doGetSession(Request.java:2758)
at org.apache.catalina.connector.Request.getSession(Request.java:2268)
Run Code Online (Sandbox Code Playgroud)
我在Tomcat 7.0.22和JDK 7上使用Mojarra 2.1.3和PrimeFaces3.0M4.
该页面是一个非常基本的数据表:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form>
<p:dataTable var="car" value="#{tableBean.cars}">
......
</p:dataTable>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
页面在浏览器上正确显示,但在控制台上我看到了异常.如果我删除了,Exception确实会消失<h:form>.
这是怎么造成的,我该如何解决?
根据这篇博客, JSF将无国籍.使用JSF的重点不在于它使保存和恢复状态成为一件苦差事.JSF成为无国籍的重点是什么?能否请您提供一个有用的示例.
在JSP中,有一个属性会话用于请求中的禁用自动生成会话.
<%@page contentType="text/html" pageEncoding="UTF-8" session="false" %>
Run Code Online (Sandbox Code Playgroud)
有没有办法在JSF2中做同样的事情?
谢谢
为什么?因为我们将公共注册表单页面作为应用程序中的默认页面.这是一个非常简单的表单,每次人(或机器人等)请求主页时都会创建会话.ManagedBean是RequestScope,但JSF在第一个navegation请求中创建一个会话.
在JSF中,似乎会在成功登录之前创建会话.即,简单地请求登录页面会导致创建新会话.
为每个收到的请求创建会话似乎非常浪费(并且容易受到DDoS攻击),而不是每个成功登录用户.
下面的代码非常通用,但显示了我所指的那种简单场景.
的index.xhtml:
<html>
<body>
<h:form id="login">
<h:outputLabel for="username">Username</h:outputLabel>
<p:inputText id="username" name="username" value="#{userController.username}"/>
<h:outputLabel for="password">Password</h:outputLabel>
<p:password id="password" name="password" value="#{userController.password}"/>
<p:commandButton id="loginButton" value="login" action="#{loginController.login}"/>
</h:form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
LoginController.java
@ViewScoped
public class LoginController implements Serializable {
String username;
String password;
public void login(){
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
if (request.getSession(false) == null){
System.out.println("No session.");
} else {
System.out.println("Session already exists.");
}
try {
request.login(username, password);
} catch (ServletException e) {
FacesContext.getCurrentInstance.addMessage(null, new FacesMessage("Login failure", e.getMessage()));
}
} …Run Code Online (Sandbox Code Playgroud)