6

handleNavigation()メソッドによってビューをリダイレクトすると、長時間実行される会話の伝播に問題があります。これが私のテストコードです:

私はconversationscoped豆と2つのビューを持っています:

conversationStart.xhtmlブラウザでURLを使用して呼び出されます

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>

ボタンをクリックしてスタートページに戻ると、会話Beanにはまだすべての値が含まれています。だからすべてが大丈夫です。これが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;
 }

}

今問題が来る!

preRenderViewBeanのメソッドでページをリダイレクトしようとすると(メソッドのコードのコメントを解除するだけで)、handleNavigation()すでに作成されているインスタンスを使用する代わりに、次のビューでBeanの使用が再度作成されます。cidパラメータは次のビューに伝播されますが!

誰かが何が悪いのか考えていますか?

よろしくトーマス

4

0 に答える 0