在 Spring MVC 中从 Controller 显示视图(所有员工的列表)
Posted
技术标签:
【中文标题】在 Spring MVC 中从 Controller 显示视图(所有员工的列表)【英文标题】:Displaying a view (list of all employees) from a Controller in Spring MVC 【发布时间】:2017-03-02 19:57:57 【问题描述】:试图在 Spring 中的控制器的视图报告中简单地输出所有员工的列表。收到与 Freemarker 相关的错误,不确定如何解决。
相关类:
Employee.java 模型
package mvc_course.models;
import java.util.HashSet;
import java.util.Set;
public class Employee
int employee_number;
String employee_name;
String address;
String ni_number;
String iban_number;
double starting_salary;
int employee_type_id;
int commission_rate;
int total_sales;
public static Set<Employee> employeeList = new HashSet<Employee>();
public Employee(String name, String address, String nin, String iban, double salary)
this.employee_name = name;
this.address = address;
this.ni_number = nin;
this.iban_number = iban;
this.starting_salary = salary;
public int getEmployee_number()
return employee_number;
public void setEmployee_number(int employee_number)
this.employee_number = employee_number;
public String getEmployee_name()
return employee_name;
public void setEmployee_name(String employee_name)
this.employee_name = employee_name;
public String getAddress()
return address;
public void setAddress(String address)
this.address = address;
public String getNi_number()
return ni_number;
public void setNi_number(String ni_number)
this.ni_number = ni_number;
public String getIban_number()
return iban_number;
public void setIban_number(String iban_number)
this.iban_number = iban_number;
public double getStarting_salary()
return starting_salary;
public void setStarting_salary(double starting_salary)
this.starting_salary = starting_salary;
public int getEmployee_type_id()
return employee_type_id;
public void setEmployee_type_id(int employee_type_id)
this.employee_type_id = employee_type_id;
public int getCommission_rate()
return commission_rate;
public void setCommission_rate(int commission_rate)
this.commission_rate = commission_rate;
public int getTotal_sales()
return total_sales;
public void setTotal_sales(int total_sales)
this.total_sales = total_sales;
ReportController.java
package mvc_course.controllers;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import mvc_course.models.Employee;
@Controller
public class ReportController
@Autowired
private DataSource dataSource;
@RequestMapping(value="showEmployees.mvc")
public String showEmployees(Model m)
List<Employee> employees = new ArrayList<Employee>();
try
Connection c = dataSource.getConnection();
Statement s = c.createStatement();
String sql = "SELECT * FROM Employees";
ResultSet rs = s.executeQuery(sql);
List<String[]>rows = new ArrayList<String[]>();
while(rs.next())
String[] row =
rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6);
rows.add(row);
for (String[] row : rows)
Employee e = new Employee(row[1], row[2], row[3], row[4], Double.parseDouble(row[5]));
employees.add(e);
System.out.println();
for (String string : row)
System.out.print(string + " ");
m.addAttribute("employees");
catch (Exception e)
System.out.println(e.getMessage());
return "EmployeesPerBuReport";
**EmployeeReport.ftl**
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Employees per BU</title>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css">
<!-- Compiled and minified javascript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js"></script>
</head>
<body>
<h1>Employees</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>National Insurance Number</th>
<th>Bank Account Number</th>
<th>Starting Salary</th>
</tr>
</thead>
<tbody>
<#list employees as employee>
<tr>
<td>$employee.getName</td>
<td>$employee.getAddress</td>
<td>$employee.getNi_number</td>
<td>$employee.getIban_number</td>
<td>$employee.getStarting_salary</td>
</tr>
</#list>
</tbody>
</table>
</body>
</body>
</html>
错误:
FreeMarker 模板错误(调试模式;在生产中使用 RETHROW!): 以下已评估为 null 或缺失:==> employee.getName [在第 31 行第 39 列的模板“EmployeesPerBuReport.ftl”中] ---- 提示:这是导致此错误的最后一个点之后的步骤,而不是 之前的那些。 ---- 提示:如果已知失败的表达式是 法律上指的是有时无效或缺失的东西,要么 指定一个默认值,如 myOptionalVar!myDefault,或使用 when-presentwhen-missing。 (这些仅涵盖 表达式的最后一步;要覆盖整个表达式,请使用 括号:(myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL 堆栈跟踪(“~”表示与嵌套相关): - 失败于:$employee.getName [in template "EmployeesPerBuReport.ftl" at line 31,第 37 栏
【问题讨论】:
我不使用freemarker,但是否可以在freemarker“模板”(或无论如何调用)而不是“employee.getName”中出现“employee.name”? 方法名中String前不应该有@ResponseBody吗? @JohnDonn,谢谢,但没有解决。 【参考方案1】:你在Employee
类中没有getName()
方法,你只有一个getEmployee_name()
(一个很奇怪的名字)。修复该问题后,您将收到一个不同的错误,如果您编写 $employee.name
而不是 $employee.getName
,则可以修复该错误。
【讨论】:
所以在将 getEmployee_name 更改为 getName 并将 $employee.getName 更改为 employee.name 之后,我实际上得到了同样的错误。 不可能,那时还有其他一些疏忽。您可以用 complete (但最小的)示例替换您问题中的源代码吗?你实际运行的那个。 但是,如果您认为我的编码不好,请告诉我! :)【参考方案2】:简单的错字:m.addAttribute("employees", employees);
【讨论】:
感谢您的评论,但是我尝试了这个,但问题没有解决。不过会保持这样。【参考方案3】:解决了。
更改 getter 和 setter 后,我通过更改解决了问题
<#list employees as employee>
<tr>
<td>$employee.getName</td>
<td>$employee.getAddress</td>
<td>$employee.getNi_number</td>
<td>$employee.getIban_number</td>
<td>$employee.getStarting_salary</td>
</tr>
</#list>
到
<#list employees as e>
<tr>
<td>$e.name</td>
<td>$e.address</td>
<td>$e.ni_number</td>
<td>$e.iban_number</td>
<td>$e.starting_salary</td>
</tr>
</#list>
不知道为什么,但这似乎解决了。 不过,问题主要在于名称错误的 getter 和字段名称。
【讨论】:
以上是关于在 Spring MVC 中从 Controller 显示视图(所有员工的列表)的主要内容,如果未能解决你的问题,请参考以下文章