Vaadin:我如何使所有片段网址不区分大小写

时间:2017-08-14 12:05:14

标签: vaadin vaadin7 case-sensitive case-insensitive navigator

在Vaadin中,我通过这样的字符串向导航器添加视图:

navigator.addView("myView", CustomClass.class);

这是区分大小写的。所以这有效:

http://domain/#!myView

这不起作用:

http://domain/#!myview

我希望两个网址都能正常工作,并且最好使其完全不区分大小写。我可以去添加小写版本,但我更喜欢一种适用于所有情况的更干净的方式。我已经有用户知道当前包含大写字母的网址。

有没有办法配置导航器,所以它的工作方式忽略了这种情况?

1 个答案:

答案 0 :(得分:2)

您需要创建一个新的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));