在Vaadin中,我通过如下所示的字符串将视图添加到导航器中:
navigator.addView("myView", CustomClass.class);
这区分大小写。所以这有效:
http://domain/#!myView
这不起作用:
http://domain/#!myview
我希望两个网址都能正常工作,最好使其完全不区分大小写。我也可以添加小写版本,但我更喜欢适用于所有情况的更干净的方式。我已经有用户知道当前带有大写字母的网址。
有没有办法配置导航器,使其以忽略大小写的方式工作?
您需要创建一个新ViewProvider
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewProvider;
public class CaseInsensitiveClassBasedViewProvider implements ViewProvider {
private static final long serialVersionUID = 1L;
private String viewName;
private final Class<? extends View> viewClass;
public CaseInsensitiveClassBasedViewProvider(String viewName, Class<? extends View> viewClass) {
if (null == viewName || null == viewClass) {
throw new IllegalArgumentException(
"View name and class should not be null");
}
this.viewName = viewName;
this.viewClass = viewClass;
}
@Override
public String getViewName(String viewAndParameters) {
if (null == viewAndParameters) {
return null;
}
String viewAndParametersU = viewAndParameters.toUpperCase();
String viewNameU = viewName.toUpperCase();
if (viewAndParametersU.equals(viewNameU) || viewAndParametersU.startsWith(viewNameU + "/")) {
return viewName;
}
return null;
}
@Override
public View getView(String viewName) {
if (this.viewName.equalsIgnoreCase(viewName)) {
try {
View view = viewClass.newInstance();
return view;
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return null;
}
}
然后不是添加addView
添加:
navigator.addProvider(new CaseInsensitiveClassBasedViewProvider("myView", CustomClass.class));