前提是已经把play导入到eclipse里面去了,没有请看这:http://blog.csdn.net/u013372441/article/details/47100129
然后在工程的目录下建立这么几个文件如下图所示
直接代码
Application.java
<span style="font-size:18px;">package controllers; import play.*; import play.mvc.*; import java.util.*; import models.*; public class Application extends Controller { public static void index() { render(); } public static void login(){ render(); } public static void dologin(String user,String password){ User user1 =User.find("user=? and password=?", user,password).first(); if(user1 !=null) { System.out.println("success!"); System.out.println(user1.user+user1.password); String hello =user1.user; System.out.println(hello); success(hello); //在这里打印find的返回值 index(); //跳转到原来的index.html网页 } else{ System.out.println("wrong!"); login(); //继续停留在登陆网页 } } public static void register(){ render(); } // 实现注册 public static void doregister(String user,String password){ User user2= new User(); user2.user=user; user2.password=password; user2.save(); else{ register(); } } public static void success(String name){ render(name); } } </span>下面是user.java
<span style="font-size:18px;">package models; import javax.persistence.Entity; import play.db.jpa.Model; @Entity public class User extends Model{ public String user; public String password; } </span>然后是几个html网页
index.html
<span style="font-size:18px;"><!DOCTYPE html > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style> a{ text-decoration:none; } </style> </head> <body> <!-- /application/dologin--> <br /> <h1>hello 欢迎来到java play的世界</h1> <a href="@{Application.login}" style="font-size:25px">login</a> <br/> <a href="@{Application.register}"style="font-size:25px">register</a> </body> </html> </span>login.html
<span style="font-size:18px;"><!DOCTYPE html > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style> body{ background-color: rgb(11,41,105); } </style> </head> <body> <!-- /application/dologin--> <br /> <form method="post" action="@{application.dologin()}"> <span style="color:white">用户: <input type="text" name="user"></span><br /> <br /> <!-- user: <input type="text" name="user"> <br> --> <span style="color:white">密码: <input type="password" name="password"></span> <!-- password:<input type="password" name="password"><br> --> <br /> <span style="padding-left:80px"> <input type="submit" value="登录"> <input type="reset" value="重置"> </span> </form> </body> </html></span>register.html
<span style="font-size:18px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form method="post" action="@{application.doregister()}"> user:<input type="text" name="user"> <br> password:<input type="password" name="password"> <br> <input type="submit" value="注册 "> <input type="reset" value="重置"> </body> </html> </span>success.html
<span style="font-size:18px;"><!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> #content{ color: #FFFFFF; height:500px; width: 500px; clear: right; position:absolute; top: 300px; left: 500px; } </style> </head> <body> <div id="content"> <span style="color:rgb(218,112,214);font-size:15px">登录名:${name}</span> <br/> <span style="color:rgb(189,183,107);font-size:15px">登陆成功正在跳转..............</span> </div> </body> </html></span>接下来是routes的配置
<span style="font-size:18px;"># Routes # This file defines all application routes (Higher priority routes first) # ~~~~ # Home page GET / Application.index GET /login Application.login GET /register Application.register GET /success Application.success # Ignore favicon requests GET /favicon.ico 404 # Map static resources from the /app/public folder to the /public path GET /public/ staticDir:public # Catch all * /{controller}/{action} {controller}.{action} </span>然后是数据库的配置
在 application.conf里面找到如下的界面
在这里我选择的是mysql数据库然后我用的用户名是root,密码是123456,localhost是本机地址不用不用改,one是我自己的数据库名字,把它换成你自己的
然后在cmd下运行 play run first
在浏览器里输入127.0.0.1:9000就会看到网页了
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013372441/article/details/47100843