jsp大作业 作业要求
mysql + CRUD 
校验器 
Struts2+Hibernate 框架 
css美化 
 
文件结构 
运行截图 登陆界面 
选择操作界面 
增加和修改界面 使用了js语言,实现了一个form中有两个按钮,指向不同的action 

校验器 应用对加入学生信息进行校验。
ID必须为0或者不为0开头的数字。
name和address不能为空。

成功添加后自动转到学生列表(改变也是一样) 
查询和删除 因为用户可能不确定要删除哪一个,所以删除的旁边还有一个查询按钮。根据ID来进行查询。


代码 接口代码 
1 2 3 4 5 6 7 8 9 10 11 package  njtech.edu.DAO;import  java.util.List;import  org.hibernate.Session;public  interface  BaseDAO      public  Session getSession ()      public  void  closeSession ()      public  List search (String hql)  } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package  njtech.edu.DAO;import  java.util.List;import  njtech.edu.model.Student;public  interface  StudentDAO      void  saveStudent (Student student)      List<Student> getAll ()  ;     void  changeStudent (Student student)      void  deleteStudent (Student student)      List<Student> queryStudent (Student student)  ; } 
1 2 3 4 5 6 7 8 9 10 11 package  njtech.edu.DAO;import  njtech.edu.model.User;import  java.util.List;public  interface  UserDAO      User queryUser (String username)  ; } 
实现类代码 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package  njtech.edu.DAO.impl;import  java.util.List;import  com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory;import  njtech.edu.DAO.BaseDAO;import  njtech.edu.DAO.StudentDAO;import  org.hibernate.Query;import  org.hibernate.Session;import  org.hibernate.SessionFactory;import  org.hibernate.Transaction;import  org.hibernate.cfg.Configuration;public  class  BaseDAOImpl  implements  BaseDAO      private  SessionFactory sessionFactory;     private  Session session;     private  Transaction tx;     public  void  init ()          session = HibernateSessionFactory.getSession();         tx = session.beginTransaction();     }     @Override      public  Session getSession ()           init();         return  session;     }     @Override      public  void  closeSession ()           session.close();     }     @Override      public  List search (String hql)           Session session = null ;         session = getSession();         List alist = null ;         alist = session.createQuery(hql).list();         session.close();         return  alist;     } } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 package  njtech.edu.DAO.impl;import  njtech.edu.model.Student;import  njtech.edu.DAO.StudentDAO;import  org.hibernate.*;import  org.hibernate.cfg.Configuration;import  org.hibernate.criterion.Restrictions;import  java.util.List;public  class  StudentDAOImpl  extends  BaseDAOImpl  implements  StudentDAO     SessionFactory sessionFactory = null ;     Session session = null ;     Transaction tx = null ;     public  StudentDAOImpl ()      public  void  init ()           sessionFactory = new  Configuration().                 configure("hibernate.cfg.xml" ).                 buildSessionFactory();         session = sessionFactory.openSession();         tx = session.beginTransaction();     }     @Override      public  void  saveStudent (Student student)           init();         session.save(student);         String sql = "insert into student (id,name,address) values('"  +                     Integer.toString(student.getId()) +".'" +                     student.getName()+"','" +                     student.getAddress()+"')"                  ;         tx.commit();         session.close();     }     @Override      public  List<Student> getAll ()           init();         List<Student> students = session.createQuery("from Student" ).list();                  return  students;     }     @Override      public  void  changeStudent (Student student)          init();         Student student1 = (Student) session.get(Student.class, new  Integer(student.getId()));                                                      student1.setName(student.getName());         student1.setAddress(student.getAddress());         session.update(student1);         tx.commit();         session.close();     }     @Override      public  void  deleteStudent (Student student)          init();         Student student1 = (Student) session.get(Student.class, student.getId());                                                      if (student  != null ){             session.delete(student1);         }         tx.commit();         session.close();     }     @Override      public  List<Student> queryStudent (Student student)          init();         Criteria criteria                 = session.createCriteria(Student.class).add(Restrictions.like("id" ,student.getId()));                  Query query = session.createQuery("from Student where id=" +student.getId());                                    List<Student> students = criteria.list();                 return  students;     } } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package  njtech.edu.DAO.impl;import  njtech.edu.DAO.UserDAO;import  njtech.edu.model.Student;import  njtech.edu.model.User;import  org.hibernate.*;import  org.hibernate.cfg.Configuration;import  org.hibernate.criterion.Restrictions;import  java.util.List;public  class  UserDAOImpl  extends  BaseDAOImpl  implements  UserDAO      SessionFactory sessionFactory = null ;     Session session = null ;     Transaction tx = null ;     public  UserDAOImpl ()      public  void  init ()           sessionFactory = new  Configuration().                 configure("hibernate.cfg.xml" ).                 buildSessionFactory();         session = sessionFactory.openSession();         tx = session.beginTransaction();     }     @Override      public  User queryUser (String username)           init();         Criteria criteria                 = session.createCriteria(User.class).add(Restrictions.like("username" ,username));         User user = (User)criteria.uniqueResult();         return  user;     } } 
action类代码 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 package  njtech.edu.Action;import  java.util.ArrayList;import  java.util.List;import  java.util.regex.Pattern;import  com.opensymphony.xwork2.ActionSupport;import  com.opensymphony.xwork2.ModelDriven;import  njtech.edu.DAO.StudentDAO;import  njtech.edu.DAO.impl.StudentDAOImpl;import  njtech.edu.model.Student;public  class  StudentAction  extends  ActionSupport  implements  ModelDriven      private  Student student;     private  List<Student> students = new  ArrayList<Student>();     private  StudentDAO dao = new  StudentDAOImpl();     public  Object getModel ()           return  student;     }     public  String saveStudent ()       {        dao.saveStudent(student);         return  "success" ;     }     public  String listStudents ()       {        students = dao.getAll();         return  "success" ;     }     public  String queryStudents ()          students = dao.queryStudent(student);         return  "success" ;     }     public  String changeStudent ()       {        dao.changeStudent(student);         return  "success" ;     }     public  String deleteStudent ()          dao.deleteStudent(student);         return  "success" ;     }     public  void  validate ()          System.out.println("校验器!" );     }     public  void  validateSaveStudent ()          if (student != null ){             Integer ID = student.getId();             String IID = ID.toString();             if ("" .equals(this .student.getName().trim())){                 this .addFieldError("username" , "用户名不能为空" );             }             if ("" .equals(this .student.getAddress().trim())){                 this .addFieldError("address" , "地址不能为空" );             }             if (!Pattern.compile("^(0|[1-9][0-9]*)$" ).matcher(IID).matches()){                 this .addFieldError("id" , "ID格式不正确" );             }         }         super .validate();     }     public  Student getStudent ()           return  student;     }     public  void  setStudent (Student student)           this .student = student;     }     public  List<Student> getStudents ()           return  students;     }     public  void  setStudents (List<Student> students)           this .students = students;     } } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package  njtech.edu.Action;import  com.opensymphony.xwork2.ActionSupport;import  com.opensymphony.xwork2.ModelDriven;import  njtech.edu.DAO.impl.UserDAOImpl;import  njtech.edu.model.User;public  class  UserLoginAction  extends  ActionSupport      private  User user;     private  UserDAOImpl dao= new  UserDAOImpl();     public  String userLogin ()          User tempuser = dao.queryUser(user.getUsername());         if (tempuser.equals(user)){             return  "success" ;         }else {             return  "error" ;         }     }     public  User getUser ()           return  user;     }     public  void  setUser (User user)           this .user = user;     } } 
Hibernate配置文件 cfg.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration  PUBLIC          "-//Hibernate/Hibernate Configuration DTD//EN"          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > <hibernate-configuration >     <session-factory >          <property  name ="connection.url" > jdbc:mysql://localhost:3306/test?useSSL=false</property >          <property  name ="connection.driver_class" > com.mysql.jdbc.Driver</property >          <property  name ="connection.username" > root</property >          <property  name ="connection.password" > 123456</property >          <property  name ="current_session_context_class" > thread</property >          <property  name ="show_sql" > true</property >          <property  name ="hbm2ddl.auto" > update</property >          <property  name ="dialect" > org.hibernate.dialect.MySQLDialect</property >          <mapping  class ="njtech.edu.model.Student" />          <mapping  resource ="njtech/edu/model/Student.hbm.xml" />          <mapping  resource ="njtech/edu/model/User.hbm.xml" />      </session-factory >  </hibernate-configuration > 
hbm user和student 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version='1.0'  encoding='utf-8' ?> <!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"      "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping>     <class  name "njtech.edu.model.Student"  table="student"  schema="test" >         <id name="id" >             <column name="id"  sql-type="int(11)" />         </id>         <property name="name" >             <column name="name"  sql-type="varchar(100)"  length="100"  not-null ="true" />         </property>         <property name="address" >             <column name="address"  sql-type="varchar(100)"  length="100"  not-null ="true" />         </property>     </class> </hibernate-mapping> 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?xml version='1.0'  encoding='utf-8' ?> <!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"      "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping>     <class  name "njtech.edu.model.User"  table="user"  schema="test" >         <id name="username" >             <column name="username"  sql-type="varchar(20)"  length="20" />         </id>         <property name="password" >             <column name="password"  sql-type="varchar(20)"  length="20" />         </property>     </class> </hibernate-mapping> 
model类 Student User 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 package  njtech.edu.model;import  javax.persistence.*;import  java.util.Objects;public  class  Student      private  int  id;     private  String name;     private  String address;     public  Student ()      public  Student (int  id, String name, String address)          this .id = id;         this .name = name;         this .address = address;     }     public  int  getId ()           return  id;     }     public  void  setId (int  id)           this .id = id;     }     public  String getName ()           return  name;     }     public  void  setName (String name)           this .name = name;     }     public  String getAddress ()           return  address;     }     public  void  setAddress (String address)           this .address = address;     }     @Override      public  boolean  equals (Object o)           if  (this  == o) return  true ;         if  (o == null  || getClass() != o.getClass()) return  false ;         Student that = (Student) o;         return  id == that.id &&                 Objects.equals(name, that.name) &&                 Objects.equals(address, that.address);     }     @Override      public  int  hashCode ()           return  Objects.hash(id, name, address);     } } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 package  njtech.edu.model;import  java.util.Objects;public  class  User      private  String username;     private  String password;     public  User ()      public  String getUsername ()           return  username;     }     public  void  setUsername (String username)           this .username = username;     }     public  String getPassword ()           return  password;     }     public  void  setPassword (String password)           this .password = password;     }     @Override      public  boolean  equals (Object o)           if  (this  == o) return  true ;         if  (o == null  || getClass() != o.getClass()) return  false ;         User user = (User) o;         return  Objects.equals(username, user.username) &&                 Objects.equals(password, user.password);     }     @Override      public  int  hashCode ()           return  Objects.hash(username, password);     } } 
Struts2配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 <?xml version="1.0"  encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC         "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"          "http://struts.apache.org/dtds/struts-2.3.dtd" > <struts>     <package  name="default"  extends="struts-default" >         <action name="addStudent"  method="saveStudent"                  class "njtech.edu.Action.StudentAction" >             <result name="success"  type="redirect" >                 listStudents.action             </result>             <result name="input">add.jsp</result>         </action>         <action name="changeStudent"  method="changeStudent"                  class "njtech.edu.Action.StudentAction" >             <result name="success"  type="redirect" >                 listStudents.action             </result>         </action>         <action name="listStudents"  method="listStudents"                  class "njtech.edu.Action.StudentAction" >             <result name="success" >                 index.jsp             </result>         </action>         <action name="queryStudents"  method="queryStudents"                  class "njtech.edu.Action.StudentAction" >             <result name="success"  >                 queryStudent.jsp             </result>         </action>         <action name="deleteStudent"  method="deleteStudent"                  class "njtech.edu.Action.StudentAction" >             <result name="success" >                 success.jsp             </result>         </action>         <action name="userLogin"  method="userLogin"                  class "njtech.edu.Action.UserLoginAction" >             <result name="success"> index.jsp</result>             <result name="error">login.jsp</result>         </action>     </package> </struts> 
前端代码 css 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 body {     background : url ("../image/2.jpg" ) no-repeat;     background-size :100% ;     background-attachment : fixed; } form {    display :inline-block;     margin-top :100px ; } h1 {     color : #ffb11c ;     text-align :center; } p {     font-family :"Times New Roman" ;     font-size :20px ; } table {     border-collapse :collapse; } table ,th , td {     border : 0px  solid black;     border-bottom-color : aqua; } form {     background :rgba (255 ,255 ,255 ,0.3 );     border : 1px  solid rgba (0 ,0 ,0 , 0.2 );     font-family :"Monaco" ;     font-size : 40px ;     border-radius : 30px ; } .input_control {    width :360px ;     margin :14px  auto; } .error_message {    color : #ff383f ;     font-size : 20px ; } input [type="text" ] {    box-sizing : border-box;     text-align :center;     font-size :1.0em ;     height :2.0em ;     border-radius :20px ;     border :1px  solid #c8cccf ;     color :#6a6f77 ;     -web-kit-appearance :none;     -moz-appearance : none;     display :block;     outline :0 ;     padding :0  1em ;     text-decoration :none;     width :100% ; } input [type="password" ] {    box-sizing : border-box;     text-align :center;     font-size :1.0em ;     height :2.0em ;     border-radius :20px ;     border :1px  solid #c8cccf ;     color :#6a6f77 ;     -web-kit-appearance :none;     -moz-appearance : none;     display :block;     outline :0 ;     padding :0  1em ;     text-decoration :none;     width :100% ; } input [type="text" ] :focus {    border :1px  solid #ff7496 ; } .button {    background-color : #af8c99 ;      border : none;     color : white;     padding : 17px  28px ;     text-align : center;     text-decoration : none;     display : inline-block;     font-size : 14px ;     border-radius :4px ; } 
JSP 第一个页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <%--   Created by IntelliJ IDEA.   User: cheasim   Date: 2018 /11 /11    Time: 11 :57  AM   To change this  template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8"  language="java"  %> <%@taglib  prefix="s"  uri="/struts-tags" %> <html> <head>     <title>登陆界面</title>     <link type="text/css"  rel="stylesheet"  href="css/mystyle.css" />     <style>         <!--         form{ height:350px; width:400px; border:1px solid #666666;}         -->     </style>     <script type="text/javascript" >         function login ()  {             document.myform.action='http://localhost:8080/userLogin.action' ;             document.myform.submit();         }     </script>     <style type="text/css" >         .button{             background-color: #af8c99; /* Green */             border: none;             color: white;             padding: 14 px 28 px;             text-align: center;             text-decoration: none;             display: inline-block;             font-size: 20 px;             border-radius:13 px;         }     </style> </head> <body> <center> <h1> 管理员登陆界面</h1> <form action="userLogin"  name="myform" >     <s:textfield name="user.username"  label="用户名" />     <s:textfield name="user.password"  label="密码"  type="password" />     <input type="button"  class "button"  value="登陆"  onclick="login();"  align="right" /> </form> </center> </body> </html> 
选择页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 <%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s"  uri="/struts-tags" %> <html> <head>   <title>学生登录系统</title>   <link type="text/css"  rel="stylesheet"  href="css/mystyle.css"  />     <style type="text/css" >         .button{             background-color: #af8c99; /* Green */             border: none;             color: white;             padding: 15 px 30 px;             text-align: center;             text-decoration: none;             display: inline-block;             font-size: 24 px;             border-radius:13 px;         }     </style>     <script type="text/javascript" >         function goAdd ()  {             document.myform.action='add.jsp' ;             document.myform.submit();         }         function goDelete ()  {             document.myform.action='queryStudent.jsp' ;             document.myform.submit();         }     </script> </head> <body> <center> <h1>学生系统学生一览</h1>     <form name="myform" >         <table>             <tr>                 <input type="button"  class "button"  value="增加和修改"  onclick="goAdd();" />             </tr>             <tr>                 <input type="button"  class "button"  value="删除和查询"  onclick="goDelete();" />             </tr>         </table>     </form> <s:if  test="students.size() > 0" >   <table border="1px" cellpadding="8px" bgcolor=#faebd7 style="filter:alpha(opacity=50);">     <tr>       <th>Student Id</th>       <th>Name</th>       <th>Address</th>     </tr>     <s:iterator value="students" >       <tr>         <td><s:property value="id" /></td>         <td><s:property value="name" /></td>         <td><s:property value="address" /></td></td>       </tr>     </s:iterator>   </table> </s:if> </center> </body> </html> 
增加和修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 <%--   Created by IntelliJ IDEA.   User: cheasim   Date: 2018 /11 /28    Time: 3 :39  PM   To change this  template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8"  language="java"  %> <%@ taglib prefix="s"  uri="/struts-tags" %> <html> <head>     <title>增加学生</title>     <link type="text/css"  rel="stylesheet"  href="css/mystyle.css"  />     <script type="text/javascript" >         function changeStudent ()  {             document.myform.action='http://localhost:8080/changeStudent.action' ;             document.myform.submit();         }         function saveStudent ()  {             document.myform.action='http://localhost:8080/addStudent.action' ;             document.myform.submit();         }     </script> </head> <body> <center>     <form action="addStudent"  name="myform" >         <h4> 加入学生信息</h4>         <s:textfield name="student.id"  label="ID" />         <s:fielderror fieldName="id"  cssClass="error_message" />         <s:textfield name="student.name"  label="Name" />         <s:fielderror fieldName="username"  cssClass="error_message" />         <s:textfield name="student.address"  label="Address" />         <s:fielderror fieldName="address"  cssClass="error_message" />         <input type="button"  class "button"  value="改变"  onclick="changeStudent();" />         <input type="button"  class "button"  value="增加"  onclick="saveStudent();" />     </form> </center> </body> </html> 
查询和删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 <%--   Created by IntelliJ IDEA.   User: cheasim   Date: 2018 /11 /14    Time: 2 :43  PM   To change this  template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8"  language="java"  %> <%@ taglib prefix="s"  uri="/struts-tags" %> <html> <head>     <title>查询结果</title>     <link type="text/css"  rel="stylesheet"  href="css/mystyle.css"  />     <script type="text/javascript" >         function deleteStudent ()  {             document.myform1.action='http://localhost:8080/deleteStudent.action' ;             document.myform1.submit();         }         function queryStudent ()  {             document.myform1.action='http://localhost:8080/queryStudents.action' ;             document.myform1.submit();         }     </script> </head> <body> <center>     <form action="queryStudents"  name="myform1" >         <h4> 查询学生信息             依据id搜索或者删除</h4>         <s:textfield name="student.id"  label="ID" />         <input type="button"  class "button" value="删除"  onclick="deleteStudent();" />         <input type="button" class "button"  value="查询"  onclick="queryStudent();" />     </form> <h1>学生查询结果</h1> <s:if  test="students.size() > 0" >     <table border="1px" cellpadding="8px" bgcolor=#faebd7 style="filter:alpha(opacity=50);">         <tr>             <th>Student Id</th>             <th>Name</th>             <th>Address</th>         </tr>         <s:iterator value="students" >             <tr>                 <td><s:property value="id" /></td>                 <td><s:property value="name" /></td>                 <td><s:property value="address" /></td></td>             </tr>         </s:iterator>     </table> </s:if> </center> </body> </html>