WEB-CRUD

本次DAO模型之前展示过,本次不展示DAO

WEB-CRUD

WEB-CRUD

 WEB-CRUD

具体操作代码(未合并):

WEB-CRUDWEB-CRUD

前端页面:

<%@ 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 http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>学生信息表</h1>

<a href="/student/edit">新增学生</a>

<table cellpadding="0" cellspacing="0" border="1" width="40%">

<tr style="background-color:#ADD8E6">

<th>编号</th>

<th>姓名</th>

<th>年龄</th>

<th>操作</th>

</tr>

<c:forEach items="${list}" var="item" varStatus="vs">

<tr style="background-color:${vs.count%2==0?'pink':''}">

<td>${item.id}</td>

<td>${item.name}</td>

<td>${item.age}</td>

<td><a href="/student/delete?id=${item.id}">删除</a>||<a href="/student/edit?id=${item.id}">编辑</a></td>

</tr>

</c:forEach>

</table>

</body>

</html>

 

 

 

<%@ 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 http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>${stu==null?"新增":"编辑"}学生</h1>

<form action="/student/save" method="post">

<input type="hidden" name="shixian" value='${stu.id}'/>

姓名:<input type="text" name="user" value='${stu.name}' /><br/>

年龄:<input type="text" name="age" value='${stu.age}'/><br/>

<input type="submit" name="login" value='${stu==null?"新增":"编辑"}' />

</form>

后台代码

删除页面:

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<Student> list=dao.list();

        req.setAttribute("list", list);

        req.getRequestDispatcher("/WEB-INF/view/student/list.jsp").forward(req, resp);

    }

}

 

效果如下

WEB-CRUD

WEB-CRUD

 

WEB-CRUD

WEB-CRUDWEB-CRUD

WEB-CRUD

WEB-CRUD

WEB-CRUD

WEB-CRUDWEB-CRUD

 

合并操作

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<Student> list=dao.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" %>

<!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>

    <h1>学生信息表</h1>

    <a href="/student?cmd=edit">新增学生</a>

    <table cellpadding="0" cellspacing="0" border="1" width="40%">

        <tr style="background-color:#ADD8E6">

            <th>编号</th>

            <th>姓名</th>

            <th>年龄</th>

            <th>操作</th>

        </tr>

        <c:forEach items="${list}" var="item" varStatus="vs">

        <tr style="background-color:${vs.count%2==0?'pink':''}">

            <td>${item.id}</td>

            <td>${item.name}</td>

            <td>${item.age}</td>

            <td><a href="/student?cmd=delete&id=${item.id}">删除</a>||<a href="/student?cmd=edit&id=${item.id}">编辑</a></td>

        </tr>

        </c:forEach>

    </table>

</body>

</html>

 

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"%>

<!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>

    <h1>${stu==null?"新增":"编辑"}学生</h1>

    <form action="/student?cmd=saveORupdate" method="post">

    <input type="hidden" name="shixian" value='${stu.id}'/>

        姓名:<input type="text" name="user" value='${stu.name}' /><br/>

        年龄:<input type="text" name="age" value='${stu.age}'/><br/>

        <input type="submit" name="login" value='${stu==null?"新增":"编辑"}' />

    </form>

</body>

</html>