我有一个问题,我不知道如何管理......
在我的控制器中我有一个方法
@RequestMapping(value = {path+"/illuminazione"}, method = RequestMethod.GET)
public String lightsSituation(ModelMap model){
String pageTitle ="Stato Luci";
List <Illuminazione> illuminazione = illuminazioneService.showLightsSituation();
model.addAttribute("illuminazione", illuminazione);
model.addAttribute("pageTitle", pageTitle);
model.addAttribute("cssActiveIll", cssActiveIll);
return path+"/illuminazione";
}
此方法只是生成从DB中获取的对象列表。通过这种方式,我可以轻松地将所有模型传递给我的视图以显示对象。问题是在字段中我有两个时间戳值,我想在视图中显示它们之间的持续时间。 我对duration函数没有任何问题,我设法直接在JSP页面中进行计算,但我更喜欢在控制器中执行此操作,只需将计算结果传递给模型(这是一种正确的方法吗? )。如何从List中获取我需要的两个值,以便将它们添加到模型中?
我想过在服务实现类中创建一个新函数,但它看起来像是一个超级解决方案......
Illuminazione.java
@Entity
@Table(name = "illuminazione", catalog = "SMARTPARK")
public class Illuminazione implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int idEvento;
private Luce luce;
private Date dateTime;
private Date lastDateTime;
private boolean isLit;
public Illuminazione() {
}
public Illuminazione(int idEvento, Luce luce, Date dateTime, Date lastDateTime, boolean isLit) {
this.idEvento = idEvento;
this.luce = luce;
this.dateTime = dateTime;
this.lastDateTime = lastDateTime;
this.isLit = isLit;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_evento", unique = true, nullable = false)
public Integer getIdEvento() {
return this.idEvento;
}
public void setIdEvento(Integer idEvento) {
this.idEvento = idEvento;
}
@ManyToOne
@JoinColumn(name = "id_luce")
public Luce getLuce() {
return this.luce;
}
public void setLuce(Luce luce) {
this.luce = luce;
}
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern="dd/MM/yyyy - hh:mm")
@Column(name = "date_time")
public Date getDateTime() {
return this.dateTime;
}
public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
}
@NotNull
@Column(name = "isLit", nullable = false)
public boolean isIsLit() {
return this.isLit;
}
public void setIsLit(boolean isLit) {
this.isLit = isLit;
}
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern="dd/MM/yyyy - hh:mm")
@Column(name = "last_date_time")
public Date getLastDateTime() {
return lastDateTime;
}
public void setLastDateTime(Date lastDateTime) {
this.lastDateTime = lastDateTime;
}
}
有什么想法吗?
根据@reos的建议,我可以在我的Illuminazione.java中添加一个duration
字段,现在已经
public String getDuration(){
DateTime sd = new DateTime(getDateTime());
DateTime ed = new DateTime(getLastDateTime());
Period period = new Period(sd, ed);
long days = period.getDays();
long hours = period.getHours();
long minutes = period.getMinutes();
long seconds = period.getSeconds();
this.duration=days+" giorni - "+hours+" ore - "+minutes+" minuti - "+seconds+" secondi";
return duration;
}
问题是我的illuminazioneService.showLightsSituation();
来自DaoImpl,这是一个普通的SQL查询(我无法使用Criteria或HQL进行此查询以处理对象)
public List<Illuminazione> showLightsSituations() {
String query = "select i.id_evento, i.id_luce, i.last_date_time, l.numero_luce, l.nome_luce, ill.MaxDate, i.isLit ";
query += "from illuminazione i inner join luci l on i.id_luce= l.id_luce ";
query += "inner join (SELECT `id_luce` as numeroLuce2, max(date_time) as MaxDate from illuminazione i2 group by `i2`.`id_luce`) ill ";
query += "on i.id_luce = ill.`numeroLuce2` and i.`date_time` = ill.MaxDate order by i.`id_luce` asc";
SQLQuery q = getSession().createSQLQuery(query);
q.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List<Illuminazione> lista = q.list();
return lista;
}
通过这种方式,当我将模型发送到JSP视图时,我只有我用查询选择的字段,我无法以这种方式调用持续时间${ill.duration}
,因为它是空的
这是JPS的摘录
<c:forEach items="${illuminazione}" var="ill">
<tr>
<td><div class="list-field">
<fmt:formatDate value="${ill.MaxDate}" pattern="dd/MM/yyyy - HH:mm" />
</div></td>
<td><div class="list-field">
<fmt:formatDate value="${ill.last_date_time}" pattern="dd/MM/yyyy - HH:mm" />
</div></td>
<td><div class="list-field">${ill.duration}</div></td>
</tr>
</c:forEach>
答案 0 :(得分:1)
这可能是一种更有效的方法,但我只是向您展示这个概念。这应该可以让你快速完成。
控制器:
@RequestMapping(value = {path+"/illuminazione"}, method = RequestMethod.GET)
public String lightsSituation(Model model){
String pageTitle ="Stato Luci";
List <Illuminazione> illuminazione = illuminazioneService.showLightsSituation();
//to be very clear with the example, we create a new list that will have
//the updated duration.
List <Illuminazione> definitivo = new ArrayList<>(illuminazione.size());
for (Illuminazione fagioloJava : illuminazione) {
DateTime start = fagioloJava.getDateTime();
DateTime end = fagioloJava.getLastDateTime();
//let your controller just route requests
//and provide the model attributes;
//put the duration calculation in a service class
String duration = IlluminazioneService.getDuration(start, end);
fagioloJava.setDuration(duration);
definitivo.add(fagioloJava);
}
model.addAttribute("definitivo", definitivo);
model.addAttribute("pageTitle", pageTitle);
model.addAttribute("cssActiveIll", cssActiveIll);
return path+"/illuminazione";
}
服务:
public class IlluminazioneService {
public static String getDuration(DateTime start, DateTime end) {
Period period = new Period(start, end);
long days = period.getDays();
long hours = period.getHours();
long minutes = period.getMinutes();
long seconds = period.getSeconds();
//if this needs to be internationalized,
//then I wouldn't return it this way.
//You could have a field for days, hours, minutes, etc.
//and get the rest from a Spring message (resource) bundle.
return days+" giorni - "+hours+" ore - "+minutes+" minuti - "+seconds+" secondi";
}
}
ILLUMINAZIONE:
//add this.
//transient because we don't need to persist this:
@Transient
private String duration;
//getter and setter
很明显,如果你有数万亿的Illuminazione
bean,那么迭代它们需要时间。但在大多数情况下,这个解决方案将完全没问题。我们一直看到这个方法在项目中使用。