MVC implements login, add, delete, modify, and query data display: jsp el expressions (2), mvcel
The data here is displayed using the jsp EL expression, which is put into the session in the background and obtained by the front-end EL.
The database design is like this. A teacher should have multiple students and set up a foreign key tid in the student of the student table to correspond to the tid in the teacher table teacher. After the teacher successfully logs on, displays the student information under the instructor.
Write from the back to the front;
1. Create the entity class student. java for the table corresponding to the database
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 Student object */public List <Student> getStudentBytid (int tid) based on the teacher id );}
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 {/*** obtain students based on the input teacher id */public List <Student> getStudentBytid (int tid) {// define the student object set students receiving database return List <S Tudent> students = new ArrayList <Student> (); // gets the database Connection con = JDBCUtil. getConnection (); // write the SQL statement StringBuffer SQL = new StringBuffer ("select * from student where tid =? "); Try {PreparedStatement pst = con. prepareStatement (SQL. toString (); pst. setInt (1, tid); // return a result set ResultSet rs1_pst.exe cuteQuery (); 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 {/*** query service * @ param tid use the instructor ID * @ return all Student object information */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, incluresp) throws ServletException, IOException {List <Student> students = new ArrayList <Student> (); searchService SC = new searchServiceImpl (); // obtain the teacher object Teacher teacher = (Teacher) req in the session. getSession (). getAttribute ("teacher"); // get the instructor ID int td = teacher. getTid (); // obtain students = SC Based on the instructor id. getStudents (td); // put the students object into the session. The foreground uses the EL expression to obtain the value HttpSession session = req. getSession (); session. setAttribute ("students", students); req. getRequestDispatcher ("main. jsp "). forward (req, resp );}}
7. Configure the web. xml file
Note: name must be in front
<! -- Query the current student information --> <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 front-end page
<Body> <form action = "selectStus"> <input type = "submit" value = "query"> </form> <! -- Display student object information --> <table class = "table-bordered" style = "margin-top: 50px "> <tr> <td> Student ID </td> <td> Student name </td> <td> Student age </td> </tr> <! -- Use the EL expression --> <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>