在播放框架工作中使用数据库中的值填充下拉列表

Populating Drop down with the values from database in play frame work

我是框架工作的新手,我觉得有点困难。 我正在从数据库中检索客户名称列表并将其填充到下拉列表中,这是我的 client.java 代码

   package models;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import play.db.ebean.Model;
public class Client extends Model {

    /**
     * 
     */
    private static final long serialVersionUID = -1932214701504374792L;
    public static String ClientName;
    public static ArrayList<String> Clientdetail= new ArrayList<>();
    public static ArrayList<String> PopulateClient() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433","SUMEET","sumeet");
            Statement sta = conn.createStatement();
            String Sql = "select * from client";
            ResultSet rs = sta.executeQuery(Sql);
            while (rs.next()) {
                ClientName = rs.getString("ClientName");
                Clientdetail.add(ClientName);
               }

        } catch (InstantiationException | IllegalAccessException
                | ClassNotFoundException |SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return(Clientdetail);

    }

}

这是我的 application.java 代码

package controllers;

import models.Client;

import play.mvc.*;
import views.html.*;

public class Application extends Controller {


    public static Result index(){

        return ok(index.render(Client.PopulateClient()));
    }

}

这是我的 index.scala.html

    @(ClientDetails: java.util.ArrayList[String])

@main("ADMS") {

   <center>
    <form id="select">
   <a>CONSULTANT</a>
       <select name=Consultant>
           <option value="lokesh">Lokesh</option>
           <option>@ClientDetails</option>
           <option>Vidyasekar</option>
           <option>Abhishek</option>
           <option>Naveen</option>
           <option>Nanda</option>
       </select>
     <table border="1">
       <tr>
       <td width=50px>Client</td>
       <td width=50px>Project</td>
       <td width=50px>Task</td>
       <td width=50px>Date</td>
       <td width=50px>Consultant</td>
       <td width=50px>Role</td>
       <td width=80px>Is Billable</td>
       </tr>
       <tr>
       <td>@ClientDetails</td>
       </tr>
       </table>
      </form>
   </center>
}

main.scala.html

@(title: String)(Content: Html)


<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
   </head>
    <body>
    @Content
    </body>
</html>

有人可以帮我解决这个问题吗?我需要用数组值填充下拉列表,填充的数据只是括号 --> "[]"

是的,不幸的是,Play 的模板系统是基于 Scala 的,因此您必须映射您的@ClientDetails:

@ClientDetails.map { clientName =>
  <option>@clientName</option>
}

如果你 return 更复杂的对象,它可能看起来更像这样:

@ClientDetails.map { client =>
  <option value="@client.id">@client.name</option>
}

希望对您有所帮助!

Play 框架提供了模板助手库,它提供了构建 select 带有选项和 selected 值的下拉列表的功能。 一旦理解正确,使用起来非常简单。

视图中的

@helper.select() 方法采用与 select 类型的输入字段相关的各种参数。第一个参数是表单字段,因为这里我们没有任何表单,所以我们可以创建一个临时表单并在其中创建一个名称为 Consultant 的新字段[因为这将是 select 字段的名称属性的值]。 第二个参数将是选项映射,其中键和值分别对应选项标签的值和选项标签中包含的文本。

控制器代码

package controllers;

import models.Client;

import play.mvc.*;
import views.html.*;

public class Application extends Controller {


    public static Result index(){

        return ok(index.render(Client.getClientDetails()));
    }

}

模型代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import play.db.ebean.Model;
public class Client extends Model {

    /**
     * 
     */
    private static final long serialVersionUID = -1932214701504374792L;
    public static String ClientName;
    public static HashMap<String, String> Clientdetail= new HashMap<String, String>();
    public static HashMap<String, String> getClientDetails() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433","SUMEET","sumeet");
            Statement sta = conn.createStatement();
            String Sql = "select * from client";
            ResultSet rs = sta.executeQuery(Sql);
            while (rs.next()) {
                ClientName = rs.getString("ClientName");
                Clientdetail.put(ClientName,ClientName);
               }

        } catch (InstantiationException | IllegalAccessException
                | ClassNotFoundException |SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return(Clientdetail);

    }

}

查看代码:index.scala.html

@(ClientDetails: java.util.HashMap[String, String])

@import helper._

@main("ADMS") {

   <center>
    <form id="select">
   <a>CONSULTANT</a>
   @select(Form.form()("Consultant"),           
       options(ClientDetails),
       'value -> "clientName1"[any value that should be selected by default])
     <table border="1">
       <tr>
       <td width=50px>Client</td>
       <td width=50px>Project</td>
       <td width=50px>Task</td>
       <td width=50px>Date</td>
       <td width=50px>Consultant</td>
       <td width=50px>Role</td>
       <td width=80px>Is Billable</td>
       </tr>
       <tr>
       <td>@ClientDetails</td>
       </tr>
       </table>
      </form>
   </center>
}