ssm

Spring 数据传递<10>

2017-08-18  本文已影响8人  天空在微笑

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
上一篇 下一篇

猜你喜欢

热点阅读