Spring 数据传递<10>

1.javabean封装的对象类型list

   //批量修改items
    @RequestMapping(value = "/item/updateItems")
    public ModelAndView updateItems(WrapperItem wrapperItem) {
        System.out.print("wrapperItem:" + wrapperItem.toString());
//        Items item = itemService.selectByPrimaryKey(id);
        ModelAndView modelAndView = new ModelAndView();
        // 设置数据到模型中
        modelAndView.addObject("username", "成功");
        // 设置视图jsp,需要设置视图的物理地址
        modelAndView.setViewName("index1");
        return modelAndView;
    }
package com.company.combine.model;

import java.util.List;

/**
 * Created by liuqun on 2017/8/15.
 */
public class WrapperItem {
    private Items items;
    private List<Items> itemsList;
    public Items getItems() {
        return items;
    }

    public void setItems(Items items) {
        this.items = items;
    }

    public List<Items> getItemsList() {
        return itemsList;
    }

    public void setItemsList(List<Items> itemsList) {
        this.itemsList = itemsList;
    }

    @Override
    public String toString() {
        return "WrapperItem{" +
                "items=" + items +
                ", itemsList=" + itemsList +
                '}';
    }
}
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem" method="post">
    查询条件:
    <table width="100%" border=1>
        <tr>
            <td><input type="submit" value="查询"/></td>
        </tr>
    </table>
</form>
    商品列表:
<%--<form action="${pageContext.request.contextPath }/item/deleteItems" method="post">--%>
<form action="${pageContext.request.contextPath }/item/updateItems" method="post">
<table width="100%" border=1>
        <tr>
            <td><input type="checkbox" name="ids" value=""/></td>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>生产日期</td>
            <td>商品描述</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${itemList }" var="item" varStatus="s">
            <tr>
                <td><input type="checkbox" name="ids" value="${item.id}"/></td>
                <td> <input type="text" name="itemsList[${s.index}].name" value="${item.name}"/></td>
                <td><input type="text" name="itemsList[${s.index}].price" value="${item.price}"/></td>
                <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                <td><input type="text" name="itemsList[${s.index}].detail" value="${item.detail}"/></td>
                <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
            </tr>
        </c:forEach>

    </table>
    <input type="submit"  value="删除"/>
    <input type="submit"  value="修改"/>
</form>
</body>

</html>
package com.company.combine.model;

import java.util.Date;

public class Items {
    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

    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 == null ? null : name.trim();
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic == null ? null : pic.trim();
    }

    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 == null ? null : detail.trim();
    }

    @Override
    public String toString() {
        return "Items{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", pic='" + pic + '\'' +
                ", createtime=" + createtime +
                ", detail='" + detail + '\'' +
                '}';
    }
}

打印结果

wrapperItem:WrapperItem{items=null, itemsList=[Items{id=null, name='台式机台式111', price=3000.0, pic='null', createtime=null, detail='质量很好'}, Items{id=null, name='笔记本111', price=6000.0, pic='null', createtime=null, detail='笔记本性能好,质量好!!!!!'}, Items{id=null, name='背包111', price=200.0, pic='null', createtime=null, detail='名牌背包,容量大质量好!!!!'}]}

2.数组类型

//数组
    @RequestMapping(value = "/item/deleteItems")
    public ModelAndView deleteItems(Integer[] ids) {
        System.out.print("id:" + Arrays.toString(ids));
//        Items item = itemService.selectByPrimaryKey(id);
        ModelAndView modelAndView = new ModelAndView();
        // 设置数据到模型中
        modelAndView.addObject("username", "成功");
        // 设置视图jsp,需要设置视图的物理地址
        modelAndView.setViewName("index1");
        return modelAndView;
    }
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem" method="post">
    查询条件:
    <table width="100%" border=1>
        <tr>
            <td><input type="submit" value="查询"/></td>
        </tr>
    </table>
</form>
    商品列表:
<form action="${pageContext.request.contextPath }/item/deleteItems" method="post">
<%--<form action="${pageContext.request.contextPath }/item/updateItems" method="post">--%>
<table width="100%" border=1>
        <tr>
            <td><input type="checkbox" name="ids" value=""/></td>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>生产日期</td>
            <td>商品描述</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${itemList }" var="item" varStatus="s">
            <tr>
                <td><input type="checkbox" name="ids" value="${item.id}"/></td>
                <td> <input type="text" name="name" value="${item.name}"/></td>
                <td><input type="text" name="price" value="${item.price}"/></td>
                <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                <td><input type="text" name="detail" value="${item.detail}"/></td>
                <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
            </tr>
        </c:forEach>

    </table>
    <input type="submit"  value="删除"/>
    <input type="submit"  value="修改"/>
</form>
</body>
</html>

打印结果

id:[1, 2]
  1. 包装类类型以及简单类型
    @RequestMapping(value = "/updateitem.action")
    public ModelAndView updateItem(Integer id,String name,Float price,String pic,String detail,Items item, Model model) {
        System.out.println("id:" + id);
        System.out.println("name:" + name);
        System.out.println("price:" + price);
        System.out.println("pic:" + pic);
        System.out.println("detail:" + detail);
        System.out.println("items:" + item.toString());
        System.out.println("model:" + model.toString());
        if (item != null) {
            item.setCreatetime(new Date());
        }
        int id1 = itemService.updateByPrimaryKey(item);
        System.out.println("id:" + id1);
        ModelAndView modelAndView = new ModelAndView();
        // 设置数据到模型中
        modelAndView.addObject("username", "修改成功");
        // 设置视图jsp,需要设置视图的物理地址
        modelAndView.setViewName("index1");
        return modelAndView;
    }
<%@ 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"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改商品信息</title>
<script type="text/javascript" src="<%=basePath%>js/jquery.min.js"></script>
<script type="text/javascript">
<%--$(function(){--%>
<%--//  alert(1);--%>
    <%--var params = '{"id": 1,"name": "测试商品","price": 99.9,"detail": "测试商品描述","pic": "123456.jpg","more":{"one":"one"}}';--%>

<%--//  $.post(url,params,function(data){--%>
        <%--//回调--%>
<%--//  },"json");//--%>
    <%--$.ajax({--%>
        <%--url : "${pageContext.request.contextPath }/json.action",--%>
        <%--data : params,--%>
        <%--contentType : "application/json;charset=UTF-8",//发送数据的格式--%>
        <%--type : "post",--%>
        <%--dataType : "json",//回调--%>
        <%--success : function(data){--%>
<%--//          alert(data.name);--%>

        <%--},--%>
    <%--});--%>
<%--});--%>
</script>
</head>
<body> 
    <!-- 上传图片是需要指定属性 enctype="multipart/form-data" -->
    <form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post" >
    <%--<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post" enctype="multipart/form-data">--%>
        <input type="hidden" name="id" value="${item.id }" /> 修改商品信息:
        <table width="100%" border=1>
            <tr>
                <td>商品名称</td>
                <td><input type="text" name="name" value="${item.name }" /></td>
            </tr>
            <tr>
                <td>商品价格</td>
                <td><input type="text" name="price" value="${item.price }" /></td>
            </tr>
            <tr>
                <td>商品日期</td>
                <td><input type="text" name="createtime" value="${item.createtime }" /></td>
            </tr>


            <tr>
                <td>商品图片</td>
                <td>
                    <c:if test="${item.pic != null}">
                        ![](/pic/${item.pic})
                        <br/>
                    </c:if>
                    <input type="file"  name="pic"/>
                </td>
            </tr>
            <tr>
                <td>商品简介</td>
                <td><textarea rows="3" cols="30" name="detail">${item.detail }</textarea>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="提交" />
                </td>
            </tr>
        </table>

    </form>
</body>

</html>

打印结果:

id:1
name:台式机台式1111111
price:3000.0112
pic:
detail:质量很好1111
items:Items{id=1, name='台式机台式1111111', price=3000.0112, pic='', createtime=null, detail='质量很好1111'}
model:{items=Items{id=1, name='台式机台式1111111', price=3000.0112, pic='', createtime=null, detail='质量很好1111'}, org.springframework.validation.BindingResult.items=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
id:1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,961评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,444评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,009评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,082评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,101评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,271评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,738评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,395评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,539评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,434评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,481评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,160评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,749评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,816评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,038评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,548评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,140评论 2 341

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,538评论 18 399
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,297评论 25 707
  • 当你说“我很焦虑”时,你知道自己为什么焦虑吗?知道自己在焦虑什么吗?知道怎么让焦虑的阻力变动力吗?如果你不知道,希...
    周计划践行者_桔子阅读 196评论 0 0
  • 原以为是个多么感人肺腑倾国倾城的爱情故事才能担得起这样一个名字——倾城之恋。 到头来是“他不过是一个自私的男子,她...
    移川藏喜阅读 723评论 0 7
  • 去参加了两次何老师开办的醒职场,让我想通了一个观念和学到了一堆干货。也不能一个人偷着乐,于是分享出来大家共同进步。...
    下里巴来的人阅读 342评论 0 1