【问题标题】:Integrating spring and vaadin集成 spring 和 vaadin
【发布时间】:2013-10-04 06:49:52
【问题描述】:

Spring和vaadin集成好不好?我希望在视图层和 spring 中使用 vaadin 来提供我的服务。到目前为止,我无法找到任何巧妙的集成解决方案。对于管理解决方案或 ERP 等生产应用程序来说,这甚至是一个好主意吗?

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

还有人可以分享这种集成相对于 Spring MVC 的优缺点。

【问题讨论】:

    标签: spring spring-security vaadin


    【解决方案1】:

    您有一个非常有用的 Vaadin 插件,称为 SpringVaadinIntegration。

    您可以使用 Vaadin 轻松保持干净的分离,只需使用 Spring @Autowired 和服务进行数据检索和修改。 我使用过 Spring 安全性,并且对 Vaadin 没有任何问题。 如果我没记错的话,您可以使用 @Scope 注释管理范围,使用三个不同的值:Singleton(默认)、Prototype 和 Session。

    【讨论】:

    【解决方案2】:

    您是否考虑过使用 Vaadin UIProvider 机制。这种方式在 UI 中自动装配是完全透明的。

    您可以在 github 上查看使用此解决方案的一个非常简单的示例:spring-vaadin-example

    【讨论】:

      【解决方案3】:

      你根本不需要任何特殊的 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>
      

      【讨论】:

        【解决方案4】:

        还可以查看 Spring UI 范围附加组件 http://vaadin.com/addon/spring-ui-scope 该插件定义了自定义的 Spring 范围:UI-scope,可以很好地与 Vaadin 应用程序一起使用。

        还有使用范围的示例应用程序。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-02-11
          • 2015-10-05
          • 2011-12-29
          • 1970-01-01
          • 2015-05-27
          • 2012-12-29
          • 1970-01-01
          相关资源
          最近更新 更多