varun
varun

Reputation: 726

Integrating spring and vaadin

Is it good to integrate Spring and vaadin? I am looking to use vaadin in view layer and spring for my services. So far I am not able to find any neat solution for integration. Is it even a good idea for Production applications like management solutions or ERP?

Also could anyone share the advantages and disadvantages of this integration over spring MVC.

Upvotes: 2

Views: 2708

Answers (4)

mendlik
mendlik

Reputation: 61

Did you consider using Vaadin UIProvider mechanism. This way autowiring in UI is totally transparent.

You can have a look at a really simple example that uses this solution on github: spring-vaadin-example

Upvotes: 1

Enerccio
Enerccio

Reputation: 251

You don't need any special vaadin addons for spring at all. Just use aspectj and @Configurable annotation along with @Autowired for every component you want to integrate with spring. Like this:

@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;
    }

}

Of course you need add support to the 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>

Upvotes: 0

michaldo
michaldo

Reputation: 4637

Take a look also on Spring UI scope add-on http://vaadin.com/addon/spring-ui-scope The add-on defines custom Spring scope: UI-scope, which pass well with Vaadin application.

There is also sample application using the scope.

Upvotes: 0

enkara
enkara

Reputation: 6349

You have a very useful add-on for Vaadin called SpringVaadinIntegration.

You can keep a clean separation very easily with Vaadin, just use Spring @Autowired and services for the data retrieval and modification. I've used Spring security and I had no problems with Vaadin. You can manage the scope with the @Scope annotation, with three differents values, if I remember correctly: Singleton (default), Prototype and Session.

Upvotes: 2

Related Questions