HTTP状态400 –错误的请求


问题内容

您好,我想在添加新类别的同时选择类别。类别详细信息从DB获取,并且我尝试command通过使用<form:select>标签将其PK提取到产品中。但是它显示以下错误。

错误

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

我的控制器

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

提前致谢!!


问题答案:

此错误与<form:select>标记Still 无关。代码中缺少一些东西,导致此错误。

  1. 在JSP中,您尝试将文件和表单数据一起上载,因此您需要在spring上下文中从common-fileupload.jar定义multipartResolver bean。MultipartResolver Spring
  2. 控制器方法应该像这样更改

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