日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

005_请求参数

發布時間:2025/4/17 编程问答 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 005_请求参数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一. SpringMVC參數綁定過程

1. 從客戶端請求的key/value數據, 經過參數綁定, 將key/value數據綁定到controller方法的形參上。

2. 處理器適配器調用SpringMVC提供的參數綁定組件, 將key/value數據轉換成controller方法的形參。

3. SpringMVC的參數綁定組件: SpringMVC提供了很多converter(轉換器), 能夠將任意類型轉換為Java的類型。在特殊情況下需要自定義converter, 最常用的就是自定義日期類型轉換的converter。

二. 簡單類型的參數綁定

1. Java的一些簡單類型在Web表單中常用的有: int、float、double、String、boolean, 以及它們的數組類型int[]、float[]、double[]、String[]、boolean[], 只要表單中name的值和處理器方法中的形參名稱一致可以直接賦值成功。

2. 簡單類型參數綁定

3. 簡單數組類型參數綁定

二. 通過@RequestParam注解對簡單類型的參數綁定

1. 如果使用@RequestParam注解, 不限制表單中name的值必須和處理器方法中的形參名一致, 可以指定。

2. 使用@RequestParam注解, 還可以通過設置required指定參數是否是必傳參數。

3. 使用@RequestParam注解, 還可以通過設置defaultValue指定默認值。

4. 使用@RequestParam注解, 解決參數不一致的問題, 設置默認值和是否是必傳參數例子:

三. pojo綁定

1. 表單中name的值和處理器方法中的形參的pojo的屬性名一致, 才能成功綁定成功。

2. 頁面中表單name的值

3. 處理器形參的pojo的屬性名

四. 自定義參數綁定實現日期類型綁定和注解實現日期類型綁定

1. 對應處理器方法形參中pojo對象, 如果屬性中有日期類型, 需要自定義參數綁定。

2. 自定義日期轉換類, 將表單中字符串類型的日期轉換成java.util.Date類型的日期

3. 需要向處理器適配器中注入自定義的參數綁定組件

4. 自定義參數綁定實現日期類型綁定使用起來有點復雜, Spring提供了注解綁定日期參數。只需要在pojo的屬性上添加如下日期格式化注解就一步到位了。

五. 包裝的pojo

1. 有的時候我們可能需要用到包裝的pojo。只要表單的name值是包裝的pojo屬性名+.+包裝的pojo實體類的屬性名, SpringMVC就能夠自動進行參數綁定。

2. 包裝的pojo類

3. 頁面中表單的name值

六. 包裝List<pojo>的pojo

1. 通常在需要批量提交數據時, 將提交的數據綁定到包裝List<pojo>的pojo類中。

2. 包裝List<pojo>的pojo類

3. 頁面中表單的name值

七. 包裝Map的pojo

1. 包裝map的pojo類

2. 頁面中表單的name值

八. 中文亂碼

1. SpringMVC解決post亂碼, 在web.xml里配置如下過濾器

2. get亂碼

2.1. tomcat默認的編碼方式是ISO8859-1。

2.2. 在tomcat的conf/server.xml配置文件中添加URIEncoding="utf-8"

九. 例子

1. 新建一個名為SpringMVCReqParam的Java工程, 拷入相關jar包

2. 新建Product.java

package com.lywgames.domain;import java.io.Serializable; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat;public class Product implements Serializable {private static final long serialVersionUID = 1L;// 商品idprivate Integer id;// 商品名稱private String name;// 商品價格private Double price;// 商品創建時間@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")private Date createtime;// 商品描述private String detail;// 商品圖片private String picture;public Product() {}public Product(Integer id, String name, Double price, Date createtime, String detail, String picture) {this.id = id;this.name = name;this.price = price;this.createtime = createtime;this.detail = detail;this.picture = picture;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}public Date getCreatetime() {return createtime;}public void setCreatetime(Date createtime) {this.createtime = createtime;}public String getDetail() {return detail;}public void setDetail(String detail) {this.detail = detail;}public String getPicture() {return picture;}public void setPicture(String picture) {this.picture = picture;}}

3. 新建ProductDetailVo.java

package com.lywgames.domain;public class ProductDetailVo {private Product product;public Product getProduct() {return product;}public void setProduct(Product product) {this.product = product;}}

4. 新建QueryVo.java

package com.lywgames.domain;import java.util.List;public class QueryVo {private List<Product> products;public List<Product> getProducts() {return products;}public void setProducts(List<Product> products) {this.products = products;} }

5. 新建ProductDetailMapVo.java

package com.lywgames.domain;import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map;public class ProductDetailMapVo {private Map<String, String> product = new HashMap<String, String>();public Map<String, String> getProduct() {return product;}public void setProduct(Map<String, String> product) {this.product = product;}public Product toProduct() {Integer id = Integer.parseInt(product.get("id")); String name = product.get("name"); Double price = Double.parseDouble(product.get("price")); Date date = null;SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {date = sdf.parse(product.get("createtime"));} catch (ParseException e) {e.printStackTrace();}String detail = product.get("detail"); String picture = product.get("picture");return new Product(id, name, price, date, detail, picture);} }

6. 新建ProductMapper.java

package com.lywgames.mapper;import java.util.List; import com.lywgames.domain.Product;public interface ProductMapper {public List<Product> getProductList();public Product getProductById(int id);public int updateProduct(Product product);public int deleteProductById(int id);public int addProduct(Product product); }

7. 新建ProductMapper.xml配置

8. 新建ProductService.java

package com.lywgames.service;import java.util.List; import com.lywgames.domain.Product;public interface ProductService {public List<Product> getProductList();public Product getProductById(int id);public int updateProduct(Product product);public int deleteProductById(int id);public int addProduct(Product product); }

9. 新建ProductServiceImpl.java

package com.lywgames.service.impl;import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.lywgames.domain.Product; import com.lywgames.mapper.ProductMapper; import com.lywgames.service.ProductService;@Service public class ProductServiceImpl implements ProductService {@Resourceprivate ProductMapper productMapper;@Overridepublic List<Product> getProductList() {return productMapper.getProductList();}@Overridepublic Product getProductById(int id) {return productMapper.getProductById(id);}@Overridepublic int updateProduct(Product product) {return productMapper.updateProduct(product);}@Overridepublic int deleteProductById(int id) {return productMapper.deleteProductById(id);}@Overridepublic int addProduct(Product product) {return productMapper.addProduct(product);}}

10. 新建ProductAction.java

package com.lywgames.web.action;import java.io.File; import java.io.IOException; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import com.lywgames.domain.Product; import com.lywgames.domain.ProductDetailMapVo; import com.lywgames.domain.ProductDetailVo; import com.lywgames.domain.QueryVo; import com.lywgames.service.ProductService;@Controller public class ProductAction {@Resourceprivate ProductService productService;@RequestMapping("productList")public ModelAndView getProductList() {ModelAndView modelAndView = new ModelAndView();List<Product> productList = productService.getProductList();modelAndView.addObject("productList", productList);modelAndView.setViewName("productList");return modelAndView;}@RequestMapping("getProductById")public ModelAndView getProductById(int id) {ModelAndView modelAndView = new ModelAndView();Product product = productService.getProductById(id);modelAndView.addObject("product", product);modelAndView.setViewName("modifyProduct");return modelAndView;}@RequestMapping("updateProduct")public ModelAndView updateProduct(Product product, String picture, @RequestParam("picFile")MultipartFile picFile, HttpServletRequest req) {if(picFile != null) {String picOldName = picFile.getOriginalFilename();if(picOldName != null && picOldName != "") {String picNewName = UUID.randomUUID().toString() + picOldName.substring(picOldName.lastIndexOf("."));File file = new File(req.getServletContext().getRealPath("/pic") + "/" + picNewName);try {picFile.transferTo(file);product.setPicture(req.getRequestURL().substring(0, req.getRequestURL().indexOf(req.getServletPath())) + "/pic/" + picNewName);} catch (IllegalStateException | IOException e) {e.printStackTrace();}}else {product.setPicture(picture);}}ModelAndView modelAndView = new ModelAndView();int result = productService.updateProduct(product);System.out.println("update操作, mybatis返回值:" + result);List<Product> productList = productService.getProductList();modelAndView.addObject("productList", productList);modelAndView.setViewName("productList");return modelAndView;}@RequestMapping(value={"deleteProductById", "delete"}, method={RequestMethod.GET})public ModelAndView deleteProductById(@RequestParam(value="id", required=true, defaultValue="0") Integer idInteger) {ModelAndView modelAndView = new ModelAndView();int result = productService.deleteProductById(idInteger);System.out.println("delete操作, mybatis返回值:" + result);List<Product> productList = productService.getProductList();modelAndView.addObject("productList", productList);modelAndView.setViewName("productList");return modelAndView;}@RequestMapping("deleteBatch")public ModelAndView deleteBatch(Integer[] ids) {if(ids != null && ids.length > 0) {for (Integer id : ids) {int result = productService.deleteProductById(id);System.out.println("delete操作, mybatis返回值:" + result);}}ModelAndView modelAndView = new ModelAndView();List<Product> productList = productService.getProductList();modelAndView.addObject("productList", productList);modelAndView.setViewName("productList");return modelAndView;}@RequestMapping("batchAddProductUi")public String updateProduct() {return "batchAddProduct";}@RequestMapping(value="batchAddProduct", method={RequestMethod.POST})public ModelAndView batchAddProduct(QueryVo vo, @RequestParam("picFile")MultipartFile[] picFile, HttpServletRequest req) {if(vo.getProducts() != null && vo.getProducts().size() > 0) {for (int i = 0; i < vo.getProducts().size(); i++) {if(vo.getProducts().get(i).getPicture() != null) {String picOldName = picFile[i].getOriginalFilename();String picNewName = UUID.randomUUID().toString() + picOldName.substring(picOldName.lastIndexOf("."));File file = new File(req.getServletContext().getRealPath("/pic") + "/" + picNewName);try {picFile[i].transferTo(file);vo.getProducts().get(i).setPicture(req.getRequestURL().substring(0, req.getRequestURL().indexOf(req.getServletPath())) + "/pic/" + picNewName);} catch (IllegalStateException | IOException e) {e.printStackTrace();}}int result = productService.addProduct(vo.getProducts().get(i));System.out.println("add操作, mybatis返回值:" + result);}}ModelAndView modelAndView = new ModelAndView();List<Product> productList = productService.getProductList();modelAndView.addObject("productList", productList);modelAndView.setViewName("productList");return modelAndView;}@RequestMapping("productDetail1")public ModelAndView productDetail1(ProductDetailVo vo) {ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("product", vo.getProduct());modelAndView.setViewName("product");return modelAndView;}@RequestMapping("productDetail2")public ModelAndView productDetail2(ProductDetailMapVo vo) {ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("product", vo.toProduct());modelAndView.setViewName("product");return modelAndView;} }

11. 新建FormDateConverter.java

package com.lywgames.web.util;import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.core.convert.converter.Converter;/*** 自定義日期轉換類, 將表單中字符串類型的日期轉換成java.util.Date類型的日期*/ public class FormDateConverter implements Converter<String, Date> {@Overridepublic Date convert(String source) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {return sdf.parse(source);} catch (ParseException e) {e.printStackTrace();}return null;}}

12. 新建applicationContext-dao.xml配置

13. 新建applicationContext-service.xml配置

14. 新建springmvc.xml配置

15. 新建jdbc.properties配置

16. 新建SqlMapConfig.xml配置

17. 修改web.xml配置

18. 新建index.jsp

19. 新建batchAddProduct.jsp

20. 新建modifyProduct.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><!DOCTYPE html><head><meta charset="utf-8" /><title>修改商品信息</title></head><body> <form action="updateProduct.action" method="post" enctype="multipart/form-data"><input type="hidden" name="id" value="${product.id }" /> <table width="100%" border=1><tr><td colspan="2" align="center">修改商品信息</td></tr><tr><td>商品名稱</td><td><input type="text" name="name" value="${product.name }"/></td></tr><tr><td>商品價格</td><td><input type="text" name="price" value="${product.price }"/></td></tr><tr><td>生產日期</td><td><input type="text" name="createtime" value="<fmt:formatDate value="${product.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td></tr><tr><td>商品描述</td><td><input type="text" name="detail" value="${product.detail }"/></td></tr><tr><td>商品圖片</td><td><c:if test="${product.picture != null}"><img name="picture" alt="商品圖片" src="${product.picture}" height="100px" width="100px"/><input type="hidden" name="picture" value="${product.picture }"/></c:if><input type="file" name="picFile"/></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交"></td></tr></table></form></body> </html>

21. 新建product.jsp

22. 新建productList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><!DOCTYPE html><head><meta charset="utf-8" /><title>查詢商品列表</title><script type="text/javascript">function createForm(action, idName, id, nameName, name, priceName, price, detailName, detail, pictureName, picture, createtimeName, createtime){var form = document.createElement('form');form.style = "display:none;";form.action = action;form.method = 'post'; var iptId = document.createElement('input');iptId.name = idName;iptId.value = id;form.appendChild(iptId);var iptName = document.createElement('input');iptName.name = nameName;iptName.value = name;form.appendChild(iptName);var iptPrice = document.createElement('input');iptPrice.name = priceName;iptPrice.value = price;form.appendChild(iptPrice);var iptDetail = document.createElement('input');iptDetail.name = detailName;iptDetail.value = detail;form.appendChild(iptDetail);var iptPicture = document.createElement('input');iptPicture.name = pictureName;iptPicture.value = picture;form.appendChild(iptPicture);var iptCreatetime = document.createElement('input');iptCreatetime.name = createtimeName;iptCreatetime.value = createtime;form.appendChild(iptCreatetime);document.body.appendChild(form);form.submit();document.body.removeChild(form);}function productDetail1(id,name,price,createtime,detail,picture){createForm('productDetail1.action','product.id',id,'product.name',name,'product.price',price,'product.detail',detail,'product.picture',picture,'product.createtime',createtime)}function productDetail2(id,name,price,createtime,detail,picture){createForm('productDetail2.action',"product['id']",id,"product['name']",name,"product['price']",price,"product['detail']",detail,"product['picture']",picture,"product['createtime']",createtime)}</script></head><body> <form action="deleteBatch.action" method="post"><table width="100%" border=1><tr><td colspan="7" align="center">查詢商品列表</td></tr><tr><td>選擇</td><td>商品名稱</td><td>商品價格</td><td>生產日期</td><td>商品描述</td><td>商品圖片</td><td>操作</td></tr><c:forEach items="${productList}" var="product"><tr><td><input type="checkbox" name="ids" value="${product.id}"></td><td>${product.name}</td><td>${product.price}</td><td><fmt:formatDate value="${product.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td><td>${product.detail}</td><td><img src="${product.picture}" alt="商品圖片" height="100px" width="100px" /></td><td><a href="getProductById.action?id=${product.id}">修改</a><a href="deleteProductById.action?id=${product.id}">刪除</a><a href="javascript:productDetail1('${product.id}','${product.name}','${product.price}','<fmt:formatDate value="${product.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>','${product.detail}','${product.picture}')">詳情1</a><a href="javascript:productDetail2('${product.id}','${product.name}','${product.price}','<fmt:formatDate value="${product.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>','${product.detail}','${product.picture}')">詳情2</a></td></tr></c:forEach><tr><td colspan="7" align="center"><input type="submit" value="批量刪除"/></td></tr></table></form></body> </html>

總結

以上是生活随笔為你收集整理的005_请求参数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。