我该怎么做? https://vaadin.com/book/-/page/advanced.urifu.html#figure.advanced.urifu
我想使用标签(例如本页&#34;图11.12,带有URI片段实用程序的应用程序状态管理&#34;)如果用户点击它,则浏览器会关注该段落。< / p>
答案 0 :(得分:2)
一种方法是在收到UI.scrollIntoView(Component)后使用方法UriFragmentChangedEvent。另一种方法是使用JavaScript。 SSCCE(MainUI类):
VerticalLayout layout = new VerticalLayout();
Label up = new Label("Up");
Label middle = new Label("Middle");
Label down = new Label("Down");
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = QwertUI.class)
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
Button buttonUp = new Button("Up");
Button buttonMiddle = new Button("Middle");
Button buttonDown = new Button("Down");
buttonUp.addClickListener(this);
buttonMiddle.addClickListener(this);
buttonDown.addClickListener(this);
up.setHeight("666px");
middle.setHeight("666px");
down.setHeight("666px");
layout.addComponents(buttonUp, buttonMiddle, buttonDown, up, middle, down);
setContent(layout);
getPage().addUriFragmentChangedListener(
new UriFragmentChangedListener() {
public void uriFragmentChanged(
UriFragmentChangedEvent source) {
enter(source.getUriFragment());
}
});
enter(getPage().getUriFragment());
}
private void enter(String uriFragment){
UI ui = up.getUI();
if(uriFragment != null){
switch(uriFragment){
case "Up": ui.scrollIntoView(up);; break;
case "Middle": ui.scrollIntoView(middle); break;
case "Down": ui.scrollIntoView(down); break;
}
}
}
@Override
public void buttonClick(ClickEvent event)
{
getPage().setUriFragment(event.getButton().getCaption());
}