【发布时间】: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,谢谢,但没有解决。
标签: java spring spring-mvc model-view-controller freemarker