Skip to content

Single Class Java WebFramework for high productivity - with templating, orm, dynamic reloads - to focus on building webapps

License

Notifications You must be signed in to change notification settings

vivekvrao/jagoframework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jagoframework

public class JagoFrameworkSample extends JagoServer {
	
    /**
     * In main - specify the port, location of your files, and configuration to use (can specify web.xml.dev / uat here for testing)
     */
    public static void main(String[] args) throws Exception {
        startFromWar(8082, "war", "WEB-INF/web.xml");
    }    
    
    /**
     * Setup the system - using configuration. 
     */
    @Override public void init(ServletConfig config) throws ServletException {
        try {
            // setup the database - from config parameter DB or in code as in initDb("jdbc:h2:file:./data", "org.h2.Driver", null, null);
            initDb(config.getInitParameter("DB"));
            
            // the framework provides a dev time admin - init it here if specified in config 
            initManager(config.getInitParameter("ADMIN_PAGE"));

            // mustache is the built in templating mechanism - it can be configured as such here 
			mustache.setDefaultValue("-"); // optional
			if(config.getInitParameter("MODE") == "DEV")
				mustache.disableCaching(); // only for testing 

			// Jago Framework provides a pure POJO ORM - where class can be linked to table by calling
			getDb().linkClassToTable(Greeting.class, "Greeting", "id", true); // last 3 params are Table name, PrimaryKey and If key is autogenerated
            
			// a call to this function - exposes the table as a Rest Service - details to come
            // exposeRestService("greetings", Greeting.class, "json", new String[]{"lang"});

            testORM();
        } catch (Exception e) {
            warn("Exception ", e);
        }

        // for Java 7 or earlier - you can register controller - using anonymous class
        // any url parameters to be extracted need to be specified - 2nd argument
        // a test string allows you to easily test the controller
        addController(GET, "/welcome/:lang", "lang=es;name=Jim;", new IController() {
			@Override
			public Response execute(Request req) throws Exception {
		        // url params and query params can all be extracted as req.param(name) 
				String greet = "es".equals(req.pathParam("lang")) ? "Hola" : "Hello";
				
		        // response is a Template - if name is null - raw text is returned
				return new TemplResponse(null, greet+ ' '+ req.param("name"));
			}
		});
        
        // Java 8 and above can use lambda expressions or function pointers
        addController(GET, "/welcome1", "", (req) -> {
        	// template can take mustache template - and add data to it as below 
            return new TemplResponse(null, "{{> templates/first.tmpl}}" //"Mustache template {{greet.greeting}}"
            		).add("greet", new Greeting("en", "Hello!!!!"));
        });

        // Java 8 + - this is the more convenient way of definiting controllers - just a pointer to a function
        addController(GET, "/welcome2/:lang", "lang=en;name=Jack;", this::showGreeting);
    }

    // model classes are just POCO - no annotations 
    public static class Greeting{
    	public Greeting(){} // default constructor is required on model - if another constructor is specified
    	public Greeting(String lang, String gr){this.lang = lang; this.greeting = gr; this.updateDate = new Date();}
        public String lang, greeting;
        public int id;
        public Date updateDate;
    }

    /**
     * In this test - we insert an object into the DB - retrieve it update it and print it.
     * Ofcourse - before this can be done - you have to create the table - which can be done on the admin page.
     */
	private void testORM() throws SQLException {
		Greeting esHola = new Greeting("es", "hola");
		getDb().insert(esHola);
		getDb().insert(new Greeting("en", "Hello"));
		
		esHola.greeting = "Hola!";
		getDb().update(esHola);
		
		Greeting obj2 = getDb().getById(Greeting.class, esHola.id);
		System.out.println(obj2.greeting);
	}
    
    public Response showGreeting(Request req) throws SQLException{
        String lang = ( req.pathParam("lang") == null ? "en" : req.pathParam("lang"));
        		
        Greeting greet = getDb().readOne(Greeting.class, " lang = ? ", lang);
        return new TemplResponse("default", "Mustache template {{greet.greeting}}").add("greet", greet);
    }
}

About

Single Class Java WebFramework for high productivity - with templating, orm, dynamic reloads - to focus on building webapps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages