Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

spring mvc - 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!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...