【发布时间】:2020-01-10 23:31:27
【问题描述】:
我想要做的:从 HTML 输入 时间 & 日期,将这些值与 DB 中的列进行比较,然后显示与 时间 相关的数据& 日期。 如果我只使用 time,它会起作用。但是当我添加关于 date 的代码并尝试将日期格式从 yyyy-MM-dd 替换为 yyyy/MM/dd 时,它会出现错误 NullPointerException。为什么 time 有值,但 date1 没有值并变为 Null。
这是我的代码,谢谢你的帮助。
App Controller.java
package com.example.demo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.ProductService;
@Controller
public class AppController {
@Autowired
private ProductService service;
@RequestMapping("/ChartLine")
public String PostForm(@ModelAttribute(value="greeting") Greeting greeting,Model model) {
model.addAttribute("greeting", greeting);
String time = greeting.getTime();
String date = greeting.getDate();
String date1 = date.replace('-', '/');
List<Object[]> listData = service.listData(time,date1);
model.addAttribute("listData", listData);
List<Object[]> listTime = service.listTime(time,date1);
model.addAttribute("listTime", listTime);
return "ChartLine";
}
}
ProductRepository.java
package com.example.demo;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.example.demo.Product;
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
@Query(value="SELECT tag00 FROM Product WHERE time LIKE ?1% AND date = ?2",nativeQuery =true)
public List<Object[]> findByTag00(String time, String date1);
@Query(value="SELECT time FROM Product WHERE time LIKE ?1% AND date = ?2",nativeQuery =true)
public List<Object[]> findByTime(String time, String date1);
}
ProductService.java
package com.example.demo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.Product;
import com.example.demo.ProductRepository;
@Service
@Transactional
public class ProductService {
@Autowired
private ProductRepository repo;
public List<Product> listAll() {
return repo.findAll();
}
public List<Object[]> listData(String time, String date1) {
return repo.findByTag00(time, date1);
}
public List<Object[]> listTime(String time, String date1) {
return repo.findByTime(time, date1);
}
public void save(Product product) {
repo.save(product);
}
public Product get(long id) {
return repo.findById(id).get();
}
public void delete(long id) {
repo.deleteById(id);
}
}
Greeting.java
package com.example.demo;
public class Greeting {
private String time;
private String date;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time=time;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date=date;
}
}
Product.java
package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product {
public int id;
public float tag00;
public String date;
public String time;
protected Product() {
}
protected Product(int id, float tag00, String date, String time) {
super();
this.id = id;
this.tag00 = tag00;
this.date = date;
this.time = time;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public float getTAG00() {
return tag00;
}
public void setTAG00(float tag00) {
this.tag00 = tag00;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
ChartLine.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<div style="position:relative;left:50px;top:5px;" > <!-- Position: relative(tuong quan theo left,right,bottom,top), absolute,fixed -->
<a href="/home">Home</a>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.5.0"></script> <!-- thu vien dung de hien thi gia tri tren bieu do -->
<div class="container">
<canvas id="ChartLine"></canvas>
</div>
<div class="container">
<canvas id="myChart" width ="350" height="350"></canvas>
</div>
<body>
<div id ="container">
<form action="#" th:action="@{/ChartLine}" th:object="${greeting}" method="post">
Enter a date :<br>
<select name ="time" th:field="*{time}">
<option value="00">0h</option>
<option value="01">1h</option>
<option value="02">2h</option>
<option value="03">3h</option>
<option value="04">4h</option>
<option value="05">5h</option>
<option value="06">6h</option>
<option value="07">7h</option>
<option value="08">8h</option>
<option value="09">9h</option>
<option value="10">10h</option>
<option value="11">11h</option>
<option value="12">12h</option>
<option value="13">13h</option>
<option value="14">14h</option>
<option value="15">15h</option>
<option value="16">16h</option>
<option value="17">17h</option>
<option value="18">18h</option>
<option value="19">19h</option>
<option value="20">20h</option>
<option value="21">21h</option>
<option value="22">22h</option>
<option value="23">23h</option>
</select>
<input type="date" th:field="*{date}" ><br>
<input type="submit">
</form>
<div th:object = "${greeting}" >
<span th:text = "*{time}" ></span>
<span th:text = "*{date}" ></span>
</div>
<div id ="content">
<script th:inline="javascript"> //dung de chay js trong thymeleaf html
let myChart = document.getElementById('myChart').getContext('2d');
// Global Options
Chart.defaults.global.defaultFontFamily = 'Lato';
Chart.defaults.global.defaultFontSize = 18;
Chart.defaults.g
let massPopChart = new Chart(myChart, {
type:'line', // bar, horizontalBar, pie, line, doughnut, radar, polarArea
data:{
labels:/*[[${listTime}]]*/,
datasets:[{
label:'Temperature',
data:/*[[${listData}]]*/,
backgroundColor:'rgba(255, 99, 132, 0.6)',
fill: false,
borderWidth:1,
borderColor:'rgba(255, 0, 0, 0.6)', //thay doi mau cho Line
hoverBorderWidth:1,
hoverBorderColor:'#111',
pointRadius: 5
}]
},
options: {
legend : {
display: false,
},
responsive : true ,
maintainAspectRatio: false,
plugins: { //plugin dung de hien thi gia tri len bieu do
datalabels: {
display: function(context) {
return context.dataIndex % 1;
},
backgroundColor: function(context) {
return context.dataset.backgroundColor;
},
backgroundColor: 'rgba(255, 255, 255,0)',
borderRadius: 2,
anchor : 'start',
align : 'top',
color: 'black',
font: {
weight: 'bold'
},
formatter: Math.round
}
},
scales: {//scales dung de cai dat option cho cot X,Y
yAxes: [{
ticks: {
fontColor : 'blue'
},
}],
xAxes: [{
ticks: {
fontColor: 'blue'
},
}]
}
}
});
</script>
</div>
</div>
</body>
</html>
Home.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>First Demo</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
</head>
<body>
<input type="button" onclick="location.href='/ChartLine'" value="Line Chart" style="position:relative;width:100px">
<br>
<br>
<input type="button" onclick="location.href='/ChartBar'" value="Bar Chart" style="position:relative;width:100px">
<br>
<br>
<input type="button" onclick="location.href='/ChartPie'" value="Pie Chart" style="position:relative;width:100px">
</body>
</html>
【问题讨论】:
-
你也可以添加产品类
-
@ShameeraAnuranga 嗨,我添加了。 Product.java 中的 TAG00 是我的数据。
-
也是发送数据的前端代码
-
@ShameeraAnuranga 我添加了我的 html。我使用 Chartjs 创建图表来显示数据。
-
您是否确认以下代码正在向后端发送任何数据? (通过放置控制台日志或其他方式)
(或在 App Controller 中调试代码并检查日期字段是否包含任何值)跨度>
标签: java sql-server spring-boot spring-data-jpa thymeleaf