p:dashboard 关闭后保持排序
p:dashboard keep sort order after close
我想使用这个 primefaces 仪表板
https://www.primefaces.org/showcase/ui/panel/dashboard.xhtml
dashboard.xhtml
<div style="height:500px">
<h:form>
<p:growl id="msgs" showDetail="true" />
<p:dashboard id="board" model="#{dashboardView.model}">
<p:ajax event="reorder" listener="#{dashboardView.handleReorder}" update="msgs" />
<p:panel id="sports" header="Sports">
<h:outputText value="Sports Content" />
</p:panel>
<p:panel id="finance" header="Finance">
<h:outputText value="Finance Content" />
</p:panel>
<p:panel id="lifestyle" header="Lifestyle">
<h:outputText value="Lifestyle Content" />
</p:panel>
<p:panel id="weather" header="Weather">
<h:outputText value="Weather Content" />
</p:panel>
<p:panel id="politics" header="Politics">
<h:outputText value="Politics Content" />
</p:panel>
</p:dashboard>
<div style="clear:both" />
</h:form>
</div>
DashBoardView.java
package org.primefaces.showcase.view.panel;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.CloseEvent;
import org.primefaces.event.DashboardReorderEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.model.DashboardColumn;
import org.primefaces.model.DashboardModel;
import org.primefaces.model.DefaultDashboardColumn;
import org.primefaces.model.DefaultDashboardModel;
@ManagedBean
@ViewScoped
public class DashboardView implements Serializable {
private DashboardModel model;
@PostConstruct
public void init() {
model = new DefaultDashboardModel();
DashboardColumn column1 = new DefaultDashboardColumn();
DashboardColumn column2 = new DefaultDashboardColumn();
DashboardColumn column3 = new DefaultDashboardColumn();
column1.addWidget("sports");
column1.addWidget("finance");
column2.addWidget("lifestyle");
column2.addWidget("weather");
column3.addWidget("politics");
model.addColumn(column1);
model.addColumn(column2);
model.addColumn(column3);
}
public void handleReorder(DashboardReorderEvent event) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_INFO);
message.setSummary("Reordered: " + event.getWidgetId());
message.setDetail("Item index: " + event.getItemIndex() + ", Column index: " + event.getColumnIndex() + ", Sender index: " + event.getSenderColumnIndex());
addMessage(message);
}
public void handleClose(CloseEvent event) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Panel Closed", "Closed panel id:'" + event.getComponent().getId() + "'");
addMessage(message);
}
public void handleToggle(ToggleEvent event) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getComponent().getId() + " toggled", "Status:" + event.getVisibility().name());
addMessage(message);
}
private void addMessage(FacesMessage message) {
FacesContext.getCurrentInstance().addMessage(null, message);
}
public DashboardModel getModel() {
return model;
}
}
这是一个非常酷的功能。
我做了一些拖放操作
然后关闭浏览器标签页再打开就全部恢复默认了。
我需要以某种方式保留此订单。
可能以某种方式或任何其他方式使用 cookie。是否可能,如果可以,该怎么做?
重要的是应该为每个用户保存小部件的顺序。我的意思是两个选项之一:
- 每个登录的用户都可以在每个设备上看到这个订单
- 没有登录浏览器,但是当打开新浏览器时,顺序应该是默认的。或者其他一些用户打开那个命令应该是默认的。
肯定不是最佳解决方案,而是解决方案:
我想我会将来自 handleReorder(DashboardReorderEvent event) 的事件保存在数据库或文件中。并在 postconstruct init 中加载订单。你怎么看?
像评论中写的那样编辑无法为我序列化事件。
我的第二次尝试是这样的:
public void handleReorder(DashboardReorderEvent event) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_INFO);
message.setSummary("Reordered: " + event.getWidgetId());
message.setDetail("Item index: " + event.getItemIndex() + ", Column index: " + event.getColumnIndex() + ", Sender index: " + event.getSenderColumnIndex());
addMessage(message);
serializeEvent(event);
}
serializeEvent 方法没有序列化事件,因为这对我来说失败了,但它序列化了一个包含必要信息的对象。
private void serializeEvent(DashboardReorderEvent event) {
String userName = userBean.getUserName();
WhosebugObject e = new WhosebugObject(event);
try {
FileOutputStream fileOut =
new FileOutputStream("c:/tmp/events"+userName+".txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
//out.writeObject(event.getWidgetId()+";"+ event.getItemIndex()+";"+ event.getColumnIndex());
out.close();
fileOut.close();
} catch (Exception i) {
i.printStackTrace();
}
}
在 postconstruct init 中,您读取序列化对象并决定放置小部件的位置。这将向您展示这个想法(它接近 1 a.m。所以我只向您展示开始)
@PostConstruct
public void init() {
model = new DefaultDashboardModel();
DashboardColumn column1 = new DefaultDashboardColumn();
DashboardColumn column2 = new DefaultDashboardColumn();
DashboardColumn column3 = new DefaultDashboardColumn();
WhosebugObject e = deserializeEvent("politics");
if (e != null &&"politics".equalsIgnoreCase(e.getWidgetId()) && e.getColumnId() == 0){
column1.addWidget("politics");
} else if (e != null &&"politics".equalsIgnoreCase(e.getWidgetId()) && e.getColumnId() == 1){
column2.addWidget("politics");
}
else{
column3.addWidget("politics");
}
.. 对所有其他小部件执行相同操作
现在您还有一个问题是针对每个用户执行此操作。 deserializeEvent 方法确定用户并为该用户读取正确的文件。
如果你问自己如何获取用户。在大多数情况下是这样
FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()
如果您有像我示例中那样的 loginbean,您可以从 userBean 中获取它。
编辑 WhosebugObject 如下所示
public class WhosebugObject implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4921468076398836905L;
private String widgetId;
private int columnId;
private int itenId;
public WhosebugObject(DashboardReorderEvent event) {
this.setWidgetId(event.getWidgetId());
this.setColumnId(event.getColumnIndex());
this.setItenId(event.getItemIndex());
}
// +getter 和 setter
我想使用这个 primefaces 仪表板 https://www.primefaces.org/showcase/ui/panel/dashboard.xhtml
dashboard.xhtml
<div style="height:500px">
<h:form>
<p:growl id="msgs" showDetail="true" />
<p:dashboard id="board" model="#{dashboardView.model}">
<p:ajax event="reorder" listener="#{dashboardView.handleReorder}" update="msgs" />
<p:panel id="sports" header="Sports">
<h:outputText value="Sports Content" />
</p:panel>
<p:panel id="finance" header="Finance">
<h:outputText value="Finance Content" />
</p:panel>
<p:panel id="lifestyle" header="Lifestyle">
<h:outputText value="Lifestyle Content" />
</p:panel>
<p:panel id="weather" header="Weather">
<h:outputText value="Weather Content" />
</p:panel>
<p:panel id="politics" header="Politics">
<h:outputText value="Politics Content" />
</p:panel>
</p:dashboard>
<div style="clear:both" />
</h:form>
</div>
DashBoardView.java
package org.primefaces.showcase.view.panel;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.CloseEvent;
import org.primefaces.event.DashboardReorderEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.model.DashboardColumn;
import org.primefaces.model.DashboardModel;
import org.primefaces.model.DefaultDashboardColumn;
import org.primefaces.model.DefaultDashboardModel;
@ManagedBean
@ViewScoped
public class DashboardView implements Serializable {
private DashboardModel model;
@PostConstruct
public void init() {
model = new DefaultDashboardModel();
DashboardColumn column1 = new DefaultDashboardColumn();
DashboardColumn column2 = new DefaultDashboardColumn();
DashboardColumn column3 = new DefaultDashboardColumn();
column1.addWidget("sports");
column1.addWidget("finance");
column2.addWidget("lifestyle");
column2.addWidget("weather");
column3.addWidget("politics");
model.addColumn(column1);
model.addColumn(column2);
model.addColumn(column3);
}
public void handleReorder(DashboardReorderEvent event) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_INFO);
message.setSummary("Reordered: " + event.getWidgetId());
message.setDetail("Item index: " + event.getItemIndex() + ", Column index: " + event.getColumnIndex() + ", Sender index: " + event.getSenderColumnIndex());
addMessage(message);
}
public void handleClose(CloseEvent event) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Panel Closed", "Closed panel id:'" + event.getComponent().getId() + "'");
addMessage(message);
}
public void handleToggle(ToggleEvent event) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getComponent().getId() + " toggled", "Status:" + event.getVisibility().name());
addMessage(message);
}
private void addMessage(FacesMessage message) {
FacesContext.getCurrentInstance().addMessage(null, message);
}
public DashboardModel getModel() {
return model;
}
}
这是一个非常酷的功能。
我做了一些拖放操作
然后关闭浏览器标签页再打开就全部恢复默认了。
我需要以某种方式保留此订单。
可能以某种方式或任何其他方式使用 cookie。是否可能,如果可以,该怎么做?
重要的是应该为每个用户保存小部件的顺序。我的意思是两个选项之一:
- 每个登录的用户都可以在每个设备上看到这个订单
- 没有登录浏览器,但是当打开新浏览器时,顺序应该是默认的。或者其他一些用户打开那个命令应该是默认的。
肯定不是最佳解决方案,而是解决方案:
我想我会将来自 handleReorder(DashboardReorderEvent event) 的事件保存在数据库或文件中。并在 postconstruct init 中加载订单。你怎么看?
像评论中写的那样编辑无法为我序列化事件。
我的第二次尝试是这样的:
public void handleReorder(DashboardReorderEvent event) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_INFO);
message.setSummary("Reordered: " + event.getWidgetId());
message.setDetail("Item index: " + event.getItemIndex() + ", Column index: " + event.getColumnIndex() + ", Sender index: " + event.getSenderColumnIndex());
addMessage(message);
serializeEvent(event);
}
serializeEvent 方法没有序列化事件,因为这对我来说失败了,但它序列化了一个包含必要信息的对象。
private void serializeEvent(DashboardReorderEvent event) {
String userName = userBean.getUserName();
WhosebugObject e = new WhosebugObject(event);
try {
FileOutputStream fileOut =
new FileOutputStream("c:/tmp/events"+userName+".txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
//out.writeObject(event.getWidgetId()+";"+ event.getItemIndex()+";"+ event.getColumnIndex());
out.close();
fileOut.close();
} catch (Exception i) {
i.printStackTrace();
}
}
在 postconstruct init 中,您读取序列化对象并决定放置小部件的位置。这将向您展示这个想法(它接近 1 a.m。所以我只向您展示开始)
@PostConstruct
public void init() {
model = new DefaultDashboardModel();
DashboardColumn column1 = new DefaultDashboardColumn();
DashboardColumn column2 = new DefaultDashboardColumn();
DashboardColumn column3 = new DefaultDashboardColumn();
WhosebugObject e = deserializeEvent("politics");
if (e != null &&"politics".equalsIgnoreCase(e.getWidgetId()) && e.getColumnId() == 0){
column1.addWidget("politics");
} else if (e != null &&"politics".equalsIgnoreCase(e.getWidgetId()) && e.getColumnId() == 1){
column2.addWidget("politics");
}
else{
column3.addWidget("politics");
}
.. 对所有其他小部件执行相同操作
现在您还有一个问题是针对每个用户执行此操作。 deserializeEvent 方法确定用户并为该用户读取正确的文件。
如果你问自己如何获取用户。在大多数情况下是这样
FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()
如果您有像我示例中那样的 loginbean,您可以从 userBean 中获取它。
编辑 WhosebugObject 如下所示
public class WhosebugObject implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4921468076398836905L;
private String widgetId;
private int columnId;
private int itenId;
public WhosebugObject(DashboardReorderEvent event) {
this.setWidgetId(event.getWidgetId());
this.setColumnId(event.getColumnIndex());
this.setItenId(event.getItemIndex());
}
// +getter 和 setter