嗨,我试图从一个类中获取一个对象并显示它,但出现错误“值映射不是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);
}
}
控制器文件
应用程序
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>
有人可以告诉我我在做什么错吗?
最佳答案
尝试代替
@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>