我正在spring-boot中制作一个项目,并且将thymeleaf用于html。我必须对Float类型的模型类“ currentTemp”中的字段进行验证。所以问题在于首先重新加载程序,然后运行天气更新,但是一旦我在网站上按添加按钮向数据库添加天气,就会遇到模板文件问题
Weather.java
@Entity
@Table(name="weather", uniqueConstraints = @UniqueConstraint(columnNames = {"city"}))
@Data
public class Weather {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "city")
private String city;
private String headline;
@Column(name = "descriptions")
private String description;
@Transient
private String wind;
@Transient
private String icon;
@Range(min=0, max=100, message = "length should be less than 100")
private float currentTemp;
private String minTemp;
private String maxTemp;
private String sunrise;
private String sunset;
@Transient
private String unit;
}
然后我有一个控制器类,如下所示:
@PostMapping("/checkWeather")
public String doActions(@ModelAttribute @Valid Weather weather,
BindingResult result, @RequestParam String action,
Map<String,Object> map, Errors errors, Model model)
throws IOException {
if(action.equals("add")){
if(errors.hasErrors()){
map.put("weatherList",crudService.getAllWeatherList());
model.addAttribute("weather",weather);
return "weather-history";
}
crudService.add(weather);
}
@GetMapping("/checkWeather")
public String checkWeather(@RequestParam(name="city", required=true, defaultValue="Oops! you typed wrong url!")
String city, Map<String,Object> map, Model model)
throws IOException,HttpClientErrorException.NotFound {
try{
Weather result = getWeatherService.getNewWeatherObject(city);
String timeConvertedSunrise = convertMillisecondsService.convertToTime(Long.parseLong(result.getSunrise()));
String timeConvertedSunset = convertMillisecondsService.convertToTime(Long.parseLong(result.getSunset()));
map.put("weatherList",crudService.getAllWeatherList());
model.addAttribute("weather",result);
model.addAttribute("weatherMap",map);
model.addAttribute("city",city.substring(0,1).toUpperCase()+city.substring(1));
model.addAttribute("headline", result.getHeadline());
model.addAttribute("description", result.getDescription());
model.addAttribute("icon", result.getIcon());
model.addAttribute("wind", result.getWind());
model.addAttribute("currentTemp", result.getCurrentTemp());
model.addAttribute("minTemp", result.getMinTemp());
model.addAttribute("maxTemp", result.getMaxTemp());
model.addAttribute("sunrise", timeConvertedSunrise);
model.addAttribute("sunset", timeConvertedSunset);
model.addAttribute("unit", result.getUnit());
}catch (HttpClientErrorException e){
LOGGER.info("Typed City cannot be found, please type name correctly! Aborting program..");
return "notfound";
}
return "weather-history";
}
在这里,我尝试在Web应用程序界面上单击“添加”按钮时,按用户将条目添加到表中。但是在添加条目之前,我想验证以下html文件中所示的字段:
weather-history.html
<!-- City search form and button -->
<div class="search-wrapper">
<form class="navbar-form" th:action="@{/checkWeather}" method="get">
<div class="row">
<div class="col-lg-8">
<input type="text" name="city" class="form-control" placeholder="Type the name of the city..">
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">Search Weather</button>
</div>
</div>
</form>
</div>
<!-- history table -->
<div class="jumbotron" id="history-table">
<div class="crud-form-field">
<form class="crud-form" th:action="@{/checkWeather}" th:object="${weather}" method="post">
<div class="form-group">
<input type="text" name="city" class="form-control" th:value="${city}" placeholder="Enter City" />
<input type="text" name="headline" class="form-control" th:value="${headline}" placeholder="Enter Headline" />
<input type="text" name="description" class="form-control" th:value="${description}" placeholder="Enter Description" />
<input type="text" name="currentTemp" class="form-control" th:value="${currentTemp}" placeholder="Enter Current Temp" th:field="*{currentTemp}" />
<p th:if="${#fields.hasErrors('currentTemp')}" th:errors="*{currentTemp}"></p>
<input type="text" name="minTemp" class="form-control" th:value="${minTemp}" placeholder="Enter Min Temp" />
<input type="text" name="maxTemp" class="form-control" th:value="${maxTemp}" placeholder="Enter Max Temp" />
<input type="text" name="sunrise" class="form-control" th:value="${sunrise}" placeholder="Enter Sunrise" />
<input type="text" name="sunset" class="form-control" th:value="${sunset}" placeholder="Enter Sunset" />
</div>
<div class="submit-crudform">
<button type="submit" name="action" value ="add" class="btn btn-outline-success"><i class="fas fa-plus-circle"></i> Add</button>
<button type="submit" name="action" value ="edit" class="btn btn-outline-success"><i class="far fa-edit"></i> Edit</button>
<button type="submit" name="action" value ="update" class="btn btn-outline-success"><i class="fas fa-sync"></i> Update</button>
<button type="submit" name="action" value ="delete" class="btn btn-outline-success"><i class="fas fa-trash-alt"></i> Delete</button>
</div>
</form>
</div>
但是它会引发与百里香模板有关的错误,
org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method get(java.lang.String) on null context object
at org.springframework.expression.spel.ast.MethodReference.throwIfNotNullSafe(MethodReference.java:153) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.ast.MethodReference.getValueRef(MethodReference.java:82) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
我认为与我要验证输入字段的行有关:
<input type="text" name="currentTemp" class="form-control" th:value="${currentTemp}" placeholder="Enter Current Temp" th:field="*{currentTemp}" />
<p th:if="${#fields.hasErrors('currentTemp')}" th:errors="*{currentTemp}"></p>
如果我删除这些额外的参数以检查错误,则问题得到解决。
我尝试阅读有关百里香的信息,但是我的语法似乎不正确。我关注了某些youtube视频,他们也像我一样在模板上应用了验证。我无法理解的是堆栈跟踪对我来说还不够清楚。它说all method get(java.lang.String) on null context object
,那么哪个字段显示空值?无法理解。
您能指导我正确的方向吗?感觉我在某个地方只想念一个很小的谜题。
编辑
我尝试从html模板中删除错误验证代码,但遇到相同的错误,因此似乎问题出在html文件中的其他对象上