what is error for the below springmvc code?

22 Views Asked by At

**Here i'm trying to do spring mvc project and i've written code like below, but i'm not getting any output on browser screen. Please anyone can help me?

code1: add_car.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>add page</title>
</head>
<body>
    <div align="center">
        <h3>ADD THE CAR DETAILS</h3>
        
        <form action="add_car" method="post">
            <table border="1px solid">
                <tr>
                    <td>Name:</td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td>Brand:</td>
                    <td><input type="text" name="brand"></td>
                </tr>
                <tr>
                    <td>Price:</td>
                    <td><input type="text" name="price"></td>
                </tr>
            </table>
            <input type="submit" value="ADD">
        </form>
    </div>
    <!-- java logic for msg -->
    
    <%
    String message = (String)request.getAttribute("message");
    if(message!=null){
    %>
        <div align="center"> <h3><%=message%> </h3></div>
    <% 
    }
    %>

</body>
</html>

code 2: CarController.java
package com.jspider.springmvc.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jspider.springmvc.service.CarService;

@Controller
public class CarController {
    
    @Autowired
    CarService carService;
    
    //method
    
    @RequestMapping(path="/add_page", method=RequestMethod.GET)
    public String getAddPage() {
        return "add_car";
        
    }
    
    public String addCar(@RequestParam(name="name") String name,@RequestParam(name="brand") String brand, @RequestParam(name="price") double price, ModelMap modelMap) {
        carService.addCar(name, brand, price);
        modelMap.addAttribute("message", "Car Details Added! ");
        return "add_car";
        
    }
    
    

}

code 3: CarService.java
package com.jspider.springmvc.service;

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

import com.jspider.springmvc.dao.CarDAO;
import com.jspider.springmvc.dto.CarDTO;

@Component
public class CarService {
    
    @Autowired
    private CarDAO carDAO;
    
    //method
    public CarDTO addCar(String name, String brand, double price) {
        CarDTO carDTO = new CarDTO();
        carDTO.setName(name);
        carDTO.setBrand(brand);
        carDTO.setPrice(price);
        
        return carDAO.addCar(carDTO);
    }

}

code 4: CarDAO.java
package com.jspider.springmvc.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.springframework.stereotype.Component;

import com.jspider.springmvc.dto.CarDTO;

@Component
public class CarDAO {
    private EntityManagerFactory entityManagerFactory;
    private EntityManager eManager;
    private EntityTransaction eTransaction;
    
    
    //open conn
    private void openConnection() {
        entityManagerFactory = Persistence.createEntityManagerFactory("car");
        eManager = entityManagerFactory.createEntityManager();
        eTransaction = eManager.getTransaction();
    }
    
    //close conn
    private void closeConnection() {
        if (entityManagerFactory!=null) {
            entityManagerFactory.close();
        }
        if (eManager!=null) {
            eManager.close();
        }
        if (eTransaction!=null) {
            if (eTransaction.isActive()) {
                eTransaction.rollback();
            }
        }
    }
    //---------------------------------------------
    
    
    //ADD CAR
    public CarDTO addCar(CarDTO carDTO) {
        openConnection();
        eTransaction.begin();            //bcz DML oprn
        eManager.persist(carDTO);
        eTransaction.commit();
        closeConnection();
        return carDTO;
    }
    

}

code 5: package com.jspider.springmvc.dto;

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

import lombok.Data;

@Entity
@Data
@Table(name="car")
public class CarDTO {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private String brand;
    @Column(nullable = false)
    private double price;
    

}


server log:
Mar 29, 2024 10:59:34 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /springmvc/add_car

url writtng in to the web browser :
http://localhost:8080/springmvc/add_car.jsp (it may be wrong or right, i'm not sure)

now tell me how to solve this.?


** now please go through code and please tell me how to solve the problem

i'm expecting output(form) which takes car details like name,brand,price etc.

0

There are 0 best solutions below