ServerRpc 无法在自定义 Vaadin Widgetset 中工作,为什么我会收到“Widgetset '...' 不包含 '...' 的实现?
ServerRpc not working in a custom Vaadin Widgetset, why do I get "Widgetset '...' does not contain implementation for '...'?
我对 Vaadin 中的 ServerRpc 实现有疑问,我在 Eclipse 中有一个带有小部件集的简单示例项目:
这里是 classes:
widgetpackage.MarqueeLabel :
public class MarqueeLabel extends AbstractComponent {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public MarqueeLabelState getState() {
return (MarqueeLabelState) super.getState();
}
@Override
public void setCaption(String caption) {
getState().text = caption;
}
}
widgetpackage.WidgetUI:
public class WidgetUI extends UI {
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = WidgetUI.class, widgetset = "widgetpackage.widgetWidgetset")
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
MarqueeLabel label = new MarqueeLabel();
label.setCaption("Hello I am custom widget!");
layout.addComponent(label);
}
}
widgetpackage.client.MarqeeLabelWidget:
public class MarqueeLabelWidget extends Label {
public MarqueeLabelWidget() {
final Element el = getElement();
el.getStyle().setPosition(Position.RELATIVE);
Timer timer = new Timer() {
private int left = 0;
@Override
public void run() {
el.getStyle().setLeft(left, Unit.PX);
left++;
}
};
timer.scheduleRepeating(100);
}
}
widgetpackage.client.MarqueeLabelState:
public class MarqueeLabelState extends AbstractComponentState {
/**
*
*/
private static final long serialVersionUID = 1L;
public String text;
}
widgetpackage.client.MarqueeLabelConnector:
@Connect(MarqueeLabel.class)
public class MarqueeLabelConnector extends AbstractComponentConnector {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Widget createWidget() {
return GWT.create(MarqueeLabelWidget.class);
}
@Override
public MarqueeLabelWidget getWidget() {
return (MarqueeLabelWidget) super.getWidget();
}
@Override
public MarqueeLabelState getState() {
return (MarqueeLabelState) super.getState();
}
@Override
public void onStateChanged(StateChangeEvent e) {
super.onStateChanged(e);
getWidget().setText(getState().text);
}
}
WidgetWidgetset.gwt.xml 位于 widgetpackage 包中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
<module>
<inherits name="com.vaadin.DefaultWidgetSet" />
</module>
当我构建小部件集时 'Run on server' 一切正常,但是当我添加这个 ServerRpc 实现时,如下所示:
1)添加widgetpackage.MarqueeLabelServerRpc接口(widgetpackage,MarqueeLabel服务器组件所在):
public interface MarqueeLabelServerRpc extends ServerRpc {
public void clicked();
}
2) 将此行添加到 widgetpackage.client.MarqueeLabelConnector:
public class MarqueeLabelConnector extends AbstractComponentConnector {
private MarqueeLabelServerRpc rpc = RpcProxy.create(
MarqueeLabelServerRpc.class, this);
public MarqueeLabelConnector() {
getWidget().addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
rpc.clicked(); // server's call
}
});
}
// ...
}
3) widgetpackage.MarqueeLabel 的构造函数:
public class MarqueeLabel 扩展了 AbstractComponent {
public MarqueeLabel() {
registerRpc(new MarqueeLabelServerRpc() {
@Override
public void clicked() {
Notification.show("Made a RPC”);
}
});
}
// ...
}
如果我重新编译 widgetset 和 运行 项目,我会在浏览器中得到这个:
Widgetset 'widgetpackage.widgetWidgetset' does not contain
implementation for 'widgetpackage.MarqueeLabel'. Check its component
connector's @Connect mapping, widgetsets GWT module description file
and re-compile your widgetset. In case you have downloaded a vaadin
add-on package, you might want to refer to add-on instructions.
为什么当我添加 RPC 实现时,widgetset 找不到 MarqueeLabel 的实现?
我已经解决了,问题是 MarqueeLabelServerRpc 需要在 widgetpackage.client 包内而不是 widgetpackage 内。
我对 Vaadin 中的 ServerRpc 实现有疑问,我在 Eclipse 中有一个带有小部件集的简单示例项目:
这里是 classes:
widgetpackage.MarqueeLabel :
public class MarqueeLabel extends AbstractComponent {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public MarqueeLabelState getState() {
return (MarqueeLabelState) super.getState();
}
@Override
public void setCaption(String caption) {
getState().text = caption;
}
}
widgetpackage.WidgetUI:
public class WidgetUI extends UI {
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = WidgetUI.class, widgetset = "widgetpackage.widgetWidgetset")
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
MarqueeLabel label = new MarqueeLabel();
label.setCaption("Hello I am custom widget!");
layout.addComponent(label);
}
}
widgetpackage.client.MarqeeLabelWidget:
public class MarqueeLabelWidget extends Label {
public MarqueeLabelWidget() {
final Element el = getElement();
el.getStyle().setPosition(Position.RELATIVE);
Timer timer = new Timer() {
private int left = 0;
@Override
public void run() {
el.getStyle().setLeft(left, Unit.PX);
left++;
}
};
timer.scheduleRepeating(100);
}
}
widgetpackage.client.MarqueeLabelState:
public class MarqueeLabelState extends AbstractComponentState {
/**
*
*/
private static final long serialVersionUID = 1L;
public String text;
}
widgetpackage.client.MarqueeLabelConnector:
@Connect(MarqueeLabel.class)
public class MarqueeLabelConnector extends AbstractComponentConnector {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Widget createWidget() {
return GWT.create(MarqueeLabelWidget.class);
}
@Override
public MarqueeLabelWidget getWidget() {
return (MarqueeLabelWidget) super.getWidget();
}
@Override
public MarqueeLabelState getState() {
return (MarqueeLabelState) super.getState();
}
@Override
public void onStateChanged(StateChangeEvent e) {
super.onStateChanged(e);
getWidget().setText(getState().text);
}
}
WidgetWidgetset.gwt.xml 位于 widgetpackage 包中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
<module>
<inherits name="com.vaadin.DefaultWidgetSet" />
</module>
当我构建小部件集时 'Run on server' 一切正常,但是当我添加这个 ServerRpc 实现时,如下所示:
1)添加widgetpackage.MarqueeLabelServerRpc接口(widgetpackage,MarqueeLabel服务器组件所在):
public interface MarqueeLabelServerRpc extends ServerRpc {
public void clicked();
}
2) 将此行添加到 widgetpackage.client.MarqueeLabelConnector:
public class MarqueeLabelConnector extends AbstractComponentConnector {
private MarqueeLabelServerRpc rpc = RpcProxy.create(
MarqueeLabelServerRpc.class, this);
public MarqueeLabelConnector() {
getWidget().addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
rpc.clicked(); // server's call
}
});
}
// ...
}
3) widgetpackage.MarqueeLabel 的构造函数: public class MarqueeLabel 扩展了 AbstractComponent {
public MarqueeLabel() {
registerRpc(new MarqueeLabelServerRpc() {
@Override
public void clicked() {
Notification.show("Made a RPC”);
}
});
}
// ...
}
如果我重新编译 widgetset 和 运行 项目,我会在浏览器中得到这个:
Widgetset 'widgetpackage.widgetWidgetset' does not contain implementation for 'widgetpackage.MarqueeLabel'. Check its component connector's @Connect mapping, widgetsets GWT module description file and re-compile your widgetset. In case you have downloaded a vaadin add-on package, you might want to refer to add-on instructions.
为什么当我添加 RPC 实现时,widgetset 找不到 MarqueeLabel 的实现?
我已经解决了,问题是 MarqueeLabelServerRpc 需要在 widgetpackage.client 包内而不是 widgetpackage 内。