CDI @Conversation没有使用handleNavigation()传播

时间:2011-01-05 11:11:52

标签: redirect navigation scope jsf-2 cdi

当我通过handleNavigation()方法重定向视图时,我遇到了传播长时间对话的问题。这是我的测试代码:

我有一个conversationscoped bean和两个视图:

在浏览器中使用网址

调用

conversationStart.xhtml

  

http://localhost/tests/conversationStart.jsf?paramTestId=ParameterInUrl

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml"
         xmlns:h="http://java.sun.com/jsf/html"
         xmlns:f="http://java.sun.com/jsf/core">

   <f:metadata>
       <f:viewParam name="paramTestId" value="#{conversationTest.fieldTestId}" />
       <f:event type="preRenderView" listener="#{conversationTest.preRenderView}" />
   </f:metadata>
   <h:head>
     <title>Conversation Test</title>
   </h:head>
   <h:body>
     <h:form>
       <h2>Startpage Test Conversation with Redirect</h2>
       <h:messages />
       <h:outputText value="Testparameter: #{conversationTest.fieldTestId}"/><br />
       <h:outputText value="Logged In: #{conversationTest.loggedIn}"/><br />
       <h:outputText value="Conversation ID: #{conversationTest.convID}"/><br />
       <h:outputText value="Conversation Transient: #{conversationTest.convTransient}"/><br />
       <h:commandButton action="#{conversationTest.startLogin}" value="Login ->" rendered="#{conversationTest.loggedIn==false}" /><br />
       <h:commandLink action="/tests/conversationLogin.xhtml?faces-redirect=true" value="Login ->" rendered="#{conversationTest.loggedIn==false}" /><br />
     </h:form>
     <h:link outcome="/tests/conversationLogin.xhtml" value="Login Link" rendered="#{conversationTest.loggedIn==false}">
      <f:param name="cid" value="#{conversationTest.convID}"></f:param>
     </h:link>
   </h:body>
</html>

参数将写入beanfield并正确显示在视图中。导航到下一个视图有3种不同的可能性。所有3个工作正常。 beanfield也会显示下一个视图(conversationLogin.xhtml):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml"
         xmlns:h="http://java.sun.com/jsf/html"
         xmlns:f="http://java.sun.com/jsf/core">

   <h:head>
     <title>Conversation Test</title>
   </h:head>
   <h:body>
     <h:form>
       <h2>Loginpage Test Conversation with Redirect</h2>
       <h:messages />
       <h:outputText value="Testparameter: #{conversationTest.fieldTestId}"/><br />
       <h:outputText value="Logged In: #{conversationTest.loggedIn}"/><br />
       <h:outputText value="Conversation ID: #{conversationTest.convID}"/><br />
       <h:outputText value="Conversation Transient: #{conversationTest.convTransient}"/><br />
       <h:commandButton action="#{conversationTest.login}" value="Login And Return" /><br />
     </h:form>
   </h:body>
</html>

当我通过单击按钮返回到Startpage时,会话bean仍然包含所有值。一切都很好。这是豆子:

package test;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.event.ComponentSystemEvent;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ConversationScoped
public class ConversationTest implements Serializable{

 private static final long serialVersionUID = 1L;

 final String CONVERSATION_NAME="longRun";

 @Inject Conversation conversation;

 private boolean loggedIn;
 private String fieldTestId;

 @PostConstruct
 public void init(){
  if(conversation.isTransient()){
   conversation.begin(CONVERSATION_NAME);
   System.out.println("New Conversation started");
  }
  loggedIn=false;
 }

 public String getConvID(){
  return conversation.getId();
 }
 public boolean isConvTransient(){
  return conversation.isTransient();
 }

 public boolean getLoggedIn(){
  return loggedIn;
 }

 public String startLogin(){
  return "/tests/conversationLogin.xhtml?faces-redirect=true";
 }

 public String login(){
  loggedIn=true;
  return "/tests/conversationStart.xhtml?faces-redirect=true";
 }

 public void preRenderView(ComponentSystemEvent ev) {
//  if(!loggedIn){
//   System.out.println("Will redirect to Login");
//   FacesContext ctx = FacesContext.getCurrentInstance();
//   ctx.getApplication().getNavigationHandler().handleNavigation(ctx, null, "/tests/conversationLogin.xhtml?faces-redirect=true");
//            ctx.renderResponse();
//  }
 }

 public void setFieldTestId(String fieldTestId) {
  System.out.println("fieldTestID was set to: "+fieldTestId);
  this.fieldTestId = fieldTestId;
 }

 public String getFieldTestId() {
  return fieldTestId;
 }

}

现在出现问题!!

我尝试在bean的 preRenderView 方法中重定向页面(只需取消注释方法中的代码),使用 handleNavigation() < / strong>在下一个视图中再次创建bean,而不是使用已创建的实例。虽然cid参数会传播到下一个视图!

有人知道什么是错的吗?

最好的问候 托马斯

0 个答案:

没有答案