无法运行以下使用JDBC模板的Spring Boot应用程序

时间:2018-07-22 16:27:46

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

我创建了一个简单的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 );
    }
}

百里叶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>

在运行此代码时,我遇到了错误

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

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

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

请帮助我解决此问题

3 个答案:

答案 0 :(得分:1)

您的请求参数在URL(名称,已救援,已接种疫苗)中丢失

您的网址应为

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

因为所有参数都是必需的

答案 1 :(得分:0)

查看您的控制器

为什么您已将其保存为日期类型,所以可以将其更改为字符串。

所以您的控制器将是

@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";
}

现在尝试使用此URL

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

答案 2 :(得分:0)

在您的百里香html文件中添加以下 xmlns : xmlns:th =“ http://www.thymeleaf.org”