Skip to content

Commit

Permalink
初始化项目
Browse files Browse the repository at this point in the history
  • Loading branch information
weishiji committed Mar 18, 2017
0 parents commit a36e195
Show file tree
Hide file tree
Showing 35 changed files with 2,152 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
logs
project/project
project/target
target
tmp
.history
dist
/.idea
/*.iml
/out
/.idea_modules
.classpath
.project
/RUNNING_PID
.settings
.target
.cache
bin
.DS_Store
activator-sbt-*-shim.sbt
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
License
-------
Written in 2016 by Lightbend <info@lightbend.com>

To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.

You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[<img src="https://img.shields.io/travis/playframework/play-java-ebean-example.svg"/>](https://travis-ci.org/playframework/play-java-ebean-example)

# play-java-ebean-example

This is an example Play application that uses Java, and communicates with an in memory database using EBean.

The Github location for this project is:

[https://github.com/playframework/play-ebean-example](https://github.com/playframework/play-ebean-example)

## Play

Play documentation is here:

[https://playframework.com/documentation/latest/Home](https://playframework.com/documentation/latest/Home)

## EBean

EBean is a Java ORM library that uses SQL:

[https://www.playframework.com/documentation/2.5.x/JavaEbean](https://www.playframework.com/documentation/2.5.x/JavaEbean)

and

[https://ebean-orm.github.io/](https://ebean-orm.github.io/)

## Play Bootstrap

The Play HTML templates use the Play Bootstrap library:

[https://github.com/adrianhurt/play-bootstrap](https://github.com/adrianhurt/play-bootstrap)

library to integrate Play with Bootstrap, the popular CSS Framework.
20 changes: 20 additions & 0 deletions app/Global.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import play.*;

/**
* Created by lxg on 18/03/2017.
*/


public class Global extends GlobalSettings {

@Override
public void onStart(Application app) {
Logger.info("Application has started");
}

@Override
public void onStop(Application app) {
Logger.info("Application shutdown...");
}

}
20 changes: 20 additions & 0 deletions app/controllers/CustomerController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controllers;

import play.Logger;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;

/**
* Created by lxg on 18/03/2017.
*/
public class CustomerController extends Controller {
@Security.Authenticated
public Result index(){
session("hello", "user@gmail.com");
String user = session("hello");
return ok(
user
);
}
}
137 changes: 137 additions & 0 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package controllers;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.Transaction;
import play.mvc.*;
import play.data.*;
import static play.data.Form.*;

import models.*;

import javax.inject.Inject;
import javax.persistence.PersistenceException;

/**
* Manage a database of computers
*/
public class HomeController extends Controller {

private FormFactory formFactory;

@Inject
public HomeController(FormFactory formFactory) {
this.formFactory = formFactory;
}

/**
* This result directly redirect to application home.
*/
public Result GO_HOME = Results.redirect(
routes.HomeController.list(0, "name", "asc", "")
);

/**
* Handle default path requests, redirect to computers list
*/
public Result index() {
return GO_HOME;
}

/**
* Display the paginated list of computers.
*
* @param page Current page number (starts from 0)
* @param sortBy Column to be sorted
* @param order Sort order (either asc or desc)
* @param filter Filter applied on computer names
*/
public Result list(int page, String sortBy, String order, String filter) {
return ok(
views.html.list.render(
Computer.page(page, 10, sortBy, order, filter),
sortBy, order, filter
)
);
}

/**
* Display the 'edit form' of a existing Computer.
*
* @param id Id of the computer to edit
*/
public Result edit(Long id) {
Form<Computer> computerForm = formFactory.form(Computer.class).fill(
Computer.find.byId(id)
);
return ok(
views.html.editForm.render(id, computerForm)
);
}

/**
* Handle the 'edit form' submission
*
* @param id Id of the computer to edit
*/
public Result update(Long id) throws PersistenceException {
Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest();
if(computerForm.hasErrors()) {
return badRequest(views.html.editForm.render(id, computerForm));
}

Transaction txn = Ebean.beginTransaction();
try {
Computer savedComputer = Computer.find.byId(id);
if (savedComputer != null) {
Computer newComputerData = computerForm.get();
savedComputer.company = newComputerData.company;
savedComputer.discontinued = newComputerData.discontinued;
savedComputer.introduced = newComputerData.introduced;
savedComputer.name = newComputerData.name;

savedComputer.update();
flash("success", "Computer " + computerForm.get().name + " has been updated");
txn.commit();
}
} finally {
txn.end();
}

return GO_HOME;
}

/**
* Display the 'new computer form'.
*/
public Result create() {
Form<Computer> computerForm = formFactory.form(Computer.class);
return ok(
views.html.createForm.render(computerForm)
);
}

/**
* Handle the 'new computer form' submission
*/
public Result save() {
Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest();
if(computerForm.hasErrors()) {
return badRequest(views.html.createForm.render(computerForm));
}
computerForm.get().save();
flash("success", "Computer " + computerForm.get().name + " has been created");
return GO_HOME;
}

/**
* Handle computer deletion
*/
public Result delete(Long id) {
Computer.find.ref(id).delete();
flash("success", "Computer has been deleted");
return GO_HOME;
}


}

24 changes: 24 additions & 0 deletions app/controllers/ProductController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controllers;

import models.*;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;


import javax.sound.sampled.Control;

/**
* Created by lxg on 17/03/2017.
*/
public class ProductController extends Controller{

public Result list(){
Product product = new Product();

return ok(
//"hello World"
Json.toJson(product.list())
);
}
}
18 changes: 18 additions & 0 deletions app/controllers/rest/AuthController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controllers.rest;

import play.mvc.Controller;
import play.mvc.Result;

/**
* Created by lxg on 18/03/2017.
* 用户登录注册
*/
public class AuthController extends Controller {

public Result save(){

return ok(
"Hello"
);
}
}
39 changes: 39 additions & 0 deletions app/models/Company.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package models;

import java.util.*;
import javax.persistence.*;

import play.db.ebean.*;
import play.data.validation.*;



/**
* Company entity managed by Ebean
*/
@Entity
public class Company extends com.avaje.ebean.Model {

private static final long serialVersionUID = 1L;

@Id
public Long id;

@Constraints.Required
public String name;

/**
* Generic query helper for entity Company with id Long
*/
public static Find<Long,Company> find = new Find<Long,Company>(){};

public static Map<String,String> options() {
LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
for(Company c: Company.find.orderBy("name").findList()) {
options.put(c.id.toString(), c.name);
}
return options;
}

}

59 changes: 59 additions & 0 deletions app/models/Computer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package models;

import java.util.*;
import javax.persistence.*;

import com.avaje.ebean.Model;
import play.data.format.*;
import play.data.validation.*;

import com.avaje.ebean.*;

/**
* Computer entity managed by Ebean
*/
@Entity
public class Computer extends Model {

private static final long serialVersionUID = 1L;

@Id
public Long id;

@Constraints.Required
public String name;

@Formats.DateTime(pattern="yyyy-MM-dd")
public Date introduced;

@Formats.DateTime(pattern="yyyy-MM-dd")
public Date discontinued;

@ManyToOne
public Company company;

/**
* Generic query helper for entity Computer with id Long
*/
public static Find<Long,Computer> find = new Find<Long,Computer>(){};

/**
* Return a paged list of computer
*
* @param page Page to display
* @param pageSize Number of computers per page
* @param sortBy Computer property used for sorting
* @param order Sort order (either or asc or desc)
* @param filter Filter applied on the name column
*/
public static PagedList<Computer> page(int page, int pageSize, String sortBy, String order, String filter) {
return
find.where()
.ilike("name", "%" + filter + "%")
.orderBy(sortBy + " " + order)
.fetch("company")
.findPagedList(page, pageSize);
}

}

Loading

0 comments on commit a36e195

Please sign in to comment.