用Eclipse 开发play

原文: http://blog.csdn.net/u013372441/article/details/47100843

前提是已经把play导入到eclipse里面去了,没有请看这:http://blog.csdn.NET/u013372441/article/details/47100129

然后在工程的目录下建立这么几个文件如下图所示


直接代码

Application.Java

[java]  view plain  copy
  1. <span style="font-size:18px;">package controllers;  
  2.   
  3. import play.*;  
  4. import play.mvc.*;  
  5.   
  6. import java.util.*;  
  7.   
  8. import models.*;  
  9.   
  10. public class Application extends Controller {  
  11.   
  12.     public static void index() {  
  13.         render();  
  14.     }  
  15.     public static void login(){  
  16.         render();  
  17.     }  
  18.      
  19.     public static void dologin(String user,String password){  
  20.         User user1 =User.find("user=? and password=?", user,password).first();  
  21.         if(user1 !=null)  
  22.         {     
  23.             System.out.println("success!");  
  24.             System.out.println(user1.user+user1.password);  
  25.             String hello =user1.user;  
  26.             System.out.println(hello);  
  27.             success(hello);            
  28.             //在这里打印find的返回值  
  29.             index();  
  30.             //跳转到原来的index.html网页  
  31.         }  
  32.         else{  
  33.             System.out.println("wrong!");  
  34.           
  35.             login();  
  36.             //继续停留在登陆网页  
  37.         }  
  38.           
  39.     }  
  40.     public static void register(){  
  41.     render();  
  42.     }  
  43.       
  44. // 实现注册  
  45.     public static void doregister(String user,String password){  
  46.         User user2= new User();  
  47.             user2.user=user;  
  48.             user2.password=password;  
  49.             user2.save();  
  50.         else{  
  51.             register();  
  52.         }  
  53.           
  54.     }  
  55.     public  static void success(String name){  
  56.         render(name);  
  57.     }  
  58.   
  59. }  
  60. </span>  
下面是user.java

[java]  view plain  copy
  1. <span style="font-size:18px;">package models;  
  2.   
  3. import javax.persistence.Entity;  
  4.   
  5. import play.db.jpa.Model;  
  6.   
  7.   
  8. @Entity  
  9. public class User extends Model{  
  10.     public String user;  
  11.     public String password;  
  12. }  
  13. </span>  
然后是几个html网页

index.html

[html]  view plain  copy
  1. <span style="font-size:18px;"><!DOCTYPE html >  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Insert title here</title>  
  6. <style>  
  7. a{  
  8. text-decoration:none;  
  9. }  
  10. </style>  
  11. </head>  
  12. <body>                                <!--  /application/dologin-->  
  13. <br />  
  14. <h1>hello 欢迎来到java play的世界</h1>  
  15. <a href="@{Application.login}" style="font-size:25px">login</a> <br/>  
  16. <a href="@{Application.register}"style="font-size:25px">register</a>  
  17. </body>  
  18. </html>  
  19. </span>  
login.html

[html]  view plain  copy
  1. <span style="font-size:18px;"><!DOCTYPE html >  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Insert title here</title>  
  6. <style>     
  7. body{  
  8.         background-color: rgb(11,41,105);  
  9.     }  
  10. </style>  
  11. </head>  
  12. <body>                                <!--  /application/dologin-->  
  13. <br />  
  14. <form method="post" action="@{application.dologin()}">  
  15. <span style="color:white">用户: <input type="text" name="user"></span><br />  
  16. <br />  
  17. <!-- user:  <input type="text" name="user"> <br> -->  
  18. <span style="color:white">密码: <input type="password" name="password"></span>  
  19. <!-- password:<input type="password" name="password"><br> -->  
  20. <br />  
  21. <span style="padding-left:80px">  
  22. <input type="submit" value="登录">   <input type="reset" value="重置">  
  23.   
  24. </span>  
  25. </form>  
  26. </body>  
  27. </html></span>  
register.html

[html]  view plain  copy
  1. <span style="font-size:18px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Insert title here</title>  
  6. </head>  
  7. <body>  
  8. <form method="post" action="@{application.doregister()}">  
  9. user:<input type="text" name="user">   
  10. <br>  
  11. password:<input type="password" name="password">  
  12. <br>  
  13. <input type="submit" value="注册 ">   <input type="reset" value="重置">  
  14. </body>  
  15. </html>  
  16.   
  17. </span>  
success.html

[html]  view plain  copy
  1. <span style="font-size:18px;"><!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>Insert title here</title>  
  6. <style>  
  7. #content{  
  8.     color: #FFFFFF;  
  9.     height:500px;  
  10.     width: 500px;  
  11.     clear: right;  
  12.     position:absolute;  
  13.     top: 300px;  
  14.     left: 500px;  
  15. }  
  16. </style>   
  17. </head>  
  18. <body>  
  19. <div id="content">  
  20. <span style="color:rgb(218,112,214);font-size:15px">登录名:${name}</span>  
  21. <br/>  
  22. <span style="color:rgb(189,183,107);font-size:15px">登陆成功正在跳转..............</span>  
  23. </div>  
  24.   
  25. </body>  
  26. </html></span>  
接下来是routes的配置


[python]  view plain  copy
  1. <span style="font-size:18px;"># Routes  
  2. # This file defines all application routes (Higher priority routes first)  
  3. # ~~~~  
  4.   
  5. # Home page  
  6. GET     /                                       Application.index  
  7. GET     /login                                  Application.login  
  8. GET     /register                               Application.register  
  9. GET     /success                                Application.success  
  10. # Ignore favicon requests  
  11. GET     /favicon.ico                            404  
  12.   
  13. # Map static resources from the /app/public folder to the /public path  
  14. GET     /public/                                staticDir:public  
  15.   
  16. # Catch all  
  17. *       /{controller}/{action}                  {controller}.{action}  
  18. </span>  
然后是数据库的配置

在 application.conf里面找到如下的界面


在这里我选择的是MySQL数据库然后我用的用户名是root,密码是123456,localhost是本机地址不用不用改,one是我自己的数据库名字,把它换成你自己的

然后在cmd下运行 play run first

在浏览器里输入127.0.0.1:9000就会看到网页了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值