此篇文章重点介绍使用jQuery来实现前台界面和后台数据的异步传输,因此,只给出action(controller层的代码)
界面很丑,请勿喷…
1.首先,给出我的项目包结构:
po包是实体类包,util是一个工具类(他将负责后台数据向前台界面的输出)mapper包映射包,内包含各种SQL语句以及增删改查等方法,biz(services)包,主要负责业务逻辑的编写,action包就是我们熟知的controller层.
StudentAction代码:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.action.IStudenAction;
import com.alibaba.fastjson.JSONObject;
import com.biz.IStudentBiz;
import com.po.Clazz;
import com.po.Student;
import com.util.StudentUtil;
@Controller
public class StudentAction implements IStudenAction {
@Autowired
private IStudentBiz studentBiz;
public IStudentBiz getStudentBiz() {
return studentBiz;
}
public void setStudentBiz(IStudentBiz studentBiz) {
this.studentBiz = studentBiz;
}
@RequestMapping(value="save_Student.do")
public String save(HttpServletRequest request,
HttpServletResponse response, Student st) {
int row = studentBiz.save(st);
if (row > 0) {
StudentUtil.printString(response, ""+1);
}else {
StudentUtil.printString(response, ""+0);
}
return null;
}
@RequestMapping(value="update_Student.do")
public String update(HttpServletRequest request,
HttpServletResponse response, Student st) {
int row = studentBiz.update(st);
if (row > 0) {
StudentUtil.printString(response, ""+1);
}else {
StudentUtil.printString(response, ""+0);
}
return null;
}
@RequestMapping(value="delById_Student.do")
public String delById(HttpServletRequest request,
HttpServletResponse response, Integer sid) {
int row = studentBiz.delById(sid);
if (row > 0) {
StudentUtil.printString(response, ""+1);
}else {
StudentUtil.printString(response, ""+0);
}
return null;
}
@RequestMapping(value="findById_Student.do")
public String findById(HttpServletRequest request,
HttpServletResponse response, Integer sid) {
Student st = studentBiz.findById(sid);
//将学生对象转为json字符串
String jsonstr = JSONObject.toJSONString(st);
System.out.println(jsonstr);
StudentUtil.printString(response, jsonstr);
return null;
}
@RequestMapping(value="findAll_Student.do")
public String findAll(HttpServletRequest request,
HttpServletResponse response) {
List lsst = studentBiz.findAll();
String jsonstr = JSONObject.toJSONString(lsst);
StudentUtil.printString(response, jsonstr);
return null;
}
@RequestMapping(value="doinit_Student.do")
public String doinit(HttpServletRequest request,
HttpServletResponse response) {
List lsca = studentBiz.doinit();
String jsonstr = JSONObject.toJSONString(lsca);
StudentUtil.printString(response, jsonstr);
return null;
}
@RequestMapping(value="findPageAll_Student.do")
public String findPageAll(HttpServletRequest request,
HttpServletResponse response, Integer page, Integer rows) {
page = page==null?1:page;
rows=rows==null?5:rows;
if (rows > 20) {
rows = 20;//每页最多20个值
}
//此处在biz(service)层查出总行数,与传进来的rows做运算得出最大页数
int maxPage=studentBiz.findMaxPage(rows);
if (page > maxPage) {
page = maxPage;
}
List lsst = studentBiz.findPageAll(page, rows);
Map map = new HashMap();
map.put("page", page);
map.put("rows", rows);
map.put("lsst", lsst);
map.put("maxpage", maxPage);
String jsonstr = JSONObject.toJSONString(map);
StudentUtil.printString(response, jsonstr);
return null;
}
}
util包中的StudentUtil代码:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class StudentUtil {
/**
* 输出字符串
*/
public static void printString(HttpServletResponse response,String str){
response.setCharacterEncoding("utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
out.print(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
out.flush();
out.close();
}
}
}
前台界面代码student.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
学生列表
每页
条记录
跳转到第
页
1/1页
需要注意的是,学生日期这有一个辅助显示日期的属性需要处理!请自行解决!