11.9.3中的示例代码。处理Book of Vaadin中的URI片段路径不适用于v7.6.7。它在v7.6.6上做得很好。
如何使它工作?
Page.setUriFragmnet 可以做一些类似的任务,但它还有另一个问题。
答案 0 :(得分:1)
我使用 UriFragmentChangedListener 来解决问题 在视图中,使用 UI.getCurrent()。getPage()。setUriFragment(" string-fragment",true)。一个提示是使用!作为片段字符串的第一个字符。
以下是View处理程序的典型格式。
package kr.re.nsr.secdiv.inseog.navi.testnavi;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@SuppressWarnings("serial")
public class AdminView extends VerticalLayout implements View {
static int count = 0;
Label label = null;
public AdminView() {
UI.getCurrent().getPage().addUriFragmentChangedListener(ev -> {
String fragment = ev.getUriFragment();
if(!fragment.startsWith("!admin"))
return;
label.setValue("Admin : " + count + "/" + fragment);
});
setMargin(true);
setSpacing(true);
Button btnAAA = new Button("Admin aaa", e -> {
this.getUI().getPage().setUriFragment("!admin/aaa", true);
});
Button btnBBB = new Button("Admin bbb", e -> {
this.getUI().getPage().setUriFragment("!admin/bbb", true);
});
Button btnQuery = new Button("Goto query", e -> {
TestNavi.navigator.navigateTo("query");
});
label = new Label();
addComponent(btnAAA);
addComponent(btnBBB);
addComponent(btnQuery);
addComponent(label);
}
@Override
public void enter(ViewChangeEvent event) {
System.out.println("Admin ENTER begin:" + event.getParameters());
String v = event.getParameters();
if (v.isEmpty())
label.setValue(++count + " : Admin EMPTY EVENT");
else
label.setValue(++count + " : " + v);
System.out.println("Admin ENTER finish");
}
}
Follwing是视图更改效果测试的另一个视图代码。
package kr.re.nsr.secdiv.inseog.navi.testnavi;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@SuppressWarnings("serial")
public class QueryView extends VerticalLayout implements View {
Label label = null;
static int count = 0;
public QueryView() {
setMargin(true);
setSpacing(true);
UI.getCurrent().getPage().addUriFragmentChangedListener(ev -> {
String fragment = ev.getUriFragment();
if(!fragment.startsWith("!query"))
return;
label.setValue("Query : " + count + "/" + fragment);
});
Button btnAAA = new Button("Query aaa", e -> {
this.getUI().getPage().setUriFragment("!query/aaa", true);
});
Button btnBBB = new Button("Query bbb", e -> {
this.getUI().getPage().setUriFragment("!query/bbb", true);
});
Button btnAdmin = new Button("Goto admin", e -> {
TestNavi.navigator.navigateTo("admin");
});
label = new Label();
addComponent(btnAAA);
addComponent(btnBBB);
addComponent(btnAdmin);
addComponent(label);
}
@Override
public void enter(ViewChangeEvent event) {
System.out.println("Query ENTER begin:" + event.getParameters());
String v = event.getParameters();
if (v.isEmpty())
label.setValue(++count + " : Query EMPTY EVENT");
else
label.setValue(++count + " : " + v);
System.out.println("Query ENTER finish");
}
}
Follwing是定义导航器对象的主控制UI。
package kr.re.nsr.secdiv.inseog.navi.testnavi;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.UI;
@SuppressWarnings("serial")
@Theme("TestNavi")
@Widgetset("kr.re.nsr.secdiv.inseog.navi.testnavi.WidgeTestNavi")
public class TestNavi extends UI {
public static Navigator navigator = null;
private QueryView qv = null;
private AdminView av = null;
private StartView sv = null;
@Override
protected void init(VaadinRequest vaadinRequest) {
navigator = new Navigator(this, this);
qv = new QueryView();
av = new AdminView();
sv = new StartView();
getPage().setTitle("서인석이 시험중입니다.");
navigator.addView("", sv);
navigator.addView("query", qv);
navigator.addView("admin", av);
}
@WebServlet(urlPatterns = "/*", name = "TestNaviServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = TestNavi.class, productionMode = false)
public static class TestNaviServlet extends VaadinServlet {
}
}
以下是开始查看。
package kr.re.nsr.secdiv.inseog.navi.testnavi;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
@SuppressWarnings("serial")
public class StartView extends VerticalLayout implements View {
public StartView() {
setSizeFull();
Button button = new Button("GO to Main View", new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
TestNavi.navigator.navigateTo("query");
}
});
addComponent(button);
setComponentAlignment(button, Alignment.MIDDLE_CENTER);
}
@Override
public void enter(ViewChangeEvent event) {
Notification.show("TestNavi");
}
}
解决方案关键点如下
1.为每个视图添加 URI片段处理程序
2.对于视图中的状态更改,请使用 setUriFragment
3.对于setUriFragment,不要忘记使用!作为第一个角色
4.最后,片段更改处理程序必须检查片段字符串owenership 。如果字符串不是您的,请立即返回。另一个处理程序将扮演他们的角色
这是Vaadin 7.2.7版本发布的解决方案。在v7.6.6之前,navigateTo在单个视图状态更改中做得很好。但是v7.6.7 navigateTo不会在视图中触发输入事件以进行片段更改
瓦丹可能会在某一天恢复输入火力逻辑