CountController.java 957 B

1234567891011121314151617181920212223242526272829303132333435
  1. package controllers;
  2. import javax.inject.*;
  3. import play.*;
  4. import play.mvc.*;
  5. import services.Counter;
  6. /**
  7. * This controller demonstrates how to use dependency injection to
  8. * bind a component into a controller class. The class contains an
  9. * action that shows an incrementing count to users. The {@link Counter}
  10. * object is injected by the Guice dependency injection system.
  11. */
  12. @Singleton
  13. public class CountController extends Controller {
  14. private final Counter counter;
  15. @Inject
  16. public CountController(Counter counter) {
  17. this.counter = counter;
  18. }
  19. /**
  20. * An action that responds with the {@link Counter}'s current
  21. * count. The result is plain text. This action is mapped to
  22. * <code>GET</code> requests with a path of <code>/count</code>
  23. * requests by an entry in the <code>routes</code> config file.
  24. */
  25. public Result count() {
  26. return ok(Integer.toString(counter.nextCount()));
  27. }
  28. }