我有一个Liferay Vaadin portlet,它具有两种模式:编辑和查看模式。我在portlet中看到的第一件事是带有标签的viewContent:“未配置,如果未配置portlet。现在,如果我在Editmode中配置portlet,我会看到我在配置中所做的事情,到目前为止但是现在,如果我注销或重新启动浏览器(退出并重新启动),我会看到带有标签的未配置viewContent(“未配置”)

代码:

Window window; // Main Window
VerticalLayout viewContent; // View Mode Content
VerticalLayout editContent; // Edit Mode Content(Configs)
Label viewText;
Button b;
Panel panel;
Embedded PictureA;

public void init() {
    window = new Window("");
    setMainWindow(window);
    viewContent = new VerticalLayout();
    editContent = new VerticalLayout();
    PictureA = new Embedded("", new ExternalResource(PictureAURL));
    PictureA.setType(Embedded.TYPE_IMAGE);

    panel = new Panel();
    panel.setStyleName(Reindeer.PANEL_LIGHT);

    // viewContent
    viewText = new Label("Not Configured" , Label.CONTENT_XHTML);
    viewContent.addComponent(viewText);
    window.setContent(viewContent);

    // EditContent
    b = new Button("PictureA");
    b.addListener(this):
    editContent.addComponent(b);
}

public void buttonClick(ClickEvent event) {
    if (event.getSource == b) {
        viewContent.revomeComponent(viewText);
        panel.addComponent(PictureA);
        viewContent.addComponent(panel);
    }
}

@Override
public void handleRenderRequest(RenderRequest request,
        RenderResponse response, Window window) {

}

@Override
public void handleActionRequest(ActionRequest request,
        ActionResponse response, Window window) {

}

@Override
public void handleEventRequest(EventRequest request,
        EventResponse response, Window window) {

}

@Override
public void handleResourceRequest(ResourceRequest request,
        ResourceResponse response, Window window) {

    // Switch the view according to the portlet mode
    if (request.getPortletMode() == PortletMode.EDIT)
        window.setContent(editContent);
    else if (request.getPortletMode() == PortletMode.VIEW)
        window.setContent(viewContent);
}


情况:如果单击按钮“ PictureA”,则将删除“未配置”标签,并将带有嵌入式图片的面板添加到viewContent。

唯一的问题是它没有被保存:/有什么想法吗?也许我忘记了什么?

最佳答案

是的,您不会在任何地方保存配置。当会话结束(关闭浏览器)并重新打开时,将再次执行应用程序初始化,并恢复原始的“未配置”状态。

例如,您可以将其存储到portlet首选项中。在handleResourceRequest方法中,您必须抓住PortletPreferences的句柄:

this.prefs = request.getPreferences();


要将状态保存在按钮单击处理程序中,请执行以下操作:

this.prefs.setValues("myapp.configured", new String[] {"true"});
this.prefs.store();


另外,您还想还原handleResourceRequest方法中的状态。做类似的事情:

boolean configured = Boolean.parseBoolean(this.prefs.getValues("myapp.configured", new String[] { "false" })[0]);

if (configured && PictureA.getParent() == null) {
    // PictureA is not visible, but it should be.
    viewContent.removeComponent(viewText);
    panel.addComponent(PictureA);
    viewContent.addComponent(panel);
}

07-24 09:36
查看更多