zoukankan      html  css  js  c++  java
  • SSH框架整合-myeclipse

    项目结构

     

    1.mysql数据库 stuinfo

     1 /*
     2 SQLyog 企业版 - MySQL GUI v8.14 
     3 MySQL - 5.5.40 : Database - stuinfo
     4 *********************************************************************
     5 */
     6 
     7 CREATE DATABASE /*!32312 IF NOT EXISTS*/`stuinfo` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */;
     8 
     9 USE `stuinfo`;
    10 
    11 /*Table structure for table `classes` */
    12 
    13 DROP TABLE IF EXISTS `classes`;
    14 
    15 CREATE TABLE `classes` (
    16   `id` int(12) NOT NULL AUTO_INCREMENT,
    17   `name` varchar(11) COLLATE utf8_bin NOT NULL,
    18   PRIMARY KEY (`id`)
    19 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
    20 
    21 /*Data for the table `classes` */
    22 
    23 insert  into `classes`(`id`,`name`) values (1,'y2134'),(2,'s2188'),(3,'s2192');
    24 
    25 /*Table structure for table `student` */
    26 
    27 DROP TABLE IF EXISTS `student`;
    28 
    29 CREATE TABLE `student` (
    30   `id` int(12) NOT NULL AUTO_INCREMENT COMMENT '学员编号',
    31   `name` varchar(11) COLLATE utf8_bin NOT NULL COMMENT '姓名',
    32   `age` int(12) NOT NULL COMMENT '年龄',
    33   `gender` char(2) COLLATE utf8_bin NOT NULL COMMENT '性别',
    34   `telephone` varchar(11) COLLATE utf8_bin NOT NULL COMMENT '电话',
    35   `email` varchar(32) COLLATE utf8_bin NOT NULL COMMENT 'Email',
    36   `classId` int(12) NOT NULL COMMENT '班级编号',
    37   PRIMARY KEY (`id`),
    38   KEY `FK_student` (`classId`),
    39   CONSTRAINT `FK_student` FOREIGN KEY (`classId`) REFERENCES `classes` (`id`)
    40 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
    41 
    42 /*Data for the table `student` */
    43 
    44 insert  into `student`(`id`,`name`,`age`,`gender`,`telephone`,`email`,`classId`) values (1,'何东东',20,'男','13910101000','hdd@accp.co',1),(2,'付好',23,'男','18856906326','fuhao@126.com',2),(3,'李建达',24,'男','18888888888','lijianda@qq.com',2),(4,'lucy',20,'女','15852033216','lucy@qq.com',3),(5,'李健',25,'男','13956063365','liyang1@126.com',2),(6,'李健康',20,'男','13956063369','lijiankang@126.com',1);
    stuinfo

    2. 数据库配置文件

    1 driverClass=com.mysql.jdbc.Driver
    2 jdbcUrl=jdbc:mysql://localhost:3306/stuinfo?characterEncoding=utf8
    3 user=root
    4 password=root
    View Code

    3. spring配置文件applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
     5 
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     7     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     8     http://www.springframework.org/schema/tx 
     9     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    10     http://www.springframework.org/schema/context 
    11     http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
    12     <!-- 1.add spring capabilitise -->
    13     <!-- 注解扫描包 -->
    14     <context:component-scan base-package="cn.stu" />
    15     <!-- 读取数据库配置文件 -->
    16     <context:property-placeholder location="classpath:jdbc.properties" />
    17     <!-- 打开事务注解 -->
    18     <tx:annotation-driven />
    19     <!-- 数据源配置 -->
    20     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    21         <property name="driverClass" value="${driverClass}" />
    22         <property name="jdbcUrl" value="${jdbcUrl}" />
    23         <property name="user" value="${user}" />
    24         <property name="password" value="${password}" />
    25     </bean>
    26 
    27     <!-- sessionFactory配置 -->
    28     <bean id="sessionFactory"
    29         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    30         <property name="dataSource" ref="dataSource"></property>
    31         <property name="hibernateProperties">
    32             <props>
    33                 <!-- hibernate方言配置 -->
    34                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    35                 <!-- 配置hibernate 控制台打印查询sql -->
    36                 <prop key="hibernate.show_sql">true</prop>
    37                 <prop key="hibernate.format_sql">true</prop>
    38             </props>
    39         </property>
    40         <property name="mappingLocations" value="classpath:cn/stu/entity/*.hbm.xml"></property>
    41     </bean>
    42     <!-- hibernate方言配置 -->
    43     <bean id="transactionManager"
    44         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    45         <property name="sessionFactory" ref="sessionFactory"></property>
    46     </bean>
    47     <!-- hibernateTemplate bean -->
    48     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    49         <property name="sessionFactory" ref="sessionFactory"></property>
    50     </bean>
    51 </beans>
    applictaionContext.xml

    4.hibernate配置文件hibernate.cfg.xml(反向工具自动生成)

     1 <?xml version='1.0' encoding='UTF-8'?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 
     6 <!-- Generated by MyEclipse Hibernate Tools.                   -->
     7 <hibernate-configuration>
     8 
     9     <session-factory>
    10         <property name="dialect">
    11             org.hibernate.dialect.MySQLDialect
    12         </property>
    13         <property name="connection.url">
    14             jdbc:mysql://localhost:3306/stuinfo
    15         </property>
    16         <property name="connection.username">root</property>
    17         <property name="connection.password">root</property>
    18         <property name="connection.driver_class">
    19             com.mysql.jdbc.Driver
    20         </property>
    21         <property name="myeclipse.connection.profile">mysql</property>
    22         <mapping resource="cn/stu/entity/Student.hbm.xml" />
    23         <mapping resource="cn/stu/entity/Classes.hbm.xml" />
    24 
    25     </session-factory>
    26 
    27 </hibernate-configuration>
    hibernate.cfg.xml

    5. strus配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2  <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 <struts>
     6     <package name="default" namespace="/" extends="struts-default">
     7         <action name="stuListPage" class="cn.stu.aciton.StuAction" method="stuListPage">
     8             <!--result不写name,默认name值是success  -->
     9             <result>/index.jsp</result>
    10         </action>
    11         
    12         <action name="stuAddPage" class="cn.stu.aciton.StuAction" method="stuAddPage">
    13             <result>/addPage.jsp</result>
    14         </action>
    15         
    16         <action name="addStu" class="cn.stu.aciton.StuAction" method="addStu">
    17             <result name="success" type="redirectAction">stuListPage</result> <!-- 保存成功,重定向到显示页面 -->
    18             <result name="failed" >/addPage.jsp</result>
    19         </action>
    20         
    21         <action name="deleteClasses" class="cn.stu.aciton.StuAction" method="delStu">
    22             <result name="success" type="redirectAction">stuListPage</result>
    23         </action> 
    24     </package>
    25     
    26 </struts>
    struts.xml

     6. JavaBean和 hibernate sql映射文件(反向工具自动生成,手动写的话,只需写Classes Student两个类就行了)

     1 package cn.stu.entity;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 /**
     7  * AbstractClasses entity provides the base persistence definition of the
     8  * Classes entity. @author MyEclipse Persistence Tools
     9  */
    10 
    11 public abstract class AbstractClasses implements java.io.Serializable {
    12 
    13     // Fields
    14 
    15     private Integer id;
    16     private String name;
    17     private Set students = new HashSet(0);
    18 
    19     public AbstractClasses() {
    20     }
    21 
    22     public AbstractClasses(String name) {
    23         this.name = name;
    24     }
    25 
    26     public AbstractClasses(String name, Set students) {
    27         this.name = name;
    28         this.students = students;
    29     }
    30 
    31     // Property accessors
    32 
    33     public Integer getId() {
    34         return this.id;
    35     }
    36 
    37     public void setId(Integer id) {
    38         this.id = id;
    39     }
    40 
    41     public String getName() {
    42         return this.name;
    43     }
    44 
    45     public void setName(String name) {
    46         this.name = name;
    47     }
    48 
    49     public Set getStudents() {
    50         return this.students;
    51     }
    52 
    53     public void setStudents(Set students) {
    54         this.students = students;
    55     }
    56 
    57 //    @Override
    58 //    public String toString() {
    59 //        return "AbstractClasses [id=" + id + ", name=" + name + ", students="
    60 //                + students + "]";
    61 //    }
    62 
    63     
    64 }
    AbstractClasses
     1 package cn.stu.entity;
     2 
     3 /**
     4  * AbstractStudent entity provides the base persistence definition of the
     5  * Student entity. @author MyEclipse Persistence Tools
     6  */
     7 
     8 public abstract class AbstractStudent implements java.io.Serializable {
     9 
    10     // Fields
    11 
    12     private Integer id;
    13     private Classes classes;
    14     private String name;
    15     private Integer age;
    16     private String gender;
    17     private String telephone;
    18     private String email;
    19 
    20     // Constructors
    21 
    22     /** default constructor */
    23     public AbstractStudent() {
    24     }
    25 
    26     /** full constructor */
    27     public AbstractStudent(Classes classes, String name, Integer age,
    28             String gender, String telephone, String email) {
    29         this.classes = classes;
    30         this.name = name;
    31         this.age = age;
    32         this.gender = gender;
    33         this.telephone = telephone;
    34         this.email = email;
    35     }
    36 
    37     // Property accessors
    38 
    39     public Integer getId() {
    40         return this.id;
    41     }
    42 
    43     public void setId(Integer id) {
    44         this.id = id;
    45     }
    46 
    47     public Classes getClasses() {
    48         return this.classes;
    49     }
    50 
    51     public void setClasses(Classes classes) {
    52         this.classes = classes;
    53     }
    54 
    55     public String getName() {
    56         return this.name;
    57     }
    58 
    59     public void setName(String name) {
    60         this.name = name;
    61     }
    62 
    63     public Integer getAge() {
    64         return this.age;
    65     }
    66 
    67     public void setAge(Integer age) {
    68         this.age = age;
    69     }
    70 
    71     public String getGender() {
    72         return this.gender;
    73     }
    74 
    75     public void setGender(String gender) {
    76         this.gender = gender;
    77     }
    78 
    79     public String getTelephone() {
    80         return this.telephone;
    81     }
    82 
    83     public void setTelephone(String telephone) {
    84         this.telephone = telephone;
    85     }
    86 
    87     public String getEmail() {
    88         return this.email;
    89     }
    90 
    91     public void setEmail(String email) {
    92         this.email = email;
    93     }
    94 
    95 }
    AbstractStudent
     1 package cn.stu.entity;
     2 
     3 import java.util.Set;
     4 
     5 /**
     6  * Classes entity. @author MyEclipse Persistence Tools
     7  */
     8 public class Classes extends AbstractClasses implements java.io.Serializable {
     9 
    10     // Constructors
    11 
    12     /** default constructor */
    13     public Classes() {
    14     }
    15 
    16     /** minimal constructor */
    17     public Classes(String name) {
    18         super(name);
    19     }
    20 
    21     /** full constructor */
    22     public Classes(String name, Set students) {
    23         super(name, students);
    24     }
    25 
    26 }
    Classes
     1 package cn.stu.entity;
     2 
     3 /**
     4  * Student entity. @author MyEclipse Persistence Tools
     5  */
     6 public class Student extends AbstractStudent implements java.io.Serializable {
     7 
     8     // Constructors
     9 
    10     /** default constructor */
    11     public Student() {
    12     }
    13 
    14     /** full constructor */
    15     public Student(Classes classes, String name, Integer age, String gender,
    16             String telephone, String email) {
    17         super(classes, name, age, gender, telephone, email);
    18     }
    19 
    20     
    21 }
    Student
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 <!-- 
     5     Mapping file autogenerated by MyEclipse Persistence Tools
     6 -->
     7 <hibernate-mapping>
     8     <class name="cn.stu.entity.Classes" table="classes" catalog="stuinfo" lazy="false">
     9         <id name="id" type="java.lang.Integer">
    10             <column name="id" />
    11             <generator class="identity" />
    12         </id>
    13         <property name="name" type="java.lang.String">
    14             <column name="name" length="11" not-null="true" />
    15         </property>
    16         <set name="students" inverse="true">
    17             <key>
    18                 <column name="classId" not-null="true">
    19                     <comment>班级编号</comment>
    20                 </column>
    21             </key>
    22             <one-to-many class="cn.stu.entity.Student" />
    23         </set>
    24     </class>
    25 </hibernate-mapping>
    Classes.hbm.xml
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 <!-- 
     5     Mapping file autogenerated by MyEclipse Persistence Tools
     6 -->
     7 <hibernate-mapping>
     8     <class name="cn.stu.entity.Student" table="student" catalog="stuinfo" lazy="false">
     9         <id name="id" type="java.lang.Integer">
    10             <column name="id" />
    11             <generator class="identity" />
    12         </id>
    13         <many-to-one name="classes" class="cn.stu.entity.Classes" fetch="select">
    14             <column name="classId" not-null="true">
    15                 <comment>班级编号</comment>
    16             </column>
    17         </many-to-one>
    18         <property name="name" type="java.lang.String">
    19             <column name="name" length="11" not-null="true">
    20                 <comment>姓名</comment>
    21             </column>
    22         </property>
    23         <property name="age" type="java.lang.Integer">
    24             <column name="age" not-null="true">
    25                 <comment>年龄</comment>
    26             </column>
    27         </property>
    28         <property name="gender" type="java.lang.String">
    29             <column name="gender" length="2" not-null="true">
    30                 <comment>性别</comment>
    31             </column>
    32         </property>
    33         <property name="telephone" type="java.lang.String">
    34             <column name="telephone" length="11" not-null="true">
    35                 <comment>电话</comment>
    36             </column>
    37         </property>
    38         <property name="email" type="java.lang.String">
    39             <column name="email" length="11" not-null="true">
    40                 <comment>Email</comment>
    41             </column>
    42         </property>
    43     </class>
    44 </hibernate-mapping>
    Student.hbm.xml

    7.DAO层

     1 package cn.stu.dao;
     2 
     3 import java.io.Serializable;
     4 import java.util.List;
     5 
     6 import cn.stu.entity.Classes;
     7 import cn.stu.entity.Student;
     8 
     9 public interface StuDao {
    10 
    11     /**
    12      * 查所有学生
    13      * 
    14      * @return
    15      */
    16     List<Student> showStudnets();
    17 
    18     /**
    19      * 添加学生
    20      * 
    21      * @param student
    22      */
    23     void addStudent(Student student);
    24 
    25     /**
    26      * 查所有班级列表
    27      * 
    28      * @return
    29      */
    30     List<Classes> showClasses();
    31 
    32     /**
    33      * 删除班级
    34      * 
    35      * @param id
    36      */
    37     void deleteClasses(Serializable id);
    38 
    39     /**
    40      * 删除学生
    41      * 
    42      * @param classId
    43      */
    44     void deleteStudent(Serializable classId);
    45 
    46 }
    StuDao
     1 package cn.stu.dao;
     2 
     3 import java.io.Serializable;
     4 import java.util.List;
     5 
     6 import org.junit.Test;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.context.support.ClassPathXmlApplicationContext;
     9 import org.springframework.orm.hibernate3.HibernateTemplate;
    10 import org.springframework.stereotype.Repository;
    11 
    12 import cn.stu.entity.Classes;
    13 import cn.stu.entity.Student;
    14 /*
    15  * @Repository注解将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。
    16  * 为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,
    17  * 需要在 XML 配置文件中启用Bean 的自动扫描功能,这可以通过<context:component-scan/>实现*/
    18 @Repository //
    19 public class StuDaoImpl implements StuDao {
    20 
    21     @Autowired //自动注入hibernateTemplate 这个bean
    22     private HibernateTemplate hibernateTemplate;
    23 
    24     
    25     @SuppressWarnings("unchecked")
    26     public List<Student> showStudnets() {
    27         String hql = "from Student ";
    28         return hibernateTemplate.find(hql);
    29     }
    30 
    31     public void addStudent(Student student) {
    32         
    33         hibernateTemplate.save(student);
    34     }
    35 
    36     public List<Classes> showClasses() {
    37         String hql="from Classes";
    38         return hibernateTemplate.find(hql);
    39         
    40     }
    41 
    42     public void deleteClasses(Serializable id) {
    43         Classes classes = hibernateTemplate.get(Classes.class, id);
    44         hibernateTemplate.delete(classes);
    45     }
    46 
    47     public void deleteStudent(Serializable classId) {
    48         //String hql = "delete from Student where classId =: classId";
    49         //bulkUpdate方法是hibernateTemplate提供的批量删除方法!!!
    50         hibernateTemplate.bulkUpdate("delete from Student where classId =?", new Object[]{classId});
    51         
    52     }
    53     
    54 
    55 }
    StuDaoImpl

    8.Service层

     1 package cn.stu.service;
     2 
     3 import java.io.Serializable;
     4 import java.util.List;
     5 
     6 import cn.stu.entity.Classes;
     7 import cn.stu.entity.Student;
     8 
     9 public interface StuService {
    10 
    11     List<Student> showStudnets();
    12 
    13     void addStudent(Student student);
    14 
    15     List<Classes> showClasses();
    16     
    17     void deleteClasses(Serializable id);
    18 }
    StuService
     1 package cn.stu.service;
     2 
     3 import java.io.Serializable;
     4 import java.util.List;
     5 
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.stereotype.Service;
     8 import org.springframework.transaction.annotation.Isolation;
     9 import org.springframework.transaction.annotation.Propagation;
    10 import org.springframework.transaction.annotation.Transactional;
    11 
    12 import cn.stu.dao.StuDao;
    13 import cn.stu.entity.Classes;
    14 import cn.stu.entity.Student;
    15 
    16 @Service
    17 @Transactional //事务注解,后面删除功能用到了事务管理
    18 public class StuServiceImpl implements StuService {
    19 
    20     @Autowired
    21     private StuDao dao;
    22     
    23     public void addStudent(Student student) {
    24         dao.addStudent(student);
    25     }
    26 
    27     public List<Classes> showClasses() {
    28         return dao.showClasses();
    29     }
    30 
    31     public List<Student> showStudnets() {
    32         return dao.showStudnets();
    33     }
    34     
    35     @Transactional(propagation=Propagation.REQUIRED,
    36             isolation=Isolation.DEFAULT,
    37             rollbackFor=Exception.class)
    38     public void deleteClasses(Serializable id){
    39         //删除班级,同时删除该班级下的学生,写在通一个service方法里,受事务保护
    40         System.out.println("删除班级和对应的学生,班级id:"+id);
    41         dao.deleteStudent(id);
    42         try {
    43             int i=1/0;
    44         } catch (Exception e) {
    45             e.printStackTrace();
    46         //    throw new RuntimeException(e);//如果进行捕获,必须要手动抛出异常,否则事务不会回滚
    47         }
    48         dao.deleteClasses(id);
    49     
    50         
    51     }
    52     
    53 
    54 }
    55 
    56 
    57 /*
    58  * @Transactional(propagation=Propagation.REQUIRED,
    59             isolation=Isolation.DEFAULT,
    60             rollbackFor=Exception.class)
    61     public void deleteClasses(Serializable id){
    62         //删除班级,同时删除该班级下的学生,写在通一个service方法里,受事务保护
    63         System.out.println("删除班级和对应的学生,班级id:"+id);
    64         try {
    65             if(id!=null){
    66                 dao.deleteStudent(id);
    67                 int i=1/0;
    68                 dao.deleteClasses(id);//在deleteClasses方法里面制造异常,还是能完整的删除
    69             }
    70         } catch (Exception e) {
    71             System.out.println("人为制造异常测试事务(删除班级及对应班级的学生)能否进行回滚!");
    72             
    73             e.printStackTrace();
    74         }
    75         
    76     }
    77  * */
    StuServiceImpl

     9.servlet层

      1 package cn.stu.aciton;
      2 
      3 import java.util.List;
      4 
      5 
      6 import org.springframework.beans.factory.annotation.Autowired;
      7 import org.springframework.stereotype.Controller;
      8 
      9 import cn.stu.entity.Classes;
     10 import cn.stu.entity.Student;
     11 import cn.stu.service.StuService;
     12 
     13 import com.opensymphony.xwork2.ActionContext;
     14 import com.opensymphony.xwork2.ActionSupport;
     15 
     16 /**
     17  * 使用struts的action作为控制器
     18  * 
     19  * @author Administrator
     20  * 
     21  */
     22 @Controller
     23 public class StuAction extends ActionSupport {
     24 
     25     private static final long serialVersionUID = 1L;
     26     @Autowired
     27     private StuService stuService;
     28     private List<Student> stus = null;
     29     private List<Classes> classes = null;
     30     private Student student;// 用于接收表单提交的添加学生信息
     31     private Integer classId;//要删除的班级编号————一定要生成get set方法,否则获取不到页面的参数值
     32 
     33     public Integer getClassId() {
     34         return classId;
     35     }
     36 
     37     public void setClassId(Integer classId) {
     38         this.classId = classId;
     39     }
     40 
     41     public List<Student> getStus() {
     42         return stus;
     43     }
     44 
     45     public void setStus(List<Student> stus) {
     46         this.stus = stus;
     47     }
     48 
     49     public List<Classes> getClasses() {
     50         return classes;
     51     }
     52 
     53     public void setClasses(List<Classes> classes) {
     54         this.classes = classes;
     55     }
     56 
     57     public Student getStudent() {
     58         return student;
     59     }
     60 
     61     public void setStudent(Student student) {
     62         this.student = student;
     63     }
     64 
     65     /**
     66      * 请求tomcat服务器http://localhost:8080/StuManagementSSH/stuListPage.action
     67      * 跳转到学生列表显示页面
     68      * 
     69      * @return
     70      */
     71     public String stuListPage() {
     72         System.out.println("in stuListPage method...");
     73         stus = stuService.showStudnets();
     74         return SUCCESS;
     75     }
     76 
     77     /**
     78      * 跳转到添加学生页面
     79      * 
     80      * @return
     81      */
     82     public String stuAddPage() {
     83         classes = stuService.showClasses();
     84         return SUCCESS;
     85     }
     86 
     87     /**
     88      * 添加学生页面表单提交
     89      * 
     90      * @return
     91      */
     92     public String addStu() {
     93         try {
     94             stuService.addStudent(student);
     95             ActionContext context = ActionContext.getContext();
     96         //    ActionContext.getContext().getSession().put("loginUser", student);//保存信息到session中
     97 //            Map<String, Object> session = context.getSession();
     98 //            Map<String, Object> application = context.getApplication();
     99 //            Map request = (Map)context.get("request");//与context.put()相同
    100 //            context.put("name", "gree");//放到request作用域
    101             return SUCCESS;
    102         } catch (Exception e) {
    103             
    104             e.printStackTrace();
    105             return "failed";
    106         }
    107     }
    108     
    109     
    110     public String delStu(){
    111         stuService.deleteClasses(classId);
    112         return SUCCESS;
    113     }
    114 }
    StuAction

    10.web.xml配置:

    (1)配置OpenSessionInViewFilter,保证一次请求期间,都使用同一个session,要设置在struts核心过滤器之前

    (2) struts过滤器配置

    (3)创建监听器,获得spring配置文件的信息,创建容器

    (4)启动时加载上下文,即加载spring

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="2.5" 
     3     xmlns="http://java.sun.com/xml/ns/javaee" 
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     7   <welcome-file-list>
     8     <welcome-file>index.jsp</welcome-file>
     9   </welcome-file-list>
    10   
    11   <!-- 防止懒加载异常,保证一次请求期间,都使用同一个session,要设置在struts核心过滤器之前 -->
    12   <filter>
    13       <filter-name>OpenSessionInViewFilter</filter-name>
    14       <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    15      <init-param>
    16          <param-name>sessionFactoryBeanName</param-name>
    17          <param-value>sessionFactory</param-value>   <!-- applicaionContext.xml中的sessionFactory -->
    18      </init-param>
    19   </filter>
    20   <filter-mapping>
    21       <filter-name>OpenSessionInViewFilter</filter-name>
    22       <url-pattern>*.action</url-pattern>
    23   </filter-mapping>
    24   
    25   <!-- struts过滤器配置 -->
    26   <filter>
    27       <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    28       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    29   </filter>
    30   <filter-mapping>
    31       <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    32       <!-- 只要是同一个action,都在一个会话里完成 -->
    33       <url-pattern>*.action</url-pattern>
    34       <!--  <url-pattern>*.jsp</url-pattern> -->
    35   </filter-mapping>
    36   
    37   <!-- 监听器,获得spring配置文件的信息,创建容器 -->
    38   <listener>
    39       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    40   </listener>
    41   
    42   <!-- 启动时加载上下文 -->
    43   <context-param>
    44       <param-name>contextConfigLocation</param-name>
    45       <param-value>classpath:applicationContext.xml</param-value>
    46   </context-param>
    47   
    48 </web-app>
    web.xml

    11. 学生列表显示页即添加信息页

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="/struts-tags"  prefix="s"%>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     
    13     <title>My JSP 'index.jsp' starting page</title>
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22   </head>
    23   
    24   <body>
    25     stulist page. <br>
    26     <a href="stuAddPage.action">添加学生</a>
    27     <table border="1">
    28         <s:iterator value="stus" var="stu">
    29             <tr>
    30                 <td><s:property value="#stu.id"/> </td>
    31                 <td><s:property value="#stu.name"/> </td>
    32                 <td><s:property value="#stu.age"/> </td>
    33                 <td><s:property value="#stu.gender"/> </td>
    34                 <td><s:property value="#stu.telephone"/> </td>
    35                 <td><s:property value="#stu.email"/> </td>
    36             </tr>
    37         </s:iterator>
    38     </table>
    39   </body>
    40 </html>
    index.jsp
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="/struts-tags" prefix="s"%>
     3 <%
     4     String path = request.getContextPath();
     5     String basePath = request.getScheme() + "://"
     6             + request.getServerName() + ":" + request.getServerPort()
     7             + path + "/";
     8 %>
     9 
    10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    11 <html>
    12     <head>
    13         <base href="<%=basePath%>">
    14 
    15         <title>My JSP 'index.jsp' starting page</title>
    16         <meta http-equiv="pragma" content="no-cache">
    17         <meta http-equiv="cache-control" content="no-cache">
    18         <meta http-equiv="expires" content="0">
    19         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    20         <meta http-equiv="description" content="This is my page">
    21         <!--
    22     <link rel="stylesheet" type="text/css" href="styles.css">
    23     -->
    24     </head>
    25 
    26     <body>
    27         add page.
    28         <br>
    29         <s:form action="addStu.action" method="post">
    30             <s:textfield label="姓名" name="student.name"></s:textfield>
    31             <s:textfield label="年龄" name="student.age"></s:textfield>
    32             <s:textfield label="性别" name="student.gender"></s:textfield>
    33             <s:textfield label="电话" name="student.telephone"></s:textfield>
    34             <s:textfield label="邮箱" name="student.email"></s:textfield>
    35             <s:select label="年级" name="student.classes.id" list="classes"
    36                 headerValue="请选择" headerKey="0" listKey="id" listValue="name">
    37             </s:select>
    38             <s:submit value="提交"></s:submit>
    39         </s:form>
    40     </body>
    41 </html>
    addPage.jsp

     根据struts配置文件中配置的请求访问路径,访问 http://localhost:8080/StuManagementSSH/stuListPage.action

    添加页面:(添加完成,要继续返回到列表显示页,因此添加完成后,应该重定向到请求列表页显示的那个服务)

    删除功能未做

  • 相关阅读:
    python3.6下安装wingIDE破解方法
    Python 列表list 和 字符串str 互转
    c# 读取txt文件并分隔
    基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系
    Jquery ajax动态更新下拉列表的内容
    vs2015使用技巧-------- 查看类关系图
    Dapper 批量操作sql
    Linq批量建表
    WebRequest的get及post提交
    git -- 常用命令
  • 原文地址:https://www.cnblogs.com/enjoyjava/p/8886777.html
Copyright © 2011-2022 走看看