2012-12-31 66 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中没有看到这一切,这一切都非常有意义 –