程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> MVC實現登錄,增刪改查之數據展示:JSP的EL表達式(二),mvcel

MVC實現登錄,增刪改查之數據展示:JSP的EL表達式(二),mvcel

編輯: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" >
        <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>

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved