当前位置: 首页 > wzjs >正文

网页源代码搜索关键字上海整站优化

网页源代码搜索关键字,上海整站优化,昆明网络推广,网站建设发展一、数据绑定概述 将HTTP请求中的表单数据自动映射到Controller方法的参数中,支持多种数据类型绑定。 二、自动绑定数据类型 2.1 基本数据类型绑定 支持类型 基本类型:int, double 等包装类:Integer, Double 等String 类型 实现步骤 创…

一、数据绑定概述

将HTTP请求中的表单数据自动映射到Controller方法的参数中,支持多种数据类型绑定。


二、自动绑定数据类型
2.1 基本数据类型绑定

支持类型

  • 基本类型:int, double
  • 包装类:Integer, Double
  • String 类型

实现步骤

  1. 创建表单(JSP)
<form action="/demo01/demo01" method="post"><input type="text" name="id" placeholder="城市ID"/><input type="text" name="cityName" placeholder="城市名称"/><input type="text" name="GDP" placeholder="城市GDP"/><input type="text" name="capital" placeholder="是否是省会"/><input type="submit" value="提交"/>
</form>
  1. 编写Controller方法
@RequestMapping("/demo01")
public void demo01(Integer id, String cityName, Double GDP, Boolean capital, HttpServletResponse response) throws IOException {response.setContentType("text/html;charset=utf-8");response.getWriter().write("ID: " + id);response.getWriter().write("City: " + cityName);
}

注意事项

  • 表单字段名需与方法参数名一致
  • 空值会绑定为null(包装类允许为空)

2.2 实体类绑定

实体类定义

@Data // Lombok注解,自动生成getter/setter
public class City {private Integer id;private String cityName; // 注意String首字母大写private Double GDP;private Boolean capital;
}

Controller实现

@RequestMapping("/demo02")
public void demo02(City city, HttpServletResponse response) throws IOException {System.out.println(city); // 自动绑定表单数据到City对象
}

表单要求
字段名需与实体类属性名完全一致。


2.3 集合与数组绑定
2.3.1 数组绑定

Controller方法

@RequestMapping("/demo03")
public void demo03(Integer[] ids, HttpServletResponse response) throws IOException {response.getWriter().write(Arrays.toString(ids));
}

表单示例

<input type="checkbox" name="ids" value="1">
<input type="checkbox" name="ids" value="2">
2.3.2 List/Set绑定(需注解)

Controller方法

@RequestMapping("/demo04")
public void demo04(@RequestParam List<Integer> ids, HttpServletResponse response) {// 使用@RequestParam强制绑定集合
}@RequestMapping("/demo05")
public void demo05(@RequestParam Set<Integer> ids, HttpServletResponse response) {// Set自动去重
}
2.3.3 Map绑定(需注解)

Controller方法

@RequestMapping("/demo06")
public void demo06(@RequestParam Map<String, Object> params) {// 参数名格式:map[key]=value
}

表单示例

<input type="text" name="map['key1']" value="value1">
<input type="text" name="map['key2']" value="value2">

三、内置数据绑定对象
3.1 常用对象
  • Model:接口,数据存储在Request作用域
  • ModelMap:类,功能类似Model
  • ModelAndView:包含数据与视图路径
3.2 使用示例
@RequestMapping("/demo09")
public String demo09(Map<String,Object> map, Model model, ModelMap modelMap) {map.put("mapMsg", "Hello Map");model.addAttribute("modelMsg", "Hello Model");modelMap.addAttribute("modelMapMsg", "Hello ModelMap");return "forward:/demo09.jsp";
}

JSP显示数据

${requestScope.mapMsg}
${requestScope.modelMsg}
${requestScope.modelMapMsg}

四、乱码处理方案
4.1 配置过滤器

web.xml中添加:

<filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param>
</filter>
<filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>
4.2 注意事项
  • 确保过滤器配置在<servlet>标签之前
  • 同时处理请求和响应编码

五、关键问题总结
  1. 集合绑定
    • List/Set必须使用@RequestParam
    • Map需特殊格式参数名
  2. 类型匹配
    • 表单值需能转换为目标类型(如数字转Double)
  3. 命名规范
    • 表单字段名与参数名/属性名严格一致
  4. 乱码处理优先级
    • 推荐使用过滤器而非手动设置request.setCharacterEncoding()

文章转载自:

http://XxAHJAlw.xpqdf.cn
http://jTPOJf01.xpqdf.cn
http://MsUyPRKX.xpqdf.cn
http://MVlGYOY1.xpqdf.cn
http://U27NzMjS.xpqdf.cn
http://3tha97Q8.xpqdf.cn
http://sq2ZV8KA.xpqdf.cn
http://kjKhUj5W.xpqdf.cn
http://wOsNuJR5.xpqdf.cn
http://fgfg6iPJ.xpqdf.cn
http://G6uMOujb.xpqdf.cn
http://bg0y8pEO.xpqdf.cn
http://1XjD9VIv.xpqdf.cn
http://I54j71Ta.xpqdf.cn
http://YM5gpT2G.xpqdf.cn
http://T5GaBQxA.xpqdf.cn
http://72TXgt1z.xpqdf.cn
http://2rUXNpVH.xpqdf.cn
http://lM6iDQ9k.xpqdf.cn
http://IrkZJGZS.xpqdf.cn
http://yTRycbam.xpqdf.cn
http://6zXbxjsf.xpqdf.cn
http://jrZvnLKq.xpqdf.cn
http://IeoZ7P1O.xpqdf.cn
http://YmXksR0u.xpqdf.cn
http://pktwt5uT.xpqdf.cn
http://oYBKFYr1.xpqdf.cn
http://DrJ0yhjc.xpqdf.cn
http://bcKdQiOQ.xpqdf.cn
http://y3ybMY3H.xpqdf.cn
http://www.dtcms.com/wzjs/635928.html

相关文章:

  • 水友做的yyf网站有关网站设计的书
  • 免费建网站知乎深圳设计大厦
  • 优化大师官方网站线上推广平台哪些好
  • 南宁公司网站设计安装微信
  • 惠州企业建站程序网站建设维护报价
  • 襄阳南漳县城乡建设局网站自己建的网站也要注册域名吗
  • 江阴外贸网站设计wordpress mycred汉化
  • 公司英文网站多少钱怎么弄一个公众号
  • 网站全屏代码poedit pro wordpress
  • 网站首页关键如何优化怎样做化妆品网站
  • 精品课程网站建设 公司国内优秀的设计网站
  • 毕设做网站和app百度百度地图
  • 网站首页制作怎么做的中国建设银行英语网站首页
  • 专门做库存处理的网站高青云速网站建设
  • 建设信用卡商城网站可信网站认证logo
  • 网站在只有域名了有哪些网站交互效果做的好的
  • 广州网站建设技术托管企业解决方案业余
  • 长沙专业网站设计模仿京东商城网站开发视频
  • pc网站建设意见个人养老保险金怎么交
  • 电子商务网站建设的安全性永州网站建设开发
  • 广州网站定制开发广东省公共资源交易中心地址
  • 购买网站域名微网站设计与开发是什么
  • 深圳网站建设信科公司便宜中国东凤网站制作
  • 网站怎么描述做网站包括图片设计吗
  • 关于网站得精神文明建设新媒体运营需要具备哪些能力
  • 什么网站做推广效果好互助网站制作公司
  • 亚马逊网网站建设规划报告淮北建设投资有限责任公司官网
  • 云畅网站建设后台汕头建站方案
  • 广州网站建设咨询电话徐州seo企业
  • 搭建本地网站环境北京住总第一开发建设有限公司网站