目录
以下这个例子涉及到的知识点如下:
绑定参数-默认参数
绑定参数-基本类型
绑定参数-bean对象
绑定参数-包装类
解决post提交乱码问题
json数据交互
使用ajax完成数据回显
绑定参数-list和array
此项目下载地址:https://pan.baidu.com/s/1eVndtbGFzTAejT2SR75q_g 提取码:smpe
需要导入的jar包:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm_project_springmvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 过滤器,解决表单post提交乱码问题 -->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<!-- 拦截.do结尾的 -->
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 读取配置文件 数据库 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.sikiedu"></context:component-scan>
<!-- 开启注解驱动 -->
<mvc:annotation-driven/>
<!-- 事务核心管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置mybatis -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.sikiedu.bean"></property>
</bean>
<!-- mapper工厂 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sikiedu.mapper"></property>
</bean>
</beans>
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web01
jdbc.username=root
jdbc.password=root
ItemInfo.java:
/**
*
*/
package com.sikiedu.bean;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
public class ItemInfo {
private Integer item_id;
private String item_name;
private String item_type;
private Double item_price;
public Integer getItem_id() {
return item_id;
}
public void setItem_id(Integer item_id) {
this.item_id = item_id;
}
public String getItem_name() {
return item_name;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
public String getItem_type() {
return item_type;
}
public void setItem_type(String item_type) {
this.item_type = item_type;
}
public Double getItem_price() {
return item_price;
}
public void setItem_price(Double item_price) {
this.item_price = item_price;
}
@Override
public String toString() {
return "ItemInfo [item_id=" + item_id + ", item_name=" + item_name + ", item_type=" + item_type
+ ", item_price=" + item_price + "]";
}
}
ItemInfoVo.java:
/**
*
*/
package com.sikiedu.bean;
/**
*
*
* @author Lijiang
*2019年5月5日
*/
public class ItemInfoVo {
private ItemInfo itemInfo;
public ItemInfo getItemInfo() {
return itemInfo;
}
public void setItemInfo(ItemInfo itemInfo) {
this.itemInfo = itemInfo;
}
}
ItemService.java:
/**
*
*/
package com.sikiedu.service;
import java.util.List;
import com.sikiedu.bean.ItemInfo;
import com.sikiedu.bean.ItemInfoVo;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
public interface ItemService {
public List<ItemInfo> selectAll();
public ItemInfo selectItemInfoById(Integer id);
public void deleteById(Integer id);
// 保存
public void save(ItemInfo item);
// 包装类 多条件查询
public List<ItemInfo> selectByVo(ItemInfoVo vo);
}
ItemServicelmpl.xml:
/**
*
*/
package com.sikiedu.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sikiedu.bean.ItemInfo;
import com.sikiedu.bean.ItemInfoVo;
import com.sikiedu.mapper.ItemMapper;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
@Service
public class ItemServicelmpl implements ItemService {
@Autowired
private ItemMapper itemMapper;
// 查询所有记录
@Override
public List<ItemInfo> selectAll() {
return itemMapper.selectAll();
}
@Override
public ItemInfo selectItemInfoById(Integer id) {
return itemMapper.selectItemInfoById(id);
}
@Override
public void deleteById(Integer id) {
itemMapper.deleteById(id);
}
@Override
public void save(ItemInfo item) {
itemMapper.save(item);
}
@Override
public List<ItemInfo> selectByVo(ItemInfoVo vo) {
return itemMapper.selectByVo(vo);
}
}
ItemMapper.java:
/**
*
*/
package com.sikiedu.mapper;
import java.util.List;
import com.sikiedu.bean.ItemInfo;
import com.sikiedu.bean.ItemInfoVo;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
public interface ItemMapper {
public List<ItemInfo> selectAll();
public ItemInfo selectItemInfoById(Integer id);
public void deleteById(Integer id);
public void save(ItemInfo item);
public List<ItemInfo> selectByVo(ItemInfoVo vo);
}
ItemMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sikiedu.mapper.ItemMapper">
<!-- 查询所有记录 -->
<!-- public List<ItemInfo> selectAll(); -->
<select id="selectAll" resultType="ItemInfo">
select * from item_info
</select>
<!-- 根据id查询记录。默认参数绑定(HttpServletRequest request,HttpServletResponse response,HttpSession session ,Model model) -->
<!-- public ItemInfo selectItemInfoById(String id); -->
<select id="selectItemInfoById" parameterType="Integer" resultType="ItemInfo">
select * from item_info where item_id=#{id}
</select>
<!-- public void deleteById(String id); -->
<select id="deleteById" parameterType="Integer">
delete from item_info where item_id=#{id}
</select>
<!-- public void save(ItemInfo item); -->
<select id="save" parameterType="ItemInfo">
insert into item_info values(#{item_id},#{item_name},#{item_type},#{item_price})
</select>
<!-- public List<ItemInfo> selectByVo(ItemInfoVo vo); -->
<select id="selectByVo" parameterType="ItemInfoVo" resultType="ItemInfo">
select * from item_info
<where>
<!-- 多条件查询 -->
<if test="itemInfo.item_id!=null and itemInfo.item_id!=''">
item_id=#{itemInfo.item_id}
</if>
<if test="itemInfo.item_name!=null and itemInfo.item_name!=''">
and item_name like "%"#{itemInfo.item_name}"%"
</if>
<if test="itemInfo.item_type!=null and itemInfo.item_type!=''">
and item_type=#{itemInfo.item_type}
</if>
<if test="itemInfo.item_price!=null and itemInfo.item_price!=''">
and item_price=#{itemInfo.item_price}
</if>
</where>
</select>
</mapper>
ItemController.java:
/**
*
*/
package com.sikiedu.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.sikiedu.bean.ItemInfo;
import com.sikiedu.bean.ItemInfoVo;
import com.sikiedu.service.ItemService;
/**
*
*
* @author Lijiang
*2019年5月3日
*/
@Controller
@RequestMapping("/item/")
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("allList.do")
public ModelAndView list(){
ModelAndView mav=new ModelAndView();
List<ItemInfo> itemList = itemService.selectAll();
mav.addObject("itemList", itemList);
mav.setViewName("item_list");
return mav;
}
// 参数绑定-默认参数
@RequestMapping("select.do")
public String select(HttpServletRequest request,HttpServletResponse response,HttpSession session ,Model model){
String idString=request.getParameter("id");
Integer id=Integer.valueOf(idString);
ItemInfo item= itemService.selectItemInfoById(id);
List<ItemInfo> itemList=new ArrayList<ItemInfo>();
itemList.add(item);
model.addAttribute("itemList", itemList);
return "item_list";
}
// 参数绑定-基本类型
@RequestMapping("delete.do")
public String delete(Integer id){
itemService.deleteById(id);//注意:这里的参数名“id”必须和item_list.jsp中的deleteItem(id)函数的参数名相同!!不然就不能自动映射了
//重定向到列表页
return "redirect:allList.do";
}
// 参数绑定-bean对象
@RequestMapping("save.do")
public String save(ItemInfo item){
itemService.save(item);
return "redirect:allList.do";
}
// 参数绑定-包装类
@RequestMapping("selectByVo.do")
public String selectByVo(ItemInfoVo vo,Model model){
System.out.println("itemInfoVo:"+vo.getItemInfo());
List<ItemInfo> itemList=itemService.selectByVo(vo);
model.addAttribute("itemList", itemList);
return "item_list";
}
// 接受前台的json字符串
// @RequestBody可以将前台传过来的json数据解析成ItemInfo
// @ResponseBody可以将后台的ItemInfo数据解析成json数据传到前台
@RequestMapping("jsonData.do")
@ResponseBody
public ItemInfo jsonData(@RequestBody ItemInfo item){
System.out.println("json Data:"+item);
return item;
}
//回显数据
@RequestMapping("editItem.do")
@ResponseBody
public ItemInfo editItem(Integer id){
ItemInfo item=itemService.selectItemInfoById(id);
return item;
}
// 使用@RequestBody来绑定list
@RequestMapping("getNameList.do")
public void getNameList(@RequestBody List<String> nameList){
System.out.println("nameList size:"+nameList.size());
for (String string : nameList) {
System.out.println("list name:"+string);
}
}
// 使用@RequestBody来绑定array
@RequestMapping("getNameArray.do")
public void getNameArray(@RequestBody String[] array){
System.out.println("nameList size:"+array.length);
for (String string : array) {
System.out.println("array name:"+string);
}
}
}
item_list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>游戏管理后台</title>
<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- bootstrap framework -->
<link href="${pageContext.request.contextPath }/css/bootstrap.min.css" rel="stylesheet" media="screen">
<!-- main stylesheet -->
<link href="${pageContext.request.contextPath }/css/main.min.css" rel="stylesheet" media="screen" id="mainCss">
<!-- elegant icons -->
<link href="${pageContext.request.contextPath }/css/style.css" rel="stylesheet" media="screen">
<!-- datepicker -->
<link href="${pageContext.request.contextPath }/css/datepicker3.css" rel="stylesheet" media="screen">
<!-- jBox -->
<link href="${pageContext.request.contextPath }/css/jbox.css" rel="stylesheet" media="screen">
<link href="${pageContext.request.contextPath }/css/noticeborder.css" rel="stylesheet" media="screen">
</head>
<body class="side_menu_active side_menu_expanded">
<div id="page_wrapper">
<!-- header -->
<header id="main_header">
<div class="container-fluid">
<!--logo-->
<div class="brand_section">
<a href="#"><img src="${pageContext.request.contextPath }/picture/logo01.png" alt="site_logo" width="108" height="40" style="margin-top: 5px"></a>
</div>
<div class="header_user_actions dropdown">
<div data-toggle="dropdown" class="dropdown-toggle user_dropdown">
<div class="user_avatar">
<img src="${pageContext.request.contextPath }/picture/head01.png" width="38" height="38">
</div>
<span class="caret"></span>
</div>
<ul class="dropdown-menu dropdown-menu-right">
<li><a href="#">个人中心</a></li>
<li><a href="#">注销</a></li>
</ul>
</div>
</div>
</header>
<!-- main content -->
<div id="main_wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<form action="${pageContext.request.contextPath }/item/select.do">
<input type="text" name="id">
<input type="submit" value="根据id查询">
</form>
<br>
<form action="${pageContext.request.contextPath }/item/selectByVo.do" method="post">
<input type="text" style="color: green;background-color: yellow;" placeholder="id" name="itemInfo.item_id">
<input type="text" style="color: green;background-color: yellow;" placeholder="名称" name="itemInfo.item_name">
<input type="text" style="color: green;background-color: yellow;" placeholder="类型" name="itemInfo.item_type">
<input type="text" style="color: green;background-color: yellow;" placeholder="原价" name="itemInfo.item_price">
<input type="submit" value="多条件查询">
</form>
<br>
<input type="button" value="ajax提交Json数据" onclick="jsonData()">
<input type="button" value="获取所有name list" onclick="getNameList()">
<input type="button" value="获取所有name array" onclick="getNameArray()">
<div class="row">
<div class="col-md-10">
<table class="table table-yuk2 toggle-arrow-tiny" id="footable_demo" data-filter="#textFilter" data-page-size="5">
<thead>
<tr>
<!--描述:商品数据标签-->
<th>ID</th>
<th>游戏名称</th>
<th>类型</th>
<th>原价</th>
</tr>
</thead>
<tbody>
<c:forEach items="${itemList }" var="item">
<tr>
<td>${item.item_id }</td>
<%-- <td>${item.item_name }</td> --%>
<td><input type="text" class="input_item_name" value="${item.item_name }"></td>
<td>${item.item_type }</td>
<td>${item.item_price }</td>
<td data-value="1">
<a herf="#" id="edit_btn" class="btn btn-xs btn-info" data-toggle="modal" data-target="#editLayer" onclick="editItem('${item.item_id}')">修改</a>
<%-- <a herf="#" id="del_btn" class="btn btn-xs btn-danger" onclick="deleteGoods('${item.item_id}')">删除</a> --%>
<a herf="#" id="del_btn" class="btn btn-xs btn-danger" onclick="deleteItem('${item.item_id}')">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
<tfoot class="hide-if-no-paging">
<tr>
<td colspan="5">
<ul class="pagination pagination-sm"></ul>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- edit layer -->
<div class="modal fade" id="editLayer">
<div class="modal-dialog modal-content">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">修改游戏信息</h4>
</div>
<div class="modal-body">
<!--游戏修改详情弹出层表单-->
<form class="form-horizontal" id="edit_item_form">
<!-- 游戏id隐藏域 -->
<input type="hidden" id="edit_item_id" name="item_id"/>
<!-- 游戏名称 -->
<div class="form-group">
<label for="edit_item_name" class="col-sm-2 control-label">游戏名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_item_name" placeholder="游戏名称" name="item_name">
</div>
</div>
<!-- 游戏类型 -->
<div class="form-group">
<label for="edit_item_type" class="col-sm-2 control-label">类型</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_item_type" placeholder="类型" name="item_type">
</div>
</div>
<!-- 游戏原价 -->
<div class="form-group">
<label for="edit_item_price" class="col-sm-2 control-label">原价</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="edit_item_price" placeholder="原价" name="item_price">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary btn-sm" onclick="updateItem()">确认修改</button>
</div>
</div>
</div>
</div>
<!-- add layer -->
<div class="modal fade" id="addLayer">
<div class="modal-dialog modal-content">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">新增游戏</h4>
</div>
<div class="modal-body">
<!--添加游戏弹出层表单-->
<form class="form-horizontal" id="add_item_form">
<!-- 游戏id隐藏域 -->
<input type="hidden" id="add_item_id" name="item_id"/>
<!-- 游戏名称 -->
<div class="form-group">
<label for="add_item_name" class="col-sm-2 control-label">游戏名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="add_item_name" placeholder="游戏名称" name="item_name">
</div>
</div>
<!-- 游戏类型 -->
<div class="form-group">
<label for="add_item_type" class="col-sm-2 control-label">类型</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="add_item_type" placeholder="类型" name="item_type">
</div>
</div>
<!-- 游戏原价 -->
<div class="form-group">
<label for="add_item_price" class="col-sm-2 control-label">原价</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="add_item_price" placeholder="原价" name="item_price">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary btn-sm" onclick="addItem()">确认修改</button>
</div>
</div>
</div>
</div>
<!-- main menu -->
<nav id="main_menu">
<div class="menu_wrapper">
<ul>
<li class="first_level">
<a href="javascript:void(0)">
<span class="icon_document_alt first_level_icon"></span>
<span class="menu-title">游戏管理</span>
</a>
<ul>
<li class="submenu-title">游戏管理</li>
<li><a href="#" data-toggle="modal" data-target="#addLayer">商品添加</a></li>
<li><a href="${pageContext.request.contextPath }/item/myitemlist.do">游戏列表</a></li>
</ul>
</li>
</ul>
</div>
<div class="menu_toggle">
<span class="icon_menu_toggle">
<i class="arrow_carrot-2left toggle_left"></i>
<i class="arrow_carrot-2right toggle_right" style="display:none"></i>
</span>
</div>
</nav>
</div>
<!-- jQuery -->
<script src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
<!-- jQuery Cookie -->
<script src="${pageContext.request.contextPath }/js/jquerycookie.min.js"></script>
<!-- Bootstrap Framework -->
<script src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
<!-- retina images -->
<script src="${pageContext.request.contextPath }/js/retina.min.js"></script>
<!-- switchery -->
<script src="${pageContext.request.contextPath }/js/switchery.min.js"></script>
<!-- typeahead -->
<script src="${pageContext.request.contextPath }/js/typeahead.bundle.min.js"></script>
<!-- fastclick -->
<script src="${pageContext.request.contextPath }/js/fastclick.min.js"></script>
<!-- match height -->
<script src="${pageContext.request.contextPath }/js/jquery.matchheight-min.js"></script>
<!-- scrollbar -->
<script src="${pageContext.request.contextPath }/js/jquery.mcustomscrollbar.concat.min.js"></script>
<!-- moment.js (date library) -->
<script src="${pageContext.request.contextPath }/js/moment-with-langs.min.js"></script>
<!-- Yukon Admin functions -->
<script src="${pageContext.request.contextPath }/js/yukon_all.min.js"></script>
<!-- page specific plugins -->
<!-- footable -->
<script src="${pageContext.request.contextPath }/js/footable.min.js"></script>
<script src="${pageContext.request.contextPath }/js/footable.paginate.min.js"></script>
<script src="${pageContext.request.contextPath }/js/footable.filter.min.js"></script>
<!-- datepicker -->
<script src="${pageContext.request.contextPath }/js/bootstrap-datepicker.js"></script>
<!-- jBox -->
<script src="${pageContext.request.contextPath }/js/jbox.min.js"></script>
<script type="text/javascript">
$(function() {
//footable
yukon_footable.goodslist();
//datepicker
yukon_datepicker.p_forms_extended();
})
//确认修改
function updateItem() {
$.post(
$("#edit_item_form").serialize(),
"${pageContext.request.contextPath }/item/update.do",
function(data){
alert("游戏信息更新成功!");
window.location.reload();
});
}
//确认删除
function deleteItem(id) {
if(confirm('确实要删除该游戏吗?')) {
$.post(
"${pageContext.request.contextPath }/item/delete.do",
{"id":id},
function(data){
window.location.reload();
});
}
}
//添加游戏
function addItem() {
$.post(
"${pageContext.request.contextPath }/item/save.do",
$("#add_item_form").serialize(),
function(data){
alert("游戏添加成功!");
window.location.reload();
});
}
// 使用ajax发送和接受json格式的字符串
function jsonData(){
var jsondata='{"item_id":10,"item_name":"超级玛丽","item_type":"横版过关","item_price":88}';
$.ajax({
type:"post",
url:"${pageContext.request.contextPath }/item/jsonData.do",
// 发送的数据格式
contentType:"application/json;charset=utf-8",
// 回调的格式
dataType:"json",
data:jsondata,
success:function(data){
alert(data);
alert(data.item_name);
}
})
}
//"修改弹窗"的回显
function editItem(id){
$.ajax({
type:"post",
url:"${pageContext.request.contextPath }/item/editItem.do",
data:{"id":id},
dataType:"json",
success:function(data){
$("#edit_item_name").val(data.item_name);
$("#edit_item_type").val(data.item_type);
$("#edit_item_price").val(data.item_price);
}
})
}
function getNameList(){
var nameList=new Array();
$(".input_item_name").each(function(){
nameList.push($(this).val());
});
// alert(nameList[3]);
// 发送到后台
$.ajax({
type:"post",
url:"${pageContext.request.contextPath }/item/getNameList.do",
// 发送的数据格式
contentType:"application/json;charset=utf-8",
data:JSON.stringify(nameList)
});
}
function getNameArray(){
var nameList=new Array();
$(".input_item_name").each(function(){
nameList.push($(this).val());
});
$.ajax({
type:"post",
url:"${pageContext.request.contextPath }/item/getNameArray.do",
// 发送的数据格式
contentType:"application/json;charset=utf-8",
data:JSON.stringify(nameList)
});
}
</script>
</body>
</html>
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<a href="item/allList.do">To item_list.jsp</a>
</body>
</html>
数据库: