Arul Suju
Arul Suju

Reputation: 706

Unable to Run the following Spring Boot Application that uses JDBC Template

I have created a simple Spring Boot Application that adds Dog information into the MySql database.

The controller class for this Application is 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";
    }


    }

and the corresponding Service class is

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 );
    }
}

And the thymeleaf HTML File is

<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>

While running this code i am getting following Error

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jul 22 21:50:32 IST 2018 There was an unexpected error (type=Bad Request, status=400). Required String parameter 'name' is not present

url is http://localhost:8080/home

Kindly help me to solve this issues

Upvotes: 0

Views: 113

Answers (3)

Berthol Yvano
Berthol Yvano

Reputation: 336

in your thymeleaf html file add this xmlns : xmlns:th="http://www.thymeleaf.org"

Upvotes: 0

MyTwoCents
MyTwoCents

Reputation: 7624

Looking at your controller

Why you have rescued as Date Type you can change it as String.

So your controller will be

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

Now try this URL

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

Upvotes: 0

Kathirvel Subramanian
Kathirvel Subramanian

Reputation: 684

Your Request parameters are missing in URL( name,rescued,vaccinated)

Your url should be

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

because all the parameters are required

Upvotes: 1

Related Questions