值映射不是模型的成员

时间:2015-04-20 08:46:01

标签: java playframework ssms

您好我试图从类中获取对象并显示它并且我得到错误"值map不是models.TimeSheetDataStore"

的成员

这是我的模型类文件

TimeSheetDataStore.java

package models;
public class TimeSheetDataStore {
    String ID;
    String EmployeeID;
    String RoleID;
    String Task;
    String TimeSheetDate;
    String CreateDate;
    String UpdateDate;
    String TaskHour;
    String IsBilled;
    String ClientID;
    String ProjectID;
    String atHome;
    String atClientSite; 
}

TimeSheet.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.ArrayList;
import java.util.List;
import models.TimeSheetDataStore;

public class Timesheet {
    public static  List<TimeSheetDataStore> PopulateProject() {
        List<TimeSheetDataStore> TimeSheetList = new ArrayList<TimeSheetDataStore>();
        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 Project";
            ResultSet rs = sta.executeQuery(Sql);
            while (rs.next()) {
                TimeSheetDataStore TSDS= new TimeSheetDataStore();
                TSDS.ID=rs.getString("ID");
                TSDS.EmployeeID=rs.getString("EmployeeID");
                TSDS.RoleID=rs.getString("RoleID");
                TSDS.Task=rs.getString("Task");
                TSDS.TimeSheetDate=rs.getString("TimeSheetDate");
                TSDS.CreateDate=rs.getString("CreateDate");
                TSDS.UpdateDate=rs.getString("UpdateDate");
                TSDS.TaskHour=rs.getString("TaskHour");
                TSDS.IsBilled=rs.getString("IsBilled");
                TSDS.ProjectID=rs.getString("ProjectID");
                TSDS.ClientID=rs.getString("ClientID");
                TSDS.atHome=rs.getString("atHome");
                TSDS.atClientSite=rs.getString("atClientSite");
                TimeSheetList.add(TSDS);
               }

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

    }

}

控制器文件 Application.java

public static Result myAction(String ClientFilter,String ConsultantFilter) {
       DynamicForm requestData = Form.form().bindFromRequest();

        if (requestData.get("Submit")!=null) {
            return ok(index.render(Client.PopulateClient(),Consultant.PopulateConsultant(),Project.PopulateProject(ClientFilter)));
        } else if (requestData.get("Generate Timesheet")!=null) {
            //return ok(list.render(Client.PopulateClient(),Consultant.PopulateConsultant(),Project.PopulateProject()));
            return ok(DisplayTimeSheet.render(Timesheet.PopulateProject()));
        } else {
            return badRequest("This action is not allowed");
        }

     }

查看文件DisplayTimeSheet.scala.html

@(currentPage:List[TimeSheetDataStore])



<html>
    <head></head>
    <body>
     <form>
      <table>
        <thead>
                <tr>
                    <td>ID</td>
                    <td>EmployeeID</td>
                    <td>RoleID</td>
                    <td>Task</td>
                    <td>TimeSheetDate</td>
                    <td>CreateDate</td>
                    <td>UpdateDate</td>
                    <td>TaskHour</td>
                    <td>IsBilled</td>
                    <td>ProjectID</td>
                    <td>ClientID</td>
                    <td>atHome</td>
                    <td>atClientSite</td>
                </tr>
        </thead>
            <tbody>
              @for(i <- currentPage.indices) {
               @for(element <- currentPage.get(i)) {
                    <tr>
                        <td>@element.ID</td>
                        <td>@element.EmployeeID</td>
                        <td>@element.RoleID</td>
                        <td>@element.Task</td>
                        <td>@element.TimeSheetDate</td>
                        <td>@element.CreateDate</td>
                        <td>@element.UpdateDate</td>
                        <td>@element.TaskHour</td>
                        <td>@element.IsBilled</td>
                        <td>@element.ProjectID</td>
                        <td>@element.ClientID</td>
                        <td>@element.atHome</td>
                        <td>@element.atClientSite</td>
                    </tr>
                }
                }
             </tbody>
        </table>
        </form>
    </body>
</html>

有人可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

尝试代替

@for(i <- currentPage.indices) {
           @for(element <- currentPage.get(i)) {
                <tr>
                    <td>@element.ID</td>
                    <td>@element.EmployeeID</td>
                    <td>@element.RoleID</td>
                    .
                    .
                    .
                </tr>
            }
            }

只使用一个for循环,因为你有currentPage: List[TimeSheetDataStore]的列表所以当你@for(element <- currentPage)时,它将返回一个分配给元素变量的对象,所以现在你在元素中有TimeSheetDataStore个对象变量,以便您可以直接访问它的字段

这样做:

<tbody>
@for(element <- currentPage) {
                <tr>
                    <td>@element.ID</td>
                    <td>@element.EmployeeID</td>
                    <td>@element.RoleID</td>
                    <td>@element.Task</td>
                    <td>@element.TimeSheetDate</td>
                    <td>@element.CreateDate</td>
                    <td>@element.UpdateDate</td>
                    <td>@element.TaskHour</td>
                    <td>@element.IsBilled</td>
                    <td>@element.ProjectID</td>
                    <td>@element.ClientID</td>
                    <td>@element.atHome</td>
                    <td>@element.atClientSite</td>
                </tr>
            }
    </tbody>