本次DAO模型之前展示过,本次不展示DAO
具体操作代码(未合并):
前端页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
编号 | 姓名 | 年龄 | 操作 |
---|---|---|---|
${item.id} | ${item.name} | ${item.age} | 删除||编辑 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
后台代码
删除页面:
package com._520it.pss.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com._520it.pss.dao.IStudentDAO;
import com._520it.pss.dao.impl.StudentDAOImpl;
@WebServlet("/student/delete")
public class delete extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private IStudentDAO dao;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
dao=new StudentDAOImpl();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String id=req.getParameter("id");
dao.delete(Long.valueOf(id));
resp.sendRedirect("/student/list");
}
}
edit页面(用于新增和编辑,传递id,根据id判断是新增还是编辑):
package com._520it.pss.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com._520it.pss.dao.IStudentDAO;
import com._520it.pss.dao.impl.StudentDAOImpl;
import com._520it.pss.domain.Student;
@WebServlet("/student/edit")
public class edit extends HttpServlet{
/**
*
*/
private IStudentDAO dao;
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
dao=new StudentDAOImpl();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
if(req.getParameter("id")!=null)
{
Student stu=dao.get(Long.valueOf(req.getParameter("id")));
System.out.println(stu);
req.setAttribute("stu", stu);
}
req.getRequestDispatcher("/WEB-INF/view/student/save.jsp").forward(req, resp);
}
}
increase页面(新增和编辑)
package com._520it.pss.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com._520it.pss.dao.IStudentDAO;
import com._520it.pss.dao.impl.StudentDAOImpl;
import com._520it.pss.domain.Student;
@WebServlet("/student/save")
public class increase extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private IStudentDAO dao;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
dao=new StudentDAOImpl();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String name=req.getParameter("user");
String id=req.getParameter("shixian");
System.out.println(id);
String age=req.getParameter("age");
Student stu=new Student(null,name,Integer.valueOf(age));
if(id!=null&&!"".equals(id))
{
dao.update(Long.valueOf(id), stu);
}else {
dao.save(stu);
}
resp.sendRedirect("/student/list");
}
}
student页面(用于传递值)
package com._520it.pss.web.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com._520it.pss.dao.IStudentDAO;
import com._520it.pss.dao.impl.StudentDAOImpl;
import com._520it.pss.domain.Student;
import com.mysql.fabric.xmlrpc.base.Data;
@WebServlet("/student/list")
public class student extends HttpServlet{
/**
*
*/
private IStudentDAO dao;
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
// TODO Auto-generated method stub
dao=new StudentDAOImpl();
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
List
req.setAttribute("list", list);
req.getRequestDispatcher("/WEB-INF/view/student/list.jsp").forward(req, resp);
}
}
效果如下
合并操作
package com._520it.pss.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com._520it.pss.dao.IStudentDAO;
import com._520it.pss.dao.impl.StudentDAOImpl;
import com._520it.pss.domain.Student;
@WebServlet("/student")
public class jihe extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private IStudentDAO dao;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
dao=new StudentDAOImpl();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String cmd=req.getParameter("cmd");
switch (cmd) {
case "edit":
edit(req, resp);
break;
case "delete":
delete(req, resp);
break;
case "saveORupdate":
saveORupdate(req, resp);
break;
case "list":
list(req, resp);
break;
default:
PrintWriter out=resp.getWriter();
out.println("请重新进行操作");
break;
}
}
protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
if(req.getParameter("id")!=null)
{
Student stu=dao.get(Long.valueOf(req.getParameter("id")));
System.out.println(stu);
req.setAttribute("stu", stu);
}
req.getRequestDispatcher("/WEB-INF/view/student/save.jsp").forward(req, resp);
}
protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String id=req.getParameter("id");
dao.delete(Long.valueOf(id));
resp.sendRedirect("/student/list");
}
protected void saveORupdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String name=req.getParameter("user");
String id=req.getParameter("shixian");
System.out.println(id);
String age=req.getParameter("age");
System.out.println(id+","+name+","+age);
Student stu=new Student(null,name,Integer.valueOf(age));
boolean a="".equals(id);
System.out.println(a);
if(id!=null&&!"".equals(id))
{
dao.update(Long.valueOf(id), stu);
}else {
System.out.println(stu);
dao.save(stu);
}
resp.sendRedirect("/student/list");
}
protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
List
req.setAttribute("list", list);
req.getRequestDispatcher("/WEB-INF/view/student/list.jsp").forward(req, resp);
}
}
同样可以实现上面的结果
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" %>
编号 | 姓名 | 年龄 | 操作 |
---|---|---|---|
${item.id} | ${item.name} | ${item.age} | 删除||编辑 |
save.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>