springmvc 400错误之--checkbox引起

本文解决了一个关于SpringMVC中checkbox导致400错误的问题。详细介绍了如何通过设置checkbox的value属性来正确地传递数据。

springmvc 400错误之--checkbox引起

 

 springmvc传checkbox

checkbox的值变成了 "on"  报400 

Status Code: 400 Bad Request

 

 

springmvc如何接收checkbox的值?

jquery serialize  checkbox的值怎么变成on?

var tabData = $("#form1").serialize();

tabinfo:"basePackageName=dfs&basePageName=fds&tab=test&clearFalg=on"

 

 

<td>&nbsp;&nbsp;<input  type="checkbox" name="check"  />   </td> 这样会有问题.

解决方法:<td>&nbsp;&nbsp;<input  type="checkbox" name="check" value="true"   />   </td>

value的值设为true,选中时就序列化,没选中则不序列化。

 

 

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启SpringMvc注解驱动,不加找不到映射路径--> <mvc:annotation-driven/> <!-- 扫描所有的controller,在ioc容器中生成处理器类的实例--> <context:component-scan base-package="lzy.study.ssm.controller"/> <!-- 配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 静态资源放行--> <mvc:default-servlet-handler/> <!-- <mvc:resources mapping="/js/**" location="/js/"/>--> <!-- 配置拦截器--> </beans><%-- Created by IntelliJ IDEA. User: DOP Date: 2025/5/18 Time: 11:14 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> function batchDelete() { var ids = []; $('input[name="checkItem"]:checked').each(function() { ids.push($(this).val()); }); if (ids.length === 0) { alert("请选择要删除的商品"); return; } $.ajax({ url: "${pageContext.request.contextPath}/goods/batchDelete", type: "DELETE", contentType: "application/json", data: JSON.stringify(ids), success: function(result) { alert(result); location.reload(); }, error: function() { alert("删除失败"); } }); } function batchInsert() { var goodsList = [ {gimg: "图片1", gname: "商品1", gseller: "卖家1", gprice: "100", tid: 1, gmassage: "商品描述1"}, {gimg: "图片2", gname: "商品2", gseller: "卖家2", gprice: "200", tid: 2, gmassage: "商品描述2"} ]; $.ajax({ url: "${pageContext.request.contextPath}/goods/batchInsert", type: "POST", contentType: "application/json", data: JSON.stringify(goodsList), success: function(result) { alert(result); location.reload(); }, error: function() { alert("添加失败"); } }); } </script> </head> <body> <h1>商品列表</h1> <button onclick="batchDelete()">批量删除</button> <button onclick="batchInsert()">批量添加</button> <table border="1"> <tr> <th><input type="checkbox" id="checkAll"></th> <th>ID</th> <th>图片</th> <th>名称</th> <th>卖家</th> <th>价格</th> <th>分类</th> <th>描述</th> </tr> <c:forEach items="${goodsList}" var="goods"> <tr> <td><input type="checkbox" name="checkItem" value="${goods.gid}"></td> <td>${goods.gid}</td> <td>${goods.gimg}</td> <td>${goods.gname}</td> <td>${goods.gseller}</td> <td>${goods.gprice}</td> <td>${goods.type}</td> <td>${goods.gmassage}</td> </tr> </c:forEach> </table> <script> $('#checkAll').click(function() { $('input[name="checkItem"]').prop('checked', $(this).prop('checked')); }); </script> </body> </html> package lzy.study.ssm.controller; import lzy.study.ssm.service.IGoodsService; import lzy.study.ssm.vo.Goods; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.List; @Controller @RequestMapping("/goods") public class GoodsController { @Autowired private IGoodsService goodsService; // 批量删除商品 @DeleteMapping("/batchDelete") @ResponseBody public String batchDeleteGoods(@RequestBody List<Integer> ids) { int result = goodsService.batchDeleteGoods(ids); if (result > 0) { return "删除成功"; } else { return "删除失败"; } } // 批量添加商品 @PostMapping("/batchInsert") @ResponseBody public String batchInsertGoods(@RequestBody List<Goods> goodsList) { int result = goodsService.batchInsertGoods(goodsList); if (result > 0) { return "添加成功"; } else { return "添加失败"; } } // 多表联合条件查询及分页显示商品信息 @GetMapping("/list") public String findGoodsByCondition(@RequestParam(required = false) String keyword, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, Model model) { List<Goods> goodsList = goodsService.findGoodsByCondition(keyword, pageNum, pageSize); model.addAttribute("goodsList", goodsList); return "goodsList"; } }src/main/webapp/goodsList.jsp在上述代码运行过程中输入http://localhost:8080/jsj20224071116/goods/list HTTP状态 404 - 未找到 类型 状态报告 消息 请求的资源[/jsj20224071116/goods/list]不可用 描述 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。 Apache Tomcat/9.0.40找出原因
最新发布
05-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值