Ошибка Error executing FreeMarker template

298
08 ноября 2021, 06:30

В проекте существует две сущности Country

@Entity
public class Country {
private int idcountry;
private String countryName;
@Id
@Column(name = "idcountry", nullable = false)
public int getIdcountry() {
    return idcountry;
}
public void setIdcountry(int idcountry) {
    this.idcountry = idcountry;
}
@Basic
@Column(name = "country_name", nullable = false, length = 45)
public String getCountryName() {
    return countryName;
}
public void setCountryName(String countryName) {
    this.countryName = countryName;
}
@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Country country = (Country) o;
    return idcountry == country.idcountry &&
            Objects.equals(countryName, country.countryName);
}
@Override
public int hashCode() {
    return Objects.hash(idcountry, countryName);
}
}

и City

@Entity
public class City {
private int idcity;
private String city;
private int countryIdcountry;
private Collection<CarForSale> carForSalesByIdcity;
private Country countryByCountryIdcountry;
@Id
@Column(name = "idcity", nullable = false)
public int getIdcity() {
    return idcity;
}
public void setIdcity(int idcity) {
    this.idcity = idcity;
}
@Basic
@Column(name = "city", nullable = false, length = 45)
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
@Basic
@Column(name = "country_idcountry",insertable = false,updatable = false, 
nullable = false)
public int getCountryIdcountry() {
    return countryIdcountry;
}
public void setCountryIdcountry(int countryIdcountry) {
    this.countryIdcountry = countryIdcountry;
}
@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    City city1 = (City) o;
    return idcity == city1.idcity &&
            countryIdcountry == city1.countryIdcountry &&
            Objects.equals(city, city1.city);
}
@Override
public int hashCode() {
    return Objects.hash(idcity, city, countryIdcountry);
}
@OneToMany(mappedBy = "cityByCityIdcity")
public Collection<CarForSale> getCarForSalesByIdcity() {
    return carForSalesByIdcity;
}
public void setCarForSalesByIdcity(Collection<CarForSale> 
carForSalesByIdcity) {
    this.carForSalesByIdcity = carForSalesByIdcity;
}
@ManyToOne
@JoinColumn(name = "country_idcountry", referencedColumnName = "idcountry", 
nullable = false)
public Country getCountryByCountryIdcountry() {
    return countryByCountryIdcountry;
}
public void setCountryByCountryIdcountry(Country countryByCountryIdcountry) 
{
    this.countryByCountryIdcountry = countryByCountryIdcountry;
}
}

связанные между собой One-to-Many и Many-to-One.

Со следующими контроллерами

@Controller
public class CityController {
@Autowired
private JPACityDAO cityDAO;
@RequestMapping(value = "/city", method = RequestMethod.GET)
public String getCity(Model model) {
    model.addAttribute("city", cityDAO.getAll());
    return "/city";
}
@GetMapping("/city/add_city")
public String getAddCity(ModelMap model){
    model.addAttribute("city",new City());
    model.addAttribute("countryByCountryIdcountry", new Country());
    return "/add_city";
}
@PostMapping("/city/add_city")
public String addCity(@ModelAttribute City city, BindingResult result) {
    if(result.hasErrors()){
        return "/add_city";
    }
    cityDAO.add(city);
    return "redirect:/city";
}
}

@Controller
public class CountryController {
@Autowired
private JPACountryDAO countryDAO;
@RequestMapping(value = "/country", method = RequestMethod.GET)
public String getCountry(Model model) {
    model.addAttribute("country", countryDAO.getAll());
    return "/country";
}
@GetMapping("/country/add_country")
public String getAddCountry(Model model){
    model.addAttribute("country",new Country());
    return "/add_country";
}
@PostMapping("/country/add_country")
public String addCountry(@ModelAttribute Country country, BindingResult 
result) {
    if(result.hasErrors()){
        return "/add_country";
    }
    countryDAO.add(country);
    return "redirect:/country";
}
}

В форме пытаюсь реализовать добавление нового города который будет относится к стране(из выпадающего списка).

<#assign sf = JspTaglibs["http://www.springframework.org/tags/form"]>
<html>
<head>
<title>Add City</title>
</head>
<body>
<@sf.form action="/city/add_city" method = "post" modelAttribute = "city">
<div>
    <@sf.label path = "countryByCountryIdcountry">Країна</@sf.label>
    <@sf.select path="countryByCountryIdcountry" itemValue="NONE"
    itemLabel="---Select--- 
">${city.countryByCountryIdcountry.getCountryName()}</@sf.select>
    <@sf.errors path = "countryByCountryIdcountry"/>
</div>
<div>
    <@sf.label path = "idcity">ID</@sf.label>
    <@sf.input path="idcity"/>
    <@sf.errors path = "idcity"/>
</div>
<div>
    <@sf.label path = "city">С</@sf.label>
    <@sf.input path="city"/>
    <@sf.errors path = "city"/>
</div>
<input type="submit">
</@sf.form>
</body>
</html>

При входе на форму в dropbox ничего не прогружается, и дальше форма тоже не отображается.

Выдает следующею ошибку.

SEVERE [http-nio-8080-exec-9] 
freemarker.log._JULLoggerFactory$JULLogger.error Error executing FreeMarker 
template
FreeMarker template error:
The following has evaluated to null or missing:
==> city.countryByCountryIdcountry  [in template "add_city.ftl" at line 21, 
column 36]
----
Tip: It's the step after the last dot that caused this error, not those 
before it.
----
Tip: If the failing expression is known to be legally refer to something 
that's sometimes null or missing, either specify a default value like 
myOptionalVar!myDefault, or use <#if myOptionalVar??>when- 
present<#else>when-missing</#if>. (These only cover the last step of the 
expression; to cover the whole expression, use parenthesis: 
(myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${city.countryByCountryIdcountry.getC...  [in template 
"add_city.ftl" at line 21, column 34]
----
Java stack trace (for programmers):
----
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
    at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131)
    at freemarker.core.UnexpectedTypeException.newDesciptionBuilder(UnexpectedTypeException.java:77)
    at freemarker.core.UnexpectedTypeException.<init>(UnexpectedTypeException.java:40)
    at freemarker.core.NonHashException.<init>(NonHashException.java:46)
    at freemarker.core.Dot._eval(Dot.java:45)
    at freemarker.core.Expression.eval(Expression.java:78)
    at freemarker.core.MethodCall._eval(MethodCall.java:55)
    at freemarker.core.Expression.eval(Expression.java:78)
    at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
    at freemarker.core.DollarVariable.accept(DollarVariable.java:41)
    at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
    at freemarker.core.Environment.visitAndTransform(Environment.java:425)
    at freemarker.core.UnifiedCall.accept(UnifiedCall.java:107)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.MixedContent.accept(MixedContent.java:54)
    at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
    at freemarker.core.Environment.visitAndTransform(Environment.java:425)
    at freemarker.core.UnifiedCall.accept(UnifiedCall.java:107)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.MixedContent.accept(MixedContent.java:54)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.Environment.process(Environment.java:302)
    at freemarker.template.Template.process(Template.java:325)
    at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:391)
    at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:304)
    at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:255)
    at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:179)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:317)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
READ ALSO
Парсинг страниц с Frame Android Studio

Парсинг страниц с Frame Android Studio

Мне необходимо парсить страницы сайта с фреймамиРазметка содержит в себе несколько frame, в них нужные мне данные

216
RuntimeException. Как вывести в консоль сообщение об ошибке?

RuntimeException. Как вывести в консоль сообщение об ошибке?

Всем привет! Есть такой кусок кода -

167
Проект не подключается к PostgreSQL

Проект не подключается к PostgreSQL

Я написал datasourse файл и поместил его в deployment-каталог сервера WildFly туда же поместил JDBC драйвер

257
Четность соседей

Четность соседей

Решаю похожие задачи для отбора на стажировку в Яндекс, вроде решил задачу, но выдает WA (Wrong Answer)

105