【问题标题】:HTTP Status 400 – Bad RequestHTTP 状态 400 – 错误请求
【发布时间】:2017-09-07 05:05:09
【问题描述】:

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

错误

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>

提前致谢!!

【问题讨论】:

  • 尝试将请求映射更改为@RequestMapping(value="/productlist/addproduct" 并在表单操作action="/productlist/addproduct"
  • 试过但还是一样
  • 能否提供web.xml和spring的配置文件?原因状态 404 表示根本不去服务器端。

标签: spring-mvc


【解决方案1】:

这个错误与&lt;form:select&gt;标签无关 您的代码中仍然缺少一些导致此错误的内容。

  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)

【讨论】:

  • 在这里您可以找到 web.xml 和 dspatchr.xml 以及 applicationContext.xml...感谢您花时间为我服务
  • 这个解决方案对你有用吗?你的显示文件看起来不错。
【解决方案2】:

这是一个老问题,但如果其他人遇到同样的问题,那么看看我是如何解决的。 该问题与提供的 post 方法无关。它由您重定向到的下一页 ("redirect:/allProduct") 抛出。您的 ORM 无法成功地将数据库结果映射到单个对象,这可能是由于未指定主键或具有评估为 null 的键造成的。所以访问你的数据库并修复它,确保你最后一切都正确。

【讨论】:

    【解决方案3】:

    错误

     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
    

    我在从一个处理程序重定向到另一个处理程序时遇到了同样的错误,我的处理程序正在处理请求,但它无法重定向到另一个页面。

    return"redirect:/employeeReport";

    我尝试了所有方法,但都无法解决我的问题。 然后我才发现Entity属性和表单属性有序列不匹配。刚刚匹配了我所有的 Entity 属性并清理了我的项目问题。

    建议: 将 Entity 类的所有字段与您的表单正确映射,清理项目并再次运行。即使实体字段的映射没有问题,也会出现此错误,然后只需清理项目并重新运行即可。

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 2018-08-09
      • 2021-09-24
      • 2017-12-20
      • 2016-04-20
      • 2017-07-23
      • 2020-05-23
      • 2013-07-21
      • 2015-04-01
      相关资源
      最近更新 更多