Directory

← Back

CDI Utils

Utilities for CDI/Vaadin applications

Author

Contributors

Rating


PROJECT DISCONTINUED! Replaced by the official Vaadin CDI plugin and CDI Properties & CDI MVP add-ons

CDI is a JavaEE standard that consists of two fundamental parts: It allows you to bind bean instances to contexts with well-defined life-cycles and inject instances to beans in a type-safe way.

CDI Utils add-on provides some utilities for CDI/Vaadin applications: -Custom CDI Context for Vaadin -Lightweight MVP framework -Producers for declaratively defined Vaadin components (@Preconfigured -annotation) -Built in i18n of @Preconfigured components

Quick start (CDI Utils 2):

  1. Add empty beans.xml -file (CDI marker file) to your project under WEB-INF dir
  2. Add cdi-utils*.jar to your project
  3. Annotate your UI with @UIScoped
  4. Give the Vaadin servlet an init parameter "UIProvider" with the value "org.vaadin.virkki.cdiutils.application.CdiUIProvider"
  5. Deploy to JavaEE/Web profile -compatible container

Quick start (CDI Utils 1):

  1. Add empty beans.xml -file (CDI marker file) to your project under WEB-INF dir
  2. Add cdiutils*.jar to your project
  3. Create your Application class by extending AbstractCdiApplication
  4. Use CdiApplicationServlet instead of the normal ApplicationServlet
  5. Deploy to JavaEE/Web profile -compatible container (CDI apps can also be run on servlet containers etc. but some further configuration is required)

See the example projects at: CDI Utils 2: https://github.com/tomivirkki/cdiutils-addressbook/tree/vaadin7 CDI Utils 1: https://github.com/tomivirkki/cdiutils-addressbook

Sample code

public class CUDemoApplication extends AbstractCdiApplication {

	@WebServlet(urlPatterns = "/*", initParams = @WebInitParam(name = "application", value = "org.vaadin.virkki.cdiutils.example.CUDemoApplication"))
	public static class CUDemoApplicationServlet extends CdiApplicationServlet {
	}

	@Inject
	private Instance<MainViewImpl> mainView;

	@Override
	public void init() {
		setMainWindow(new Window("demo"));
		getMainWindow().addComponent(mainView.get());
		mainView.get().openView();
	}
}
public class CustomersViewImpl extends AbstractView implements CustomersView {
	@Inject
	@Preconfigured(sizeFull = true)
	private VerticalLayout mainLayout;
	@Inject
	@Preconfigured(caption = "Some data", width = 400, widthUnits = UNITS_PIXELS, enabled = false)
	private TextArea textArea;
	@Inject
	@Preconfigured(captionKey = "update-button-caption", styleName = Reindeer.BUTTON_LINK)
	private Button button;

	@Override
	protected void initView() {
		setCompositionRoot(mainLayout);
		mainLayout.addComponent(textArea);
		button.addListener(new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				// The event is observed in presenter
				fireViewEvent("update", this);
			}
		});
		mainLayout.addComponent(button);
	}

	@Override
	public void updateTextContent(String content) {
		textArea.setValue(content);
	}
}
@ViewInterface(CustomersView.class)
public class CustomersPresenter extends AbstractPresenter<CustomersView> {
	// Access your backend in the presenter
	@EJB
	private CustomersBean customersBean;

	@Override
	protected void initPresenter() {}

	@Override
	public void viewOpened() {
		view.updateTextContent(null);
	}

	public void updateRequested(@Observes @CDIEvent( "update") ParameterDTO param) {
		view.updateTextContent("Updated content");
	}
}
@UIScoped
public class AddressBookUI extends UI {

    @WebServlet(urlPatterns = "/*", initParams = {
            @WebInitParam(name = "UI", value = "org.vaadin.virkki.cdiutils.addressbook.AddressBookUI"),
            @WebInitParam(name = "UIProvider", value = "org.vaadin.virkki.cdiutils.application.CdiUIProvider")})
    public static class AddressBookApplicationServlet extends VaadinServlet {
    }

    @Inject
    private MainViewImpl mainView;

    @Override
    protected void init(final VaadinRequest request) {
        setContent(mainView);
        mainView.openView();
    }
}

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Released
2013-03-03
Maturity
STABLE
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.0+
Vaadin 6.7+ in 1.2.1
Browser
Browser Independent
Online