Home > Software design >  Hii, I face issue in Spring Boot. Here is my question: My form & table successfully developed, but d
Hii, I face issue in Spring Boot. Here is my question: My form & table successfully developed, but d

Time:05-06

Controller Controller:

package com.isolutions4u.onlineshopping.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.isolutions4u.onlineshopping.model.Reservation;
import com.isolutions4u.onlineshopping.service.ReservationService;

@Controller
public class ReservationController {

@Autowired
private ReservationService reservationService;

@RequestMapping("/reservation")
public String reservationReg()
{
return "contactSave";
}

@RequestMapping("/saveContact")
public String  saveReservation(@ModelAttribute Reservation model)
{
    reservationService.saveMyUser(model);
    return "contactSave";
}
}

Repository Repository:

package com.isolutions4u.onlineshopping.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.isolutions4u.onlineshopping.model.Reservation;
@Repository
public interface ReservationRepository extends CrudRepository<Reservation, Long> {
        public List<Reservation> findAll();
}

Service Service:

package com.isolutions4u.onlineshopping.service;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.isolutions4u.onlineshopping.model.Reservation;
import com.isolutions4u.onlineshopping.repository.ReservationRepository;
@Service
@Transactional
public class ReservationService {
        private ReservationRepository repo;
        public ReservationService(ReservationRepository repo) {
            super();
            this.repo = repo;
        }
        public void saveMyUser(Reservation reservation)
        {
            repo.save(reservation);
        }
}

Model

package com.isolutions4u.onlineshopping.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "reservation")
public class Reservation {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
private String mobile;
private String email;
private String message;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
    this.mobile = mobile;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public Reservation(String name, String mobile, String email, String message) {
super();
this.name = name;
this.mobile = mobile;
this.email = email;
this.message = message;
}

public Reservation()
{

}
@Override
public String toString() {
return "Reservation [name="   name   ", mobile="   mobile   ", email="   email   ", message="   message   "]";
}
}

Form in localhost

fail to save

jsp location Jsp coding :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reservation Form</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>     

</head>
<style>
    body
    {
        background-color: grey;
    }
    .row{
        margin-top: 10%;
        margin-left:35%;
    }
</style>

<body>
    <form method="post" action="/saveContact">
        <div >

        <div >

          <div >

            <div >

              <span >Reservation Form</span>

              <input type="text" placeholder="Enter the name" id="name" name="name" required/>

              <input type="email" placeholder="Enter Email" id="email" name="email" required/>

              <input type="text" placeholder="Enter the number" id="mobile" name="mobile" required/>

              <input type="text" placeholder="Enter Message" id="message" name="message" required />
              
              <input type="submit" value="SAVE">
            </div>
          </div>
        </div>
      </div>
    </form>

    
</body>

</html>

Normally: store my information in the MySQL database, then this page will refresh to an empty table. But I fail to store in my database The above is the problem I'm having, thanks!

Console error:

2022-05-05 15:01:22.852  WARN 9356 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
2022-05-05 15:03:33.517  WARN 9356 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

application-properties:

spring.datasource.url=jdbc:mysql://localhost:3307/test?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username=root
spring.datasource.password= xxx
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.file-size-threshold=1MB
spring.queries.users-query=select email, password, enabled from user_detail where email=?
spring.queries.roles-query=select email, role from user_detail where email=?

CodePudding user response:

From the error you shared in comments, looks like you're making a HTTP POST request while the controller you've defined is for HTTP GET.

Change your controller annotations to the either of the following

@PostMapping("/saveContact")

or

@RequestMapping(path = "/saveContact", method = RequestMethod.POST)

CodePudding user response:

Controller:

package com.isolutions4u.onlineshopping.controllers;

import org.springframework.beans.factory.annotation.Autowired;
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 com.isolutions4u.onlineshopping.model.Reservation;
import com.isolutions4u.onlineshopping.service.ReservationService;


@Controller
public class ReservationController {
    
    @Autowired
    private ReservationService reservationservice;
    
    @RequestMapping(value="/reservation",method=RequestMethod.GET)
    String addReservationForm(Model model) {
        System.out.println("Add reservation Form testing 123");
        model.addAttribute("reservation", new Reservation());
        return"reservation_form";
    }
    @RequestMapping(value="/saveReservation",method=RequestMethod.POST)
     String saveReservation(Reservation reservation_info) {
        System.out.println("Save repository information testing 456");
        reservationservice.saveReservation(reservation_info);
        return"sucessful_page";
    }
}

Model:

package com.isolutions4u.onlineshopping.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="reservation")
public class Reservation {
    
    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String name;
    private String email;
    private String phone_number;
    private String datetime;
    private String message;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone_number() {
        return phone_number;
    }
    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }
    public String getDatetime() {
        return datetime;
    }
    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

Repository:

package com.isolutions4u.onlineshopping.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.isolutions4u.onlineshopping.model.Reservation;

public interface ReservationRepository extends JpaRepository<Reservation,Long>{

    
}

Service:

package com.isolutions4u.onlineshopping.service;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.isolutions4u.onlineshopping.model.Reservation;
import com.isolutions4u.onlineshopping.repository.ReservationRepository;

@Service
@Transactional
public class ReservationService {

    @Autowired
    private ReservationRepository reservationrepo;
    
    public void saveReservation(Reservation Reservation) {
        reservationrepo.save(Reservation);
    }
}

Reservation_form.jsp

<body>
        <h3 style="text-align:center;">Reservation Form</h3>
        <div >
        <div >
        <div >
        <div >
        <table>
            <form:form id="ReservationForm" modelAttribute="reservation" action="saveReservation" method="post">
                    <form:hidden path="id" />
        
                    <form:label path="name" for="name" >Name:</form:label>
                    <form:input path="name" name="name" required="required"/>
            
                    <form:label path="email" for="email" >Email:</form:label>
                    <form:input path="email" name="email" required="required"/>
                    
                    <form:label path="phone_number" for="phone_number" >Email/Phone:</form:label>
                    <form:input path="phone_number" name="phone_number" required="required"></form:input>
                
                    <form:label path="datetime" for="datetime" >Reservation Date & Time</form:label>
                    <form:input path="datetime" name="datetime" required="required"></form:input>
                
                    <form:label path="message" for="message" >Message:</form:label>
                    <form:input path="message" name="message" required="required"></form:input>
                
                    <form:button name="submit" type="submit">Submit</form:button>
                            
            </form:form>
        </table>
        </div>
        </div>
        </div>
        </div>
    </body>
  • Related