4

私の問題はこの問題に似ています。現在、 として注釈が付けられた単一のプロパティを持つBaseBean@ManagedPropertyあります。

ただし、コマンド ボタンのアクション メソッドでこの継承された管理プロパティのゲッターにアクセスすると、null が返されます。上記のリンクで既に説明されているように、ベース Bean コンストラクターが 2 回呼び出されたことをデバッグして確認しました。

記事の選択された回答とこの投稿に記載されている提案に従いましたが、役に立ちませんでした。

以下は私のコードです:

public abstract class BaseBean
{
   @ManagedProperty(value = "#{serviceLocator}")
   private IServiceLocator serviceLocator;

   public IServiceLocator getServiceLocator() {
      return serviceLocator;
   }

   public void setServiceLocator(IServiceLocator serviceLocator) {
      this.serviceLocator = serviceLocator;
   }
}

@ManagedBean
@ViewScoped
public class RegistrationBean extends BaseBean implements Serializable
{
   private static final long serialVersionUID = -6449858513581500971L;

   private String userID;
   private String password;
   private String firstName;
   private String lastName;
   private String email;
   private String addressLine1;
   private String addressLine2;
   private String city;
   private String state;
   private String pincode;

   private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationBean.class);

   /* getter / setters */

   public String register() 
   {
      String nextPage = null;
      try {
         RegistrationDetails userDetails = ModelBuilder.populateRegistrationData(this);
         int registrationID = getServiceLocator().getUserService().registerUser(userDetails);
         LOGGER.info("Registered user successfully. Registration ID - {}", registrationID);
         nextPage = "success";
      }
      catch (RegistrationException e) {
         LOGGER.error(e.getMessage());
      }
      return nextPage;
   }

   public void checkUserExists() 
   {
      int regID = getServiceLocator().getUserService().findUser(getUserID());

      if(regID > 0) {
         FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "User already exists !!", null);
         FacesContext.getCurrentInstance().addMessage(null, message);
      }
   }
}

フォーム送信時にコンストラクターが再度呼び出されるのはなぜですか??? :/

checkUserExists()userID フィールドの blur イベントで ajax 経由で呼び出されたメソッドでも getter は null を返します。

編集: ServiceLocator のコードを追加..

@ManagedBean
@ApplicationScoped
public class ServiceLocator implements IServiceLocator
{
   private static final String USER_SERVICE = "userService";
   private static final String MOVIE_SERVICE = "movieService";

   @PostConstruct
   public void init() {
      final ServletContext sc = FacesUtils.getServletContext();
      this.webAppContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
      this.userService = (IUserService) webAppContext.getBean(USER_SERVICE);
      this.movieService = (IMovieService) webAppContext.getBean(MOVIE_SERVICE);
   }

   private ApplicationContext webAppContext;

   private IUserService userService;

   private IMovieService movieService;

   @Override
   public IUserService getUserService() {
      return userService;
   }

   @Override
   public IMovieService getMovieService() {
      return movieService;
   }
}
4

2 に答える 2

1

私の知る限り、2つの答えを混ぜようとしています.1つは@RequestScopedmbeans用で、もう1つはmbeans用@ViewScopedです。投稿した最初のリンクが表示されている場合、BalusC は、ViewParam と@ManagedProperty(value = “#{param.id})”</a>に示されているように、mbeans を使用@ManagedPropertyする必要はないと言っています。@ViewScoped

ビュー パラメーターを介して渡すことができない場合はserviceLocator、その値を取得する別の方法を見つける必要があります (セッションからの保存/取得)。

また、@ViewScoped要求ごとに mbean が再作成される理由を説明する BalusC からのこの情報を確認してください。

一言で言えば、 @ViewScoped は、バインド属性を使用して UIComponent が Bean にバインドされている場合、またはビューで JSTL またはタグを使用している場合に壊れます。どちらの場合も、Bean はリクエスト スコープのように動作します。私の意見では、最初のものはかなり重大なバグです。

これは JSF 2.0 issue 1492 に関連しています。関連性の抜粋を以下に示します。ビューは、デルタ状態が適用されるにビューを設定するために実行されるため、説明した動作が表示されます。現時点では、このユース ケースを解決する明確な方法はわかりません。ビュー スコープのバインディングを使用する必要がある場合の回避策は、javax.faces.PARTIAL_STATE_SAVING を false に設定することです。

から


@ApplicationScopedコメントと編集に基づいて、次のコードを使用して mbean にアクセスできます。

これは次の行になります。

FacesContext.getCurrentInstance().getExternalContext()
    .getApplicationMap().get("serviceLocator");

どうやら、@ViewScopedBean は による注入を受け入れることができないため、そのコードを使用する必要があります@ManagedProperty

于 2012-11-04T17:27:58.767 に答える
0

のコードServiceLocatorは表示されていないため、問題に対する回答を特定するのに役立つ未解決の質問がいくつかあります。コメントしたり質問したりしますが、何かが「あはは」のきっかけになるかもしれません。あなたのために:

  • ServiceLocatorが実際に で注釈されていることを確認し@ManagedBeanます。

  • ターゲット Bean よりも短いスコープ (寿命) の Bean を注入できないことに注意してください。この場合ServiceLocator、たとえば の場合、 Bean@RequestScopedに注入することはできません。@ViewScoped

  • IServiceLocatorこれは、マネージド Bean によって実装されたインターフェイスであると想定する必要がありますServiceLocatorか?

  • ServiceLocatorEJB のようなにおいがします。@EJBそうである場合は、 EJB の注入に使用します。

于 2012-11-04T17:21:15.410 に答える