之前了解过ajax,但是不是很深入,之前使用的ajax都是servert里边。最近学习ssm还没有使用到ajax,于是尝试写了一下ajax,在这里与小白一起分享一下。这是实现的是利用ajax提交表单。
1、准备工作
引入jquery、josn相关的jar包,配置springmvc(在这里你大概知道要做这些工作,接下来会一一的实现)
我将用到的jquery和josn的jar包放在网盘,有需要的根据链接进行下载。
链接:https://pan.baidu.com/s/1IB2e4DSOmZ-h6gcLjSP2NQ
提取码:o7e7
2、创建表和实体类
(1)数据库的表
(2)对应de实体类
package org.test.entity;
public class user {
private int id;
private String sex;
private String name;
private int age;
private String card;
private String addr;
public user() {
}
public user(int id, String sex, String name, int age, String card, String addr) {
this.id = id;
this.sex = sex;
this.name = name;
this.age = age;
this.card = card;
this.addr = addr;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
3、mapper
(1)mapper,xml
user.name,user.sex,user.age,user.card,user.addr
insert into user(name,sex,age,card,addr) values(#{name},#{sex},#{age},#{card},#{addr})
(2)对应的接口
package org.test.mapper;
import java.util.List;
import org.test.entity.user;
public interface userMapper {
List listuser();
void adduser(user u);
user listone(int id);
void updateone(user u);
void deleteid(int id);
List list(int id);
void insertuser(user us);//这是对应的insert
}
3、service
(1)接口
package org.test.service;
import java.util.List;
import org.test.entity.user;
public interface UserService {
public List listquery();
public void adduser(user u);
public user listone(int id);
public void updateone(user u);
public void deleteid(int id);
List list(int id);
public int insertuser(user us);//我们要使用的接口
}
(2)接口的实现
package org.test.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.test.entity.user;
import org.test.mapper.userMapper;
import org.test.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
public userMapper usermapper;
@Override
public List listquery() {
return usermapper.listuser();
}
@Override
public void adduser(user u) {
usermapper.adduser(u);
}
@Override
public user listone(int id) {
// TODO Auto-generated method stub
user userone=usermapper.listone(id);
return userone;
}
@Override
public void updateone(user u) {
// TODO Auto-generated method stub
usermapper.updateone(u);
}
@Override
public void deleteid(int id) {
// TODO Auto-generated method stub
usermapper.deleteid(id);
}
@Override
public List list(int id) {
// TODO Auto-generated method stub
List u=usermapper.list(id);
return u;
}
@Override
public int insertuser(user us) {
// TODO Auto-generated method stub
usermapper.insertuser(us);
return 1;
}
}
5、对应的contoolr
package org.test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.test.entity.user;
import org.test.service.UserService;
@Controller
@RequestMapping("UserController")
public class UserController {
@Autowired
public UserService uservice;
@ResponseBody
@RequestMapping("ajaxtest")
public String ajaxtest(user us) {
System.out.println("ajax");
int res=uservice.insertuser(us);
if(res==1) {
return "101";
}else {
return "100";
}
}
6、写jsp
<%@ 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 + "/";
%>
//一定引入对应的jquery
添加
这句一定需要写 //一定引入对应的jquery
写对应的js代码
注意:
还是开头啰嗦的,jquery一定引入,因为我们是使用jquery操作的ajax
一定配置springmvc(配置josn)
添加对应的jar包