GithubHelp home page GithubHelp logo

marceloemanoel / vraptor-js-controller Goto Github PK

View Code? Open in Web Editor NEW
7.0 6.0 2.0 5.11 MB

Javascript Controller Generated to Help Ajax Communications

Groovy 14.01% Shell 20.58% Java 62.03% JavaScript 3.38%

vraptor-js-controller's Introduction

VRAPTOR-JS-CONTROLLER

The plugin's main idea is to help the interaction between ajax clients and vraptor. As for now, it depends on jQuery to do ajax communication.

INSTALATION

The plugin is self contained and doesn't require any special configuration on your project. Just put the jar in your classpath and all should be ready to go. If you use dependency management systems like Maven or Gradle you're just a few lines from joy!

Maven Dependecy

Add the following to your pom.xml:

  <dependency>
    <groupId>com.github.marceloemanoel</groupId>
    <artifactId>vraptor-js-controller</artifactId>
    <version>0.2</version>
  </dependency>

Gradle Dependency

As shown below, just add the these lines to your build.gradle file:

  repositories {
    mavenCentral()
  }

  dependencies {
    compile "com.github.marceloemanoel:vraptor-js-controller:0.2"
  }

As for last, but not the least, you can always build the project from source and add vraptor-js-controller.jar file on your WEB-INF/lib folder. Instructions on how to build the project are listed bellow.

On the web page you want to use the controller just add the following line:

    <script type="text/javascript" src="<c:url value='/js/ControllerName'/>"></script>

to use a minified version of it include the following line:

    <script type="text/javascript" src="<c:url value='/js/min/ControllerName'/>"></script>

For instance suppose that exist a ProductsController than you should insert the following snippet:

    <script type="text/javascript" src="<c:url value='/js/ProductsController'/>"></script>

Usage

Suppose you have the following controller on your application:

    //package and imports ommited..
    
    @Resource
    @Path("products")
    public class ProductsController {
  
      private Result result;
      private Products products;
      
      public ProductsController(Result result, Products products) {
        this.result = result;
        this.products = products;
      }
      
      @Get
      @Path("/")
      public void index(){
      }
       
      @Get
      @Path("/list")
      public void list() {
        result.use(json()).withoutRoot().from(products.all()).recursive().serialize();
      }
      
      @Get
      @Path("/{id}")
      public void show(int id) {
        result.use(json()).withoutRoot().from(products.select(id)).recursive().serialize();
      }
      
      @Post
      @Path("/new")
      public void newProduct(Product product){
        products.add(product);
        result.use(status()).ok();
      }
      
      @Put
      @Path("/{id}")
      public void update(int id, Product product){
        products.update(id, product);
        result.use(status()).ok();
      }
      
      @Delete
      @Path("/{id}")
      public void remove(int id) {
        products.remove(id);
        result.use(status()).ok();
      }
    }

Given that controller your client code could be something like this:

index.jsp:

include the js controller:

     <script type="text/javascript" src="<c:url value='/js/ProductsController'/>"></script>

and then create another script block with client code:

    <script type="text/javascript">
        var controller = ProductsController();    
        controller.list({
                onSuccess : function(results) {
                              var console = $("#console");
                              console.text('');
                              $.each(results, function(index, product) {
                                  console.append("<p>product.description: " + product.description + " - product.price: " + product.price + "</p>");
                              });
                }
        });
    </script>

All js controller methods can receive an object with the following properties:

onSuccess: Callback to treat the successful result from VRaptor controller
onError: Callback to treat an unsuccessful result from VRaptor controller
data: An object with the parâmeters that will be delivered to VRaptor controller

COMPILING FROM SOURCE-CODE

  • open the command line

  • cd to the root folder

  • type

      ./gradlew jar
    

BUILD WITH ECLIPSE

To use eclipse just do the following:

  • open the command line

  • cd to the root folder

  • type:

      ./gradlew eclipse
    
  • open eclipse

  • import project to the workspace

  • I use the following code conventions, when contributing, please use it.

RUN THE SAMPLE

To run the sample code:

  • open the command line

  • cd to the sample folder

  • type:

      ../gradlew jettyRunWar
    

It's also possible to run from eclipse WTP.

THANKS

To Caelum, thank you so much for this great framework!

To Douglas Crockford, thank you for the wonderful book JavaScript: The Good Parts it helped me a lot.

To Cristhiano Milfont, some of his ideas from the course Javascript Fundamental were used to build this plugin.

To Handerson Frota, after a talk with him some functionalities came to light.

vraptor-js-controller's People

Contributors

marceloemanoel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

felipethi jarey

vraptor-js-controller's Issues

Add method overloading support to generated javascript

If a VRaptor controller has two or more methods with the same name (Java supports method overloading but JavaScript doesn't) the javascript controller is generated with problems in its routes. But I'm not sure what would be the real behavior in production when calling a duplicated action from javascript controller.

The plugin should analyze the duplicated methods and defaults to one of them, maybe the method with a @get route.

Fail when try to inject Velocity Generator

We're having a little problem with the plugin here. All of the computers works, but there's one that when try to load the javascript fails trying to inject Velocity Generator.

the stacktrace generated is down:

Failed to load resource: the server responded with a status of 500 (Error creating bean with name 'jsController': Unsatisfied dependency expressed through constructor argument with index 2 of type [br.com.vraptor.contrib.jscontroller.JsGenerator]: : Error creating bean with name 'velocityJsGenerator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.vraptor.contrib.jscontroller.VelocityJsGenerator]: Constructor threw exception; nested exception is org.apache.velocity.exception.VelocityException: Error initializing log: Failed to initialize an instance of org.apache.velocity.runtime.log.Log4JLogChute with the current runtime configuration.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'velocityJsGenerator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.vraptor.contrib.jscontroller.VelocityJsGenerator]:)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.