文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Java 在学习四十二【原创】  


Java Play:

/Users/houhuiyang/Web/Java/play-2.2.1/anan

../play run 默认端口9000

../play "~run 9091"


/usr/libexec/java_home -verbose 版本


export JAVA_HOME=$(/usr/libexec/java_home -v1.7)


 ../play eclipse


在:Application

package controllers;


import java.util.ArrayList;

import java.util.List;


import play.*;

import play.data.DynamicForm;

import play.data.Form;

import play.mvc.*;

import views.html.*;

import models.*;


public class Application extends Controller {


    public static Result index() {

        //return ok(index.render("Your new application is ready."));

    //return ok("Hello Anan安安").as("text/html");

    List<String> lines = new ArrayList<String>();

    lines.add("aaa");

    lines.add("bbb");

    lines.add("ccc");

    

    List<Person> persons = Person.findAll();

    

    return ok(views.html.index.render("Anan Home","Anan欢迎您", lines, persons));

    }

    

public static Result somePost() {

// return ok("<h1>See you</h1>").as("text/html");

DynamicForm in = Form.form().bindFromRequest();

String result = in.get("uname");

Person p1 = new Person();

p1.name = result;

p1.save();

return ok(result);

//redirect("/index");

}

    

    public static Result show(Integer id) {

    return ok("Show Id:" + id.toString());

    }


}


在Modeles/Person.java中:

package models;


import java.util.List;


import javax.persistence.Entity;

import javax.persistence.Id;


import play.db.ebean.Model;

import play.db.ebean.Model.Finder;


@Entity

public class Person extends Model {

@Id

public Integer id;

public String name;


// Query

public static Finder<Integer, Person> find = new Finder<Integer, Person>(Integer.class, Person.class);


public static List<Person> findAll() {

return find.all();

}


public static Person findByName(String name) {

return find.where().eq("name", name).findUnique();

}

}


build.sbt:添加

name := "anan"


version := "1.0-SNAPSHOT"


libraryDependencies ++= Seq(

  javaJdbc,

  javaEbean,

  cache,

   "mysql" % "mysql-connector-java" % "5.1.18"

)     


play.Project.playJavaSettings


conf:添加

# Database configuration

db.default.driver=com.mysql.jdbc.Driver

db.default.url="jdbc:mysql://127.0.0.1:3306/test"

db.default.user="root"

db.default.password="123456"


# Ebean configuration

ebean.default="models.*"


模板:

@(title: String, content: String, lines: List[String], personList: List[models.Person])

<!DOCTYPE html>

<html>

<header>

<title>@title</title>

</header>


<body>

<p>@{content}</p>

<ul>

    @for(line <- lines) {

      <li>@line</li>

    }

    </ul>

    <form action="/somePost" method="post">

    姓名:<input type="text" name="uname"><br />

    <input type="submit" value="提交">

    </form>

    <ul>

      @for(person <- personList) {

        <li>@person.name</li>

      }

    </ul>

</body>

</html>


routs:

# Routes

# This file defines all application routes (Higher priority routes first)

# ~~~~


# Home page

GET     /                           controllers.Application.index()

GET     /show/:id                   controllers.Application.show(id: Integer)

POST    /somePost                   controllers.Application.somePost()


# Map static resources from the /public folder to the /assets URL path

GET     /assets/*file               controllers.Assets.at(path="/public", file)