2012-12-31 69 views
0

我正在創建一個portlet,其中在視圖和編輯模式下適用。我想要一種情況,並且更新將portlet從編輯模式切換到視圖模式。下面是我的代碼片斷Viewscoped託管的Bean和Portlets

@ManagedBean(name = "portletBackingBean") 
@ViewScoped 
public class FirstPortlet extends GenericFacesPortlet implements Serializable { 


private transient Logger logger = LoggerFactory.getLogger(getClass()); 

private void doActionResponse(PortletMode mode){ 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    ExternalContext externalContext = 
    facesContext.getExternalContext(); 
    ActionResponse actionresponce = (ActionResponse) externalContext.getResponse(); 
    try { 
     actionresponce.setPortletMode(mode); 
    } catch (PortletModeException e) { 
     // TODO Auto-generated catch block 
     LiferayFacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error setting property")); 
    } 
} 


private String userName; 

/** 
* @return the userName 
*/ 
public String getUserName() { 
    return userName; 
} 

/** 
* @param userName the userName to set 
*/ 
public void setUserName(String userName) { 
    this.userName = userName; 
} 


//submitting the values 
public void doSubmit(){ 
    if(this.userName != null) { 
     logger.debug("value of property in backing bean set to " + getUserName()); 
     doActionResponse(PortletMode.VIEW); 
    } 


} 

到目前爲止,一切都很好,但隨後在該portlet視圖模式呈現的#{portletBackingBean.userName}值爲null。

請有沒有提前做這個

感謝

回答

2

有這個代碼一些嚴重的缺陷,更優雅的方式。

@ManagedBean(name = "portletBackingBean") 
@ViewScoped 
public class FirstPortlet extends GenericFacesPortlet implements Serializable { 
//... 
    private String userName; 

的門戶...

  • 是始終應用​​範圍的
  • 必須是線程安全的
  • 不能是託管bean
  • 不能有每個用戶的狀態(例如userName

每當portletBackingBean已解決,它將導致JSF框架創建FirstPortlet類的新實例。它不會返回對包含它的portlet實例的引用。

另外,如果您對編輯和查看portlet模式使用不同的視圖,則@ViewScoped不適合此狀態。

總之,我認爲您需要再次關注您的模型設計,並弄清楚如何將狀態從Portlet功能中分離出來。

+0

非常感謝,在2011年的Liferay中沒有看到這一切,這一切都非常有意義 –