【问题标题】:Unable to Run the following Spring Boot Application that uses JDBC Template无法运行以下使用 JDBC 模板的 Spring Boot 应用程序
【发布时间】:2018-12-30 05:08:21
【问题描述】:

我创建了一个简单的 Spring Boot 应用程序,它将 Dog 信息添加到 MySql 数据库中。

此应用程序的控制器类是 DogController.java

package com.dog.resue.controller;

import com.dog.resue.dao.DodRepository;
import com.dog.resue.service.DogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping(path="/")
public class DogController {

    @Autowired
    private DodRepository dodRepository;

    @Autowired
    private DogService dogService;


   @RequestMapping(value ="/home",method = {RequestMethod.POST,RequestMethod.GET})
    public String adddog(@RequestParam("name") String name,
                          @RequestParam("rescued") @DateTimeFormat(pattern = "yyyy-MM-dd") Date rescued,
                          @RequestParam("vaccinated") Boolean vaccinated, Model model)
    {
        dogService.addADog(name, rescued, vaccinated);
        System.out.println("name = " + name + ",rescued = " + rescued + ", vaccinated = " + vaccinated);
        return "index";
    }


    }

对应的Service类是

package com.dog.resue.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Date;

@Service
public class DogServiceImpl implements DogService {

    @Autowired
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate=new JdbcTemplate(dataSource);
    }

    @Override
    public void addADog(String name, Date rescued, Boolean vaccinated) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("INSERT INTO dog(name,rescued,vaccinated) VALUES(?,?,?)",name,rescued,vaccinated );
    }
}

而 thymeleaf HTML 文件是

<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <!-- META SECTION -->
    <title>Dog Rescue</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- END META SECTION -->
    <!--  BEGIN STYLE -->
    <style>
        table, th, td {
            border: 1px solid black;
            padding: 1px;
        }
    </style>
    <!--  END STYLE -->

</head>
<body>
<h2>Current Dogs In Rescue</h2>
<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Rescue Date</th>
        <th>Vaccinated</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="dogs : ${dogs}">
        <td th:text="${dogs.id}">Text ...</td>
        <td th:text="${dogs.name}">Text ...</td>
        <td th:text="${dogs.rescued}">Text ...</td>
        <td th:text="${dogs.vaccinated}">Text...</td>
    </tr>
    </tbody>
</table>
</div>

<h2>Add A Dog</h2>
<form action="#" th:action="@{/home}" >
    <label>Name<input type="text" name="name" id="name"></input></label>
    <label>Vaccinated<input type="text" name="vaccinated" id="vaccinated"></input></label>
    <label>Rescued<input type="text" name="rescued" id="rescued"></input></label>
    <input type="submit" value="Submit"></input>
</form>
</body>
</html>

运行此代码时出现以下错误

白标错误页面 此应用程序没有显式映射 /error,因此您将其视为后备。

2018 年 7 月 22 日星期日 21:50:32 IST 出现意外错误(类型=错误请求,状态=400)。 所需的字符串参数“名称”不存在

网址是http://localhost:8080/home

请帮我解决这个问题

【问题讨论】:

  • 不是您当前的问题,但是 jdbcTemplate 不会被初始化,因为您自动连接数据源 field,因此 Spring 不会使用初始化模板的 setter。
  • 嗨,我看到您正在尝试运行我为此博客创建的示例应用程序:springframework.guru/…。任何尝试运行此应用程序的人都可以随时与我联系以了解如何运行它! :)

标签: spring spring-boot spring-data-jpa thymeleaf


【解决方案1】:

在你的 thymeleaf html 文件中添加这个 xmlns : xmlns:th="http://www.thymeleaf.org"

【讨论】:

    【解决方案2】:

    看着你的控制器

    为什么你已经拯救为日期类型,你可以将它更改为字符串。

    所以你的控制器将是

    @RequestMapping(value ="/home",method = {RequestMethod.POST,RequestMethod.GET})
    public String adddog(@RequestParam("name") String name,
                      @RequestParam("rescued") String rescued,
                      @RequestParam("vaccinated") Boolean vaccinated, Model model)
    {
        dogService.addADog(name, rescued, vaccinated);
        System.out.println("name = " + name + ",rescued = " + rescued + ", vaccinated = " + vaccinated);
        return "index";
    }
    

    现在试试这个网址

    http://localhost:8080/home?name=test&rescued=2014-12-12&vaccinated=true
    

    【讨论】:

      【解决方案3】:

      URL 中缺少您的请求参数(姓名、获救、接种疫苗)

      你的网址应该是

      http://localhost:8080/home?name=ARULSUJU&rescued=2012-12-12&vaccinated=true

      因为所有参数都是必需的

      【讨论】:

      • 现在我收到以下错误 Sun Jul 22 22:20:38 IST 2018 出现意外错误(类型=错误请求,状态=400)。无法将“java.lang.String”类型的值转换为所需的“java.util.Date”类型;嵌套异常是 org.springframework.core.convert.ConversionFailedException: 无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] 的值为“真”;嵌套异常是 java.lang.IllegalArgumentException:值 [true] 的解析尝试失败
      • 正确的网址是localhost:8080/…
      • 你能帮我写代码吗,我已经在我的github上更新了这个link
      猜你喜欢
      • 2020-10-03
      • 2020-11-06
      • 2020-04-20
      • 2020-02-22
      • 2018-07-02
      • 1970-01-01
      • 2018-11-07
      • 2018-05-08
      • 2019-05-02
      相关资源
      最近更新 更多