Spring MVC:将表级联中的数据放到JSP中

时间:2015-07-18 17:00:32

标签: java spring jsp spring-mvc datatables

我有类似的表格: enter image description here

我需要创建包含Device的测量值的JSP文件。

我的想法是这样的网页(表格):

Device:
  SensorA:
     Temp (table row 1)
     Temp (table row 2)
     Temp (table row n)
  SensorB:
     Hum (table row 1)
     Hum (table row 2)
     Hum (table row n)

有我的代码:

@Entity
@Table(name="device")
public class Device implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(length=45)
    private String title;

    @OneToMany(mappedBy="device")
    private List<Value> values;

    public Device() {
    }
    // getters and setters....

}

@Entity
@Table(name="`values`")
public class Value implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private int id;

    @org.hibernate.annotations.Source(SourceType.DB)
    @org.hibernate.annotations.Generated(value = GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "date", insertable = true, updatable = false)
    private Date date;

    @OneToMany(mappedBy="value")
    private List<SensorA> sensorA;

    @OneToMany(mappedBy="value")
    private List<SensorB> sensorB;

    @ManyToOne
    @JoinColumn(name="device_id", nullable=false)
    private Device device;

    public Value() {
    }
    // getters and setters...
}

@Entity
@Table(name="sensor_a")
public class SensorA implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(name="TEMP", nullable=false)
    private float temp;

    @ManyToOne
    @JoinColumn(name="values_id", nullable=false)
    private Value value;

    public SensorA() {
    }
    // getters and setters...
}

@Entity
@Table(name="sensor_b")
public class SensorB implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(name="HUM", nullable=false)
    private float hum;

    @ManyToOne
    @JoinColumn(name="values_id", nullable=false)
    private Value value;

    public SensorB() {
    }
    // getters and setters...
}

@RequestMapping("/value")
@Controller
public class ValueController {

    @Autowired
    DeviceManager deviceManager;

    @RequestMapping(value="/{date}", method=RequestMethod.GET)
    public String getPage(Model model, @PathVariable("date") Date date) {

        Device d = deviceManager.findByDate(date);
        List<Value> values = d.getValues();     
        model.addAttribute("values", values);
        return "value";     
        }
}

和JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<h3>Values with datas</h3>
<div>
    <table>
        <tr>
            <th>value id</th>
            <th>sensor_a id</th>
            <th>temp</th>
        </tr>
        <c:forEach var="value" items="${values}">
            <tr>
                <td><c:out value="${value.id}" /></td>              
                <td><c:out value="${value.sensorA.id}" /></td>              
                <td><c:out value="${value.sensorA.temp}" /></td>                
            </tr>
        </c:forEach>
    </table>
</div>

如何将所有sensorA和sensorB传感器的所有值发送到JSP?我该如何访问它?我的JSP只写表头。

0 个答案:

没有答案