整合Spring和vaadin

瓦伦

整合Spring和vaadin很好吗?我希望在视图层和Spring中使用vaadin来提供服务。到目前为止,我还没有找到任何用于集成的灵巧解决方案。对于生产应用程序(例如管理解决方案或ERP),这甚至是个好主意吗?

  • 应用程序的设计是什么?
  • 如何保持应用程序层之间的清晰分隔?
  • Vaadin与Spring Security集成的问题?
  • 如何管理春豆的范围?

与spring MVC相比,任何人都可以分享这种集成的优点和缺点。

Enerccio

您根本不需要在春季使用任何特殊的vaadin插件。对于要与spring集成的每个组件,只需使用aspectj和@Configurable注解@Autowired像这样:

@Configurable(preConstruction = true)
public class LoginUserPasswdDialog extends LoginDialogBase {
static final Logger log = Logger.getLogger(LoginUserPasswdDialog.class);

    @Autowired
    private AppConfig config;

    @Autowired
    UserFactory userFactory;

    StringBuffer name;
    LoggedAction action;

    protected Window parent = null;
    protected Button ok;
    protected Label l;
    protected TextField nameText;
    protected PasswordField password;
    protected CheckBox saveUserPass;

    protected final Window w = new Window("");

    @SuppressWarnings("serial")
    public void create(AbstractComponent component) throws Exception {
        parent = component.getWindow();

        VerticalLayout v = new VerticalLayout();
        v.setSizeFull();
        v.setSpacing(true);

        l = new Label(
                _.getString("LoginUserPasswdDialog.0"), Label.CONTENT_XHTML); //$NON-NLS-1$
        l.setSizeFull();
        l.addStyleName("centeredLabel");
        v.addComponent(l);

        HorizontalLayout h = new HorizontalLayout();
        h.setMargin(true);
        h.setSpacing(true);

        nameText = new TextField();
        nameText.setWidth("100%");
        v.addComponent(nameText);
        nameText.focus();

        password = new PasswordField();
        password.setWidth("100%");
        v.addComponent(password);

        saveUserPass = new CheckBox(_.getString("LoginUserPasswdDialog.1")); //$NON-NLS-1$
        v.addComponent(saveUserPass);
        v.setComponentAlignment(saveUserPass, Alignment.MIDDLE_RIGHT);

        ok = new Button(_.getString("LoginUserPasswdDialog.2")); //$NON-NLS-1$
        ok.setWidth("100px");
        ok.setClickShortcut(KeyCode.ENTER);

        h.addComponent(ok);
        h.setComponentAlignment(ok, Alignment.MIDDLE_CENTER);

        v.addComponent(h);
        v.setComponentAlignment(h, Alignment.MIDDLE_CENTER);

        Cookie nameCookie = CookieUtils.getCookie("username");
        Cookie passCookie = CookieUtils.getCookie("password");

        if (nameCookie != null && passCookie != null) {
            nameText.setValue(nameCookie.getValue());
            password.setValue(passCookie.getValue());
            saveUserPass.setValue(true);
        }

        w.setWidth("400px");
        w.setCaption(config.getTitle() + _.getString("LoginUserPasswdDialog.4"));
        w.setResizable(false);
        w.setClosable(false);
        w.setModal(true);
        w.center();

        ok.addListener(new ClickListener() {
            public void buttonClick(ClickEvent event) {
                String name = (String) nameText.getValue();
                String pass = (String) password.getValue();

                User u = userFactory.getUser(name, pass);

                if (u != null) {

                    if ((Boolean) saveUserPass.getValue()) {
                        CookieUtils.makeCookie("username", name);
                        CookieUtils.makeCookie("password", pass);
                    } else {
                        CookieUtils.deleteCookie("username");
                        CookieUtils.deleteCookie("password");
                    }

                    userFactory.updateUser(u);

                    action.loggedIn(u);
                    parent.removeWindow(w);
                    return;

                } else {
                    password.setValue("");
                    WaresystemsUI.handle
                            .get()
                            .getMainWindow()
                            .showNotification(
                                    "",
                                    _.getString("LoginUserPasswdDialog.3"), Notification.TYPE_ERROR_MESSAGE); //$NON-NLS-1$
                    return;
                }
            }

        });

        w.addComponent(v);

        parent.addWindow(w);
    }

    @Override
    public void setAction(LoggedAction loggedAction) {
        this.action = loggedAction;
    }

}

当然,您需要向web.xml添加支持:

<!-- SPRING -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/application-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>requestContextFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
    <init-param>
        <param-name>threadContextInheritable</param-name>
         <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>requestContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章