菜单

MVC达成登入,增加和删除改查之数据彰显:JSP的EL表明式(二)

2019年8月15日 - Java

MVC达成登陆,增加和删除改查之数据呈现:JSP的EL表明式(二),mvcel

那边的数额呈现利用jsp的EL表明式,后台放入session,前台EL获取

数据库设计是那般的,贰个教育工笔者对应该多少个学生,在学员表student中国建工总公司立外键tid与老师表teacher的tid对应,未来少将登入成功现在,实现对教职工上边包车型客车学习者音讯举办展示

从后往前写;

1创建与数据库对应的表的实体类student.java

public class Student {
        private int sid;
        private  String  sname;
        private  String sage;
        public int getSid() {
            return sid;
        }
        public void setSid(int sid) {
            this.sid = sid;
        }
        public String getSname() {
            return sname;
        }
        public void setSname(String sname) {
            this.sname = sname;
        }
        public String getSage() {
            return sage;
        }
        public void setSage(String sage) {
            this.sage = sage;
        }
        public Student() {
            super();
        }
        public Student(int sid, String sname, String sage) {
            super();
            this.sid = sid;
            this.sname = sname;
            this.sage = sage;
        }

}

 2 StudentDao.java

public interface StudentDao {

    /**
     * 
     * @param tid
     * @return 学生对象
     * 根据老师id返回学生对象
     */
    public  List<Student> getStudentBytid(int tid);
}

 3 StudentDaoImpl.java

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import JDBCUtil.JDBCUtil;
import com.zr.dao.StudentDao;
import com.zr.model.Student;
public class StudentDaoImpl  implements StudentDao{
    /**
     * 根据传入的老师id获取学生
     */
    public List<Student> getStudentBytid(int tid) {
        //定义学生对象集合students接收数据库返回
        List<Student>  students =  new ArrayList<Student>();
        //获取数据库连接
        Connection con=JDBCUtil.getConnection();
        //编写SQL语句
        StringBuffer sql=new StringBuffer("select * from student where tid=?");
        try {
            PreparedStatement pst=con.prepareStatement(sql.toString());
            pst.setInt(1, tid);
            //返回一个结果集
            ResultSet rs=pst.executeQuery();
            while (rs.next()) {
                //学生对象接收结果集的结果
                Student s=new Student();
                s.setSid(rs.getInt("sid"));
                s.setSname(rs.getString("sname"));
                s.setSage(rs.getString("sage"));
                students.add(s);
            }
        } catch (SQLException e) {
            e.printStackTrace();    
        }
        return students;
    }

4 searchService.java

import java.util.List;
import com.zr.model.Student;
public interface searchService {
    /**
     * 查询服务
     * @param tid 通过老师ID
     * @return 学生对象所有信息
     */
public List<Student> getStudents(int  tid);
}

5 searchServiceImpl.java

import java.util.ArrayList;
import java.util.List;
import com.zr.dao.StudentDao;
import com.zr.daoIm.StudentDaoImpl;
import com.zr.model.Student;
import com.zr.service.searchService;
public class searchServiceImpl implements searchService{

    public List<Student> getStudents(int tid) {

        List<Student> students=new ArrayList<Student>();
        StudentDao studentDaoImpl =new StudentDaoImpl();
        students=    studentDaoImpl.getStudentBytid(tid);    
        return students;
    }

6 SearchController.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.zr.model.Student;
import com.zr.model.Teacher;
import com.zr.service.searchService;
import com.zr.serviceIm.searchServiceImpl;
public class SearchController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        List<Student> students=new  ArrayList<Student>();
        searchService sc=new searchServiceImpl();
        //获取存放在session中的teacher对象
        Teacher teacher=(Teacher) req.getSession().getAttribute("teacher");
        //获取老师ID
        int  td=   teacher.getTid();
        //根据老师id获取学生对象 
        students= sc.getStudents(td);

        //将students对象放入session中,前台用EL表达式获取值
        HttpSession session= req.getSession();
        session.setAttribute("students", students);
        req.getRequestDispatcher("main.jsp").forward(req, resp);
    }
    }

7 配置web.xml文件

注意:name要在前

<!-- 查询当前下面的学生信息 -->
          <servlet>
              <servlet-name>selectStus</servlet-name>
              <servlet-class>com.zr.controller.SearchController</servlet-class>
          </servlet>
          <servlet-mapping>
                  <servlet-name>selectStus</servlet-name>
                  <url-pattern>/selectStus</url-pattern>
          </servlet-mapping>

8 mian.jsp前台页面

<body>
    <form action="selectStus">
        <input type="submit" value="查询">
    </form>
    <!-- 展示学生对象信息 -->
    <table class="table table-bordered" style="margin-top: 50px">
        <tr>
            <td>学生编号</td>
            <td>学生名</td>
            <td>学生年龄</td>
        </tr>
        <!-- 利用EL表达式 -->
        <c:forEach items="${students}" var="student" varStatus="studentIndex">
            <tr>
                <td>${student.sid}</td>
                <td>${student.sname}</td>
                <td>${student.sage}</td>
            </tr>
        </c:forEach>
    </table>
</body>

 

 

http://www.bkjia.com/Javabc/1170298.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1170298.htmlTechArticleMVC实现登录,增删改查之数据展示:JSP的EL表达式(二),mvcel
这里的数目体现利用jsp的EL表明式,后台放入session,前台EL获取
数据库设计是那般的…

此处的多寡彰显利用jsp的EL表明式,后台放入session,前台EL获取

数据库设计是如此的,三个教员职员和工人对相应四个学生,在学生表student中确立外键tid与教师表teacher的tid对应,未来老师登陆成功之后,实现对名师下边包车型大巴学员消息进行显示

从后往前写;

1确立与数据库对应的表的实体类student.java

public class Student {
        private int sid;
        private  String  sname;
        private  String sage;
        public int getSid() {
            return sid;
        }
        public void setSid(int sid) {
            this.sid = sid;
        }
        public String getSname() {
            return sname;
        }
        public void setSname(String sname) {
            this.sname = sname;
        }
        public String getSage() {
            return sage;
        }
        public void setSage(String sage) {
            this.sage = sage;
        }
        public Student() {
            super();
        }
        public Student(int sid, String sname, String sage) {
            super();
            this.sid = sid;
            this.sname = sname;
            this.sage = sage;
        }

}

 2 StudentDao.java

public interface StudentDao {

    /**
     * 
     * @param tid
     * @return 学生对象
     * 根据老师id返回学生对象
     */
    public  List<Student> getStudentBytid(int tid);
}

 3 StudentDaoImpl.java

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import JDBCUtil.JDBCUtil;
import com.zr.dao.StudentDao;
import com.zr.model.Student;
public class StudentDaoImpl  implements StudentDao{
    /**
     * 根据传入的老师id获取学生
     */
    public List<Student> getStudentBytid(int tid) {
        //定义学生对象集合students接收数据库返回
        List<Student>  students =  new ArrayList<Student>();
        //获取数据库连接
        Connection con=JDBCUtil.getConnection();
        //编写SQL语句
        StringBuffer sql=new StringBuffer("select * from student where tid=?");
        try {
            PreparedStatement pst=con.prepareStatement(sql.toString());
            pst.setInt(1, tid);
            //返回一个结果集
            ResultSet rs=pst.executeQuery();
            while (rs.next()) {
                //学生对象接收结果集的结果
                Student s=new Student();
                s.setSid(rs.getInt("sid"));
                s.setSname(rs.getString("sname"));
                s.setSage(rs.getString("sage"));
                students.add(s);
            }
        } catch (SQLException e) {
            e.printStackTrace();    
        }
        return students;
    }

4 searchService.java

import java.util.List;
import com.zr.model.Student;
public interface searchService {
    /**
     * 查询服务
     * @param tid 通过老师ID
     * @return 学生对象所有信息
     */
public List<Student> getStudents(int  tid);
}

5 searchServiceImpl.java

import java.util.ArrayList;
import java.util.List;
import com.zr.dao.StudentDao;
import com.zr.daoIm.StudentDaoImpl;
import com.zr.model.Student;
import com.zr.service.searchService;
public class searchServiceImpl implements searchService{

    public List<Student> getStudents(int tid) {

        List<Student> students=new ArrayList<Student>();
        StudentDao studentDaoImpl =new StudentDaoImpl();
        students=    studentDaoImpl.getStudentBytid(tid);    
        return students;
    }

6 SearchController.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.zr.model.Student;
import com.zr.model.Teacher;
import com.zr.service.searchService;
import com.zr.serviceIm.searchServiceImpl;
public class SearchController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        List<Student> students=new  ArrayList<Student>();
        searchService sc=new searchServiceImpl();
        //获取存放在session中的teacher对象
        Teacher teacher=(Teacher) req.getSession().getAttribute("teacher");
        //获取老师ID
        int  td=   teacher.getTid();
        //根据老师id获取学生对象 
        students= sc.getStudents(td);

        //将students对象放入session中,前台用EL表达式获取值
        HttpSession session= req.getSession();
        session.setAttribute("students", students);
        req.getRequestDispatcher("main.jsp").forward(req, resp);
    }
    }

7 配置web.xml文件

注意:name要在前

<!-- 查询当前下面的学生信息 -->
          <servlet>
              <servlet-name>selectStus</servlet-name>
              <servlet-class>com.zr.controller.SearchController</servlet-class>
          </servlet>
          <servlet-mapping>
                  <servlet-name>selectStus</servlet-name>
                  <url-pattern>/selectStus</url-pattern>
          </servlet-mapping>

8 mian.jsp前台页面

<body>
    <form action="selectStus">
        <input type="submit" value="查询">
    </form>
    <!-- 展示学生对象信息 -->
    <table class="table table-bordered" style="margin-top: 50px">
        <tr>
            <td>学生编号</td>
            <td>学生名</td>
            <td>学生年龄</td>
        </tr>
        <!-- 利用EL表达式 -->
        <c:forEach items="${students}" var="student" varStatus="studentIndex">
            <tr>
                <td>${student.sid}</td>
                <td>${student.sname}</td>
                <td>${student.sage}</td>
            </tr>
        </c:forEach>
    </table>
</body>

 

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图