HTTP状态400 - 错误请求

时间:2017-04-11 17:07:30

标签: spring-mvc

您好我正在尝试在添加新类别时选择类别。类别详细信息来自数据库,我正在尝试使用command标记将其提取到产品<form:select>。 但它显示以下错误。

错误

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>

提前致谢!!

2 个答案:

答案 0 :(得分:2)

此错误与<form:select>标记无关 仍然在您的代码中缺少一些导致此错误的内容。

  1. 在JSP中,您尝试上传文件和表单数据,因此您需要在common-fileupload.jar中在spring上下文中定义multipartResolver bean。 MultipartResolver Spring
  2. 控制器方法应该像这样改变
  3. @RequestMapping(value="/productlist/addproduct" , method= RequestMethod.POST,consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public ModelAndView addProdt(@ModelAttribute("prdt") Product p,BindingResult bindingResult)
    

答案 1 :(得分:0)

这是一个古老的问题,但是如果其他任何人遇到同样的问题,请看看我是如何解决的。 该问题与提供的post方法无关。重定向到("redirect:/allProduct"的下一页将引发该错误。您的ORM无法成功将数据库结果映射到单个对象,这可能是由于未指定主键或键值评估为空引起的。因此,请访问数据库并进行修复,确保最后一切都正确。