Mohanraj
Mohanraj

Reputation: 406

HTTP Status 400 – Bad Request

Hi i am trying to select category while adding new category.Category details get from DB and I am trying to fetch it's PK to product command by using <form:select> tag. But it shows following error.

error

HTTP Status 400 – Bad Request

Type Status Report

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

My Controller

@RequestMapping(value="productlist/addproduct" , method=RequestMethod.POST)
    public String addProdt( @ModelAttribute ("prdt") Product p)
    {   

        pd.addProduct(p);
        MultipartFile prodImage=p.getImage();
        if(!prodImage.isEmpty()){
            Path paths=Paths.get("C:/Users/Dont open/Documents/Eclipse/ClickBuy/src/main/webapp/resources/Images/"+ p.getId()+".png");
            try
        {
            prodImage.transferTo(new File(paths.toString()));
        } catch (IllegalStateException e) 
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        }

        return "redirect:/allProduct";
    } 

Jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ include file="header.jsp"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@ page isELIgnored="false" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#mfg" ).datepicker();
  } );
  </script>
</head>
<body>
<br>
<h2 align="center">PRODUCT FORM</h2><hr>
<div class="col-md-2 "></div>
<div align="center"><div class="container"><div class="col-md-8 ">

<form:form method="POST" action="productlist/addproduct" commandName="prdt" enctype="multipart/form-data">
<table class="table table-hover">
<tr>
<td> <form:label  path="product_Name"> Enter Product Name</form:label></td>
<td><form:input type="text" path="product_Name" class="form-control"/></td>
</tr>
<tr>
<td> <form:label path="descripction"> Enter Product Descripction</form:label></td>
<td><form:input type="text" path="descripction"  class="form-control"/></td>
</tr>
<tr>
<td> <form:label path="category"> Enter Product Category</form:label></td>
<td>
<form:select path="category">
<c:forEach var="x" items="${catg}">
<form:option value="${x.category_id}" label="${x.category_name}" /></c:forEach>
</form:select>
</td>
</tr>
<tr>
 <td> <form:label  path="price"> Enter Product Price</form:label></td>
<td><form:input type="text" path="price" placeholder=" Enter Product Price" class="form-control"/>
</td></tr>
<tr>
<td> <form:label  path="mfg_Date"> Enter Manufacture Date</form:label></td>
<td><form:input type="text" id="mfg" path="mfg_Date" class="form-control"/></td>

</tr>
<tr>
<td> <label> Choose Image</label></td>
<td><form:input type="file" path="image" class="form-control"/></td>
</tr>

</table>
 <input type="submit" class="btn btn-primary btn-block" value="Add" class="form-control"/>

</form:form>
</div></div></div></body>
</html>

Thanks in advance!!

Upvotes: 1

Views: 47819

Answers (3)

Ratnesh K
Ratnesh K

Reputation: 29

Error

 HTTP Status 400 – Bad Request depctive, malfunction
    Type Status Report
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested in logs

I have got the same error while redirecting from one handler to another, my handler was handling request but it couldn't redirected to another Page.

return"redirect:/employeeReport";

I tried all methods but that couldn't solve my issue. Then I just found that there was sequence mismatch in Entity property and form Property. Just matched all my Entity property and cleaned my project issue resolved.

Advice: Map all fields of your Entity class properly with your form, clean project and run again. Even if there is no issue in mapping of Entity's field this error comes then simply clean project and rerun might work.

Upvotes: 0

Adindu Stevens
Adindu Stevens

Reputation: 3567

This is an old question but In case anyone else experiences the same issue then look how I solved it. The problem has nothing to do with the post method that was provide. It is thrown by the next page that you are redirecting to ("redirect:/allProduct"). Your ORM could not successfully map the database result to individual objects, this could be caused by not specifying a primary key or having keys that evaluate to null. So visit your database and fix it, make sure you have everything correct in the end.

Upvotes: 0

Vijendra Kumar Kulhade
Vijendra Kumar Kulhade

Reputation: 2255

This error has nothing to do with <form:select> tag Still There are few things missing in your code which is resulting this error.

  1. In JSP you are trying to upload file along with form data so you need to have multipartResolver bean defined in spring context from common-fileupload.jar MultipartResolver Spring
  2. Controller method should be changed like this

@RequestMapping(value="/productlist/addproduct" , method= RequestMethod.POST,consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ModelAndView addProdt(@ModelAttribute("prdt") Product p,BindingResult bindingResult)

Upvotes: 3

Related Questions