浏览器和服务器的数据交互方式主要有六种:
1.表单提交;
2.超链接;
3.js/jquery方式
3.1 地址栏的替换 location.href="地址栏";(本文有)
3.2 js提交表单___利用form的action属性(本文有)
3.3 ajax(本文有)
前端页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ 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>
<base href="<%=basePath%>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
<script type="text/javascript" src="resource/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(function(){
$("#selectAll").click(function(){
$("[name=ids]").attr("checked",this.checked);
})
$("#updateAll").click(function(){
if($("input:checked").length==0){
alert("请先选择要删除的数据");
return;
}
var ids="";
$("input:checked").each(function(){
ids = ids+this.value+",";
})
ids=ids.substring(0,ids.length-1);
alert(ids);
location.href = "formSubmit/playPlay?ids="+ids;
})
$("#deleteAll").click(function(){
if($("input:checked").length==0){
alert("请先选择要删除的数据");
return;
}
$("#myform").attr("action","formSubmit/deleteItems").submit();
})
$("#ajaxTest").click(function(){
var url = "formSubmit/ajax";
var params = $("#myform").serialize();
alert(params);
$.post(url,params,function(data){
alert(data);
},"text")
})
})
function jsonTest() {
$.ajax({
type:"post",
url:"formSubmit/editItemSubmit_RequestJson",
contentType:"application/json;charset=utf-8",
data:'{"name":"测试商品","price":99.9}',
success:function(data){
var jsonStr=JSON.stringify(data);
alert(JSON.parse(jsonStr)['id']);
alert(JSON.parse(jsonStr).id);
}
});
}
</script>
</head>
<body>
<form action="" method="post" id="myform">
查询条件:
<table width="100%" border=1>
<tr>
<td>
商品名称:<input name="items.name" >
<input type="button" value="批量删除" id="deleteAll" >
<input type="button" value="批量修改" id="updateAll" >
<input type="button" value="ajax测试" id="ajaxTest">
<input type="button" value="json测试" id="ajaxTest" onclick="jsonTest()">
</td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td><input type="checkbox" id="selectAll"></td>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList}" var="item" varStatus="status">
<input type="hidden" name="itemList[${status.index }].id" value ="${item.id}" >
<tr>
<td><input type="checkbox" name="ids" value="${item.id}"></td>
<td><input type="text" name="itemList[${status.index }].name" value="${item.name }"></td>
<td><input type="text" name="itemList[${status.index }].price" value="${item.price }"></td>
<td><input type="text" name="itemList[${status.index }].createtime"
value="<fmt:formatDate value="${item.createtime}"
pattern="yyyy-MM-dd HH:mm:ss"/>"></td>
<td><input type="text" name="itemList[${status.index }].detail" value="${item.detail }"></td>
<td><a href="item/itemEdit/${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
后端代码
package cn.nrsc.ssm.action;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.nrsc.ssm.pojo.Items;
import cn.nrsc.ssm.pojo.QueryVo;
import cn.nrsc.ssm.service.ItemsService;
@Controller
@RequestMapping("/formSubmit")
public class FormSubmitTest {
@Resource
private ItemsService itemsService;
@RequestMapping("list")
public String list(HttpServletRequest request){
List<Items> list = itemsService.findAllItems();
request.setAttribute("itemList", list);
return "formTest";
}
@RequestMapping("ajax")
public String demo1_ajax(QueryVo queryVo,Integer[] ids) {
System.out.println(111);
return "redirect:list";
}
@RequestMapping("deleteItems")
public String deleteItems(QueryVo queryVo, Integer[] ids){
System.out.println(111);
return "redirect:list";
}
@RequestMapping("editItemSubmit_RequestJson")
public @ResponseBody Items editItemSubmit_RequestJson(@RequestBody Items items) {
items.setId(111);
return items;
}
@RequestMapping("/playPlay")
public String playPlay(String ids){
System.out.println(ids);
return "redirect:list";
}
}