hibernate3、struts2 web小程序

来源:互联网 发布:前端框架ajax取数据 编辑:程序博客网 时间:2024/05/17 09:24
第一步:建立一个web工程 
第二部:使用myeclipse对struts2的支持


选择使用自己的lib包
如果使用myeclipse的依赖包不会帮你放到\WebRoot\WEB-INF\lib里 
在编译的时候不会出错 但是在服务器运行的时候就会找不到这些lib包而报错


接下来我们再来把Struts的依赖包放进到\WebRoot\WEB-INF\lib里面

commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.16.jar
javassist-3.11.0.GA.jar
ognl-3.0.1.jar
struts2-core-2.2.3.1.jar
xwork-core-2.2.3.1.jar



这样我们的struts的环境就算是搭建好了
myeclipse 自动帮我生成了struts.xml 和 web.xml中需要的配置


这是在web.xml下面对struts的配置
  <filter>
     <filter-name >struts2</ filter-name>
     <filter-class >
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     </filter-class >
  </filter >
  <filter-mapping >
     <filter-name >struts2</ filter-name>
     <url-pattern >/*</ url-pattern>
  </filter-mapping >

第三步添加hibernate支持
这里我们依旧吧依赖包放到web-inf里面的lib包里面 道理同上

这里是创建hibernate配置文件 继续next 

配置数据库连接 其实也可以在后面手动配置效果是一样的 接着就finish 完成配置 (后面的那个next是创建一个sessionfactory类)这里我们手动写就不用创建了

这里是myeclipse自动生成的hibernate.cfg.xml

<?xml version='1.0' encoding= 'UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory >
        <property name="dialect" >org.hibernate.dialect.MySQLDialect</ property>
        <property name="connection.url" >jdbc:mysql://localhost:3306/hibernate</property >
        <property name="connection.username" >root</ property>
        <property name="connection.password" >root</ property>
        <property name="connection.driver_class" >com.mysql.jdbc.Driver</ property>
   
    </session-factory >

</hibernate-configuration>


我们要在里面加上一句 这样我们就知道hibernate干了些什么 
<!-- 设置在使用数据库时会显示使用的sql语句    开发过程中开启  -->
            <property name="show_sql" >true</ property>

接下来我们创建hibernate与表的映射文件 Person.hbm.xml

<? xml version ="1.0" encoding= "UTF-8" ?>
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping>
      <!-- 建立表与模型类的映射  name里面填类的名字 table里面填表的名字-->
      <class name= "model.Person" table ="person" >
      <!-- 告诉hibernate 模型类里面的id对映表中的id 如果 表中名字与模型类种的名字相同就可以缺省掉 column="id"-->
            < id name ="id" column= "id" type ="int" >
                 < generator class ="increment" > <!-- 主键id的生成方式为自增 -->
                 </ generator>
            </ id>
      <!-- 这里的设置与上面相同 只是上面定义的是主键 其他都一样 -->
            < property name ="username" type= "string"></ property >
            < property name ="password" type= "string"></ property >
            < property name ="age" type= "int"></ property >
            < property name ="registerDate" type= "date"></ property >
      </class >

</ hibernate-mapping>

创建一个model类 Person 没有强制性要求和表同名 但是为了看起来方便还是尽量取相同的名字

package model;

import java.sql.Date;

/**
 *
 * @author HuangMin 添加一个模型类对应于数据库种的一张表
 */

public class Person
{
      private String id ;
     
      private int age ;

      private String username ;
     
      private String password ;

      private Date registerDate ;

      public String getId()
     {
            return id ;
     }

      public void setId(String id)
     {
            this .id = id;
     }

      public int getAge()
     {
            return age ;
     }

      public void setAge( int age)
     {
            this .age = age;
     }

      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;
     }

      public Date getRegisterDate()
     {
            return registerDate ;
     }

      public void setRegisterDate(Date registerDate)
     {
            this .registerDate = registerDate;
     }


}


创建好了之后 返回到hibernate.cfg.xml里添加一行 让hibernate知道这个文件在哪
< mapping resource = "Person.hbm.xml" />

这样我们的基本环境就配好了

第四步 编写代码
先建立一个注册页面
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
< head>
< title> 注册 </title >
</ head>
< s:actionerror cssStyle ="color:red" />
< body>
      <form action= "savePerson.action">
           用户名: < input type ="text" name= "username"> <br >
           密 &nbsp;&nbsp; 码: < input type = "password" name ="password" > < br >
           年 &nbsp;&nbsp; 龄: < input type = "text" name ="age" > < br>
            < input type ="submit" value= "提交">
      </form >
</ body>
</ html>

提交之后给savePerson.action存到数据库中然后请求转发到一个查看所有用户信息的页面 

这里使用请求转发是为了你在看到用户信息的那个页面不会重复执行开始的那个存储功能

好了我们现在配置struts.xml 添加我们的action

<? xml version ="1.0" encoding= "UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" >
< struts>
      <package name= "hibernate" extends ="struts-default" >
      <!-- 这里一般都会继承struts-default 里面定义了让struts正常工作的拦截器什么的 我们用时候记得继承就好了-->

<!--action 的配置 name 就是外面跳转到你这的名字 class是你这个action的执行类 method是你这个action调用的方法 如果你这里不写默认就调用execute -->
    
<!--result 的配置 name表示的你要跳转的方法 我们现在用的这两个是struts自带的 success 成功 input失败 后面个就是他们跳转的页面了 type 里面就是他们跳转的类型 默认是dispatcher 就是请求转发  这里用的redirect 就是重定向 -->
    
                    
          
         < action name ="listPerson" class= "action.PersonAction"method ="list" >
                 < result name ="success" > /list.jsp</ result >
            </ action>

            < action name ="deletePerson" class ="action.PersonAction"method= "delete">
                 < result name ="success" type ="redirect" > listPerson</ result >
            </ action>

            < action name ="getPerson" class= "action.PersonAction" method ="getPerson" >
                 < result name ="success" > /getPerson.jsp</ result >
            </ action>

            < action name ="updatePPerson" class ="action.PersonAction"
                 method ="getPerson" >
                 < result name ="success" > /updatePerson.jsp</ result >
            </ action>

            < action name ="updatePerson" class ="action.PersonAction"method= "update">
                 < result name ="success" type ="redirectAction" > listPerson</ result >
            </ action>

      </package >

</ struts>     

跟着编写action的类
PersonAction.java


package action;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import model.Person;
import persistence.DBPerson;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport
{
     private String username;
     private String password;
     private int age;
     private int id;

     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;
     }

     public int getAge()
     {
          return age;
     }

     public void setAge(int age)
     {
          this.age = age;
     }

     public int getId()
     {
          return id;
     }

     public void setId(int id)
     {
          this.id = id;
     }
         
     public String save()
     {
          Person person = new Person();
          person.setAge(age);
          person.setId(id);
          person.setPassword(password);
          person.setUsername(username);
          
          java.sql.Date registerDate = new java.sql.Date(
                    new java.util.Date().getTime());
          person.setRegisterDate(registerDate);
          //这里在存之前先组装下对象

          DBPerson.save(person);

          // int id = person.getId();
          // person = DBPerson.getPeson(id);
          // HttpServletRequest request = ServletActionContext.getRequest();
          // request.setAttribute("person", person);

          return SUCCESS;
     }

     public String list()
     {     
          //这里得到结果的那个list的然后存到request里面

          List<Person> list = DBPerson.list();
          HttpServletRequest request = ServletActionContext.getRequest();
          request.setAttribute("list", list);
          return SUCCESS;
     }
     //删除用户
     public String delete()
     {

          Person person = new Person();
          DBPerson.delete(person, id);
          return SUCCESS;
     }
     //得到用户的详细信息
     public String getPerson()
     {
          Person person = DBPerson.getPeson(id);
          HttpServletRequest request = ServletActionContext.getRequest();
          request.setAttribute("person", person);
          return SUCCESS;
     }
     //更新操作
     public String update()
     {
          Person person = DBPerson.getPeson(id);
          person.setAge(age);
          person.setPassword(password);
          DBPerson.update(person);
          return SUCCESS;
     }
     //这里是对表单的验证
     public void validateSave()
     {
          if ("".equals(username) || "".equals(password))
          {
               addActionError("用户名或密码为空,请确认后提交!");
          }
     }

}


这些方法都是在BDPerson这个类里面写好的 这里只是调用 现在我们去写下这个类

package persistence;

import java.util.List;

import model.Person;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;

/**
 *
 * @author HuangMin 控制Person表的方法
 */

public class DBPerson
{
      /**
      * 创建新的用户
      */
      public static void save(Person person)
     {
           Session session = HibernateUtil.openSession(); // 打开session
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                session.save(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "添加用户时出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }

     }

      /**
      * 查询所有用户
      */
      @SuppressWarnings( "unchecked" )
      public static List<Person> list()
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务

           List<Person> list = null ;

            try
           {
                Query query = session.createQuery( "from Person"); // hql语句,Hibernate查询语句

                list = (List<Person>) query.list();

                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "查询用户时出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
            return list;
     }

      public static void delete(Person person, String id)
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                person = (Person) session.get(Person. class , id);
                session.delete(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "删除用户出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
     }

      public static Person getPeson(String id)
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务
           Person person = null ;
            try
           {
                person = (Person) session.get(Person. class , id);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "得到用户信息出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
            return person;
     }

      public static void update(Person person)
     {
           Session session = HibernateUtil.openSession(); // 打卡session
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                session.update(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "更新用户信息出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }

     }
}

这里我们还需要创建一个hibernate工具类 让他创建sessionfactory 还有控制seesion的开关


package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil
{
     private static SessionFactory sessionFactory;
     
     
     //在静态代码块里面创建sessionFactory
     static
     {
          try
          {     
               sessionFactory = new Configuration().configure()
                         .buildSessionFactory();
          }

          catch (Exception ex)
          {
               System.err.println("构造SessionFactory异常发生: " + ex.getMessage());
          }

     }
     //创建两个方法管理session
     public static Session openSession()
     {
          Session session = sessionFactory.openSession();

          return session;
     }

     public static void closeSession(Session session)
     {
          if (null != session)
          {
               session.close();
          }
     }
}

至此我们的java类已经全部写好了 回过头去写jsp 显示页面
getPerson.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
  <head >
   
    <title > 查看用户资料 </title >
   

  </head >
 
  <body >
   用户名: < s:property value ="#request.person.username" />< br>
   密 &nbsp;&nbsp;码: < s:property value ="#request.person.password" />< br>
   年 &nbsp;&nbsp;龄: < s:property value ="#request.person.age" />< br>
   注册日期: < s:property value ="#request.person.registerDate" />< br>
  
   <a href= "listPerson"> 点击返回用户页面 </ a>
  </body >
</ html>

list.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="GB18030" %>
<%@ taglib uri= "/struts-tags" prefix ="s" %>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
  <head >
   
    <title > 查询所有用户 </title >
   
      <meta http-equiv= "pragma" content = "no-cache">
      <meta http-equiv= "cache-control" content = "no-cache">
      <meta http-equiv= "expires" content = "0">    
      <meta http-equiv= "keywords" content ="keyword1,keyword2,keyword3" >
      <meta http-equiv= "description" content = "This is my page">
      <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->

      <script type= "text/javascript">
     
     function del()
     {
            if (confirm("您確定要要刪除么?" ))
           {
                 return true ;
           }
           
            return false ;
     }
     
      </ script>


  </head >
 
  <body >
 
      <table width= "60%" align ="center" border= "1">
     
      <tr >
      <th >
     用户名
      </th >
     
      <th >
     密码
      </th >
     
      <th >
     注册日期
      </th >
     
      <th >
     年龄
      </th >
     
      <th >
     更新
      </th >
     
      <th >
     删除
      </th >
      </tr >
     
      <s:iterator value= "#request.list" id ="person" >
     
      <tr align= "center">
      <td >
     
      <s:a href= "getPerson.action?id=%{#person.id}" >
      <s:property value= "username" />
      </s:a >
     
      </td >
     
      <td >
     
      <s:property value= "password"/>
     
      </td >

      <td >
     
      <s:property value= "registerDate"/>
     
      </td >

      <td >
     
      <s:property value= "age"/>
     
      </td >
     
      <td >
     
      <s:a href= "updatePPerson.action?id=%{#person.id}" >
     
     更新
     
      </s:a >
      </td >
     
     
      <td >
     
      <s:a href= "deletePerson.action?id=%{#person.id}" onclick ="return del();" >
 
     删除
     
      </s:a >
      </td >
     
      </tr >
     
      </s:iterator >
     
      </table >
 
  </body >
</ html>

updatePerson.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
< head>

< title> 更新用户信息 </title >

</ head>

< form action ="updatePerson" method= "post">
      <s:hidden name= "id" value ="%{#request.person.id}" ></ s:hidden>

     用户名:
      <s:textfield name= "username" value ="%{#request.person.username}"
            readonly ="true" ></ s:textfield>
            < br>
     密 &nbsp;&nbsp; 码:
      <s:textfield name= "password" value ="%{#request.person.password}" ></ s:textfield>
      <br >
     年 &nbsp;&nbsp; 龄:
      <s:textfield name= "age" value ="%{#request.person.age}" ></ s:textfield>
      <br >
     注册日期
      <s:textfield name= "registerDate"
            value ="%{#request.person.registerDate}" readonly ="true" ></ s:textfield>
      <br >
      <input type= "submit" value ="更新" >

</ form>


< body>


</ body>
</ html>
现在打开你的服务器就能看到了 非常简单的小程序 看着张龙老师的视频 之后写的 因为知识有限如果有错的地方希望指正第一步:建立一个web工程 
第二部:使用myeclipse对struts2的支持


选择使用自己的lib包
如果使用myeclipse的依赖包不会帮你放到\WebRoot\WEB-INF\lib里 
在编译的时候不会出错 但是在服务器运行的时候就会找不到这些lib包而报错


接下来我们再来把Struts的依赖包放进到\WebRoot\WEB-INF\lib里面

commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.16.jar
javassist-3.11.0.GA.jar
ognl-3.0.1.jar
struts2-core-2.2.3.1.jar
xwork-core-2.2.3.1.jar



这样我们的struts的环境就算是搭建好了
myeclipse 自动帮我生成了struts.xml 和 web.xml中需要的配置


这是在web.xml下面对struts的配置
  <filter>
     <filter-name >struts2</ filter-name>
     <filter-class >
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     </filter-class >
  </filter >
  <filter-mapping >
     <filter-name >struts2</ filter-name>
     <url-pattern >/*</ url-pattern>
  </filter-mapping >

第三步添加hibernate支持
这里我们依旧吧依赖包放到web-inf里面的lib包里面 道理同上

这里是创建hibernate配置文件 继续next 

配置数据库连接 其实也可以在后面手动配置效果是一样的 接着就finish 完成配置 (后面的那个next是创建一个sessionfactory类)这里我们手动写就不用创建了

这里是myeclipse自动生成的hibernate.cfg.xml

<?xml version='1.0' encoding= 'UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory >
        <property name="dialect" >org.hibernate.dialect.MySQLDialect</ property>
        <property name="connection.url" >jdbc:mysql://localhost:3306/hibernate</property >
        <property name="connection.username" >root</ property>
        <property name="connection.password" >root</ property>
        <property name="connection.driver_class" >com.mysql.jdbc.Driver</ property>
   
    </session-factory >

</hibernate-configuration>


我们要在里面加上一句 这样我们就知道hibernate干了些什么 
<!-- 设置在使用数据库时会显示使用的sql语句    开发过程中开启  -->
            <property name="show_sql" >true</ property>

接下来我们创建hibernate与表的映射文件 Person.hbm.xml

<? xml version ="1.0" encoding= "UTF-8" ?>
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping>
      <!-- 建立表与模型类的映射  name里面填类的名字 table里面填表的名字-->
      <class name= "model.Person" table ="person" >
      <!-- 告诉hibernate 模型类里面的id对映表中的id 如果 表中名字与模型类种的名字相同就可以缺省掉 column="id"-->
            < id name ="id" column= "id" type ="int" >
                 < generator class ="increment" > <!-- 主键id的生成方式为自增 -->
                 </ generator>
            </ id>
      <!-- 这里的设置与上面相同 只是上面定义的是主键 其他都一样 -->
            < property name ="username" type= "string"></ property >
            < property name ="password" type= "string"></ property >
            < property name ="age" type= "int"></ property >
            < property name ="registerDate" type= "date"></ property >
      </class >

</ hibernate-mapping>

创建一个model类 Person 没有强制性要求和表同名 但是为了看起来方便还是尽量取相同的名字

package model;

import java.sql.Date;

/**
 *
 * @author HuangMin 添加一个模型类对应于数据库种的一张表
 */

public class Person
{
      private String id ;
     
      private int age ;

      private String username ;
     
      private String password ;

      private Date registerDate ;

      public String getId()
     {
            return id ;
     }

      public void setId(String id)
     {
            this .id = id;
     }

      public int getAge()
     {
            return age ;
     }

      public void setAge( int age)
     {
            this .age = age;
     }

      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;
     }

      public Date getRegisterDate()
     {
            return registerDate ;
     }

      public void setRegisterDate(Date registerDate)
     {
            this .registerDate = registerDate;
     }


}


创建好了之后 返回到hibernate.cfg.xml里添加一行 让hibernate知道这个文件在哪
< mapping resource = "Person.hbm.xml" />

这样我们的基本环境就配好了

第四步 编写代码
先建立一个注册页面
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
< head>
< title> 注册 </title >
</ head>
< s:actionerror cssStyle ="color:red" />
< body>
      <form action= "savePerson.action">
           用户名: < input type ="text" name= "username"> <br >
           密 &nbsp;&nbsp; 码: < input type = "password" name ="password" > < br >
           年 &nbsp;&nbsp; 龄: < input type = "text" name ="age" > < br>
            < input type ="submit" value= "提交">
      </form >
</ body>
</ html>

提交之后给savePerson.action存到数据库中然后请求转发到一个查看所有用户信息的页面 

这里使用请求转发是为了你在看到用户信息的那个页面不会重复执行开始的那个存储功能

好了我们现在配置struts.xml 添加我们的action

<? xml version ="1.0" encoding= "UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" >
< struts>
      <package name= "hibernate" extends ="struts-default" >
      <!-- 这里一般都会继承struts-default 里面定义了让struts正常工作的拦截器什么的 我们用时候记得继承就好了-->

<!--action 的配置 name 就是外面跳转到你这的名字 class是你这个action的执行类 method是你这个action调用的方法 如果你这里不写默认就调用execute -->
    
<!--result 的配置 name表示的你要跳转的方法 我们现在用的这两个是struts自带的 success 成功 input失败 后面个就是他们跳转的页面了 type 里面就是他们跳转的类型 默认是dispatcher 就是请求转发  这里用的redirect 就是重定向 -->
    
                    
                 < result name = "success" type = "redirect" >/result.jsp </ result>
     
                 < result name ="input" > /regitser.jsp</ result >
            </ action>
     
            < action name ="listPerson" class= "action.PersonAction"method ="list" >
                 < result name ="success" > /list.jsp</ result >
            </ action>

            < action name ="deletePerson" class ="action.PersonAction"method= "delete">
                 < result name ="success" type ="redirect" > listPerson</ result >
            </ action>

            < action name ="getPerson" class= "action.PersonAction" method ="getPerson" >
                 < result name ="success" > /getPerson.jsp</ result >
            </ action>

            < action name ="updatePPerson" class ="action.PersonAction"
                 method ="getPerson" >
                 < result name ="success" > /updatePerson.jsp</ result >
            </ action>

            < action name ="updatePerson" class ="action.PersonAction"method= "update">
                 < result name ="success" type ="redirectAction" > listPerson</ result >
            </ action>

      </package >

</ struts>     

跟着编写action的类
PersonAction.java


package action;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import model.Person;
import persistence.DBPerson;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport
{
     private String username;
     private String password;
     private int age;
     private int id;

     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;
     }

     public int getAge()
     {
          return age;
     }

     public void setAge(int age)
     {
          this.age = age;
     }

     public int getId()
     {
          return id;
     }

     public void setId(int id)
     {
          this.id = id;
     }
         
     public String save()
     {
          Person person = new Person();
          person.setAge(age);
          person.setId(id);
          person.setPassword(password);
          person.setUsername(username);
          
          java.sql.Date registerDate = new java.sql.Date(
                    new java.util.Date().getTime());
          person.setRegisterDate(registerDate);
          //这里在存之前先组装下对象

          DBPerson.save(person);

          // int id = person.getId();
          // person = DBPerson.getPeson(id);
          // HttpServletRequest request = ServletActionContext.getRequest();
          // request.setAttribute("person", person);

          return SUCCESS;
     }

     public String list()
     {     
          //这里得到结果的那个list的然后存到request里面

          List<Person> list = DBPerson.list();
          HttpServletRequest request = ServletActionContext.getRequest();
          request.setAttribute("list", list);
          return SUCCESS;
     }
     //删除用户
     public String delete()
     {

          Person person = new Person();
          DBPerson.delete(person, id);
          return SUCCESS;
     }
     //得到用户的详细信息
     public String getPerson()
     {
          Person person = DBPerson.getPeson(id);
          HttpServletRequest request = ServletActionContext.getRequest();
          request.setAttribute("person", person);
          return SUCCESS;
     }
     //更新操作
     public String update()
     {
          Person person = DBPerson.getPeson(id);
          person.setAge(age);
          person.setPassword(password);
          DBPerson.update(person);
          return SUCCESS;
     }
     //这里是对表单的验证
     public void validateSave()
     {
          if ("".equals(username) || "".equals(password))
          {
               addActionError("用户名或密码为空,请确认后提交!");
          }
     }

}


这些方法都是在BDPerson这个类里面写好的 这里只是调用 现在我们去写下这个类

package persistence;

import java.util.List;

import model.Person;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;

/**
 *
 * @author HuangMin 控制Person表的方法
 */

public class DBPerson
{
      /**
      * 创建新的用户
      */
      public static void save(Person person)
     {
           Session session = HibernateUtil.openSession(); // 打开session
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                session.save(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "添加用户时出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }

     }

      /**
      * 查询所有用户
      */
      @SuppressWarnings( "unchecked" )
      public static List<Person> list()
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务

           List<Person> list = null ;

            try
           {
                Query query = session.createQuery( "from Person"); // hql语句,Hibernate查询语句

                list = (List<Person>) query.list();

                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "查询用户时出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
            return list;
     }

      public static void delete(Person person, String id)
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                person = (Person) session.get(Person. class , id);
                session.delete(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "删除用户出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
     }

      public static Person getPeson(String id)
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务
           Person person = null ;
            try
           {
                person = (Person) session.get(Person. class , id);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "得到用户信息出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
            return person;
     }

      public static void update(Person person)
     {
           Session session = HibernateUtil.openSession(); // 打卡session
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                session.update(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "更新用户信息出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }

     }
}

这里我们还需要创建一个hibernate工具类 让他创建sessionfactory 还有控制seesion的开关


package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil
{
     private static SessionFactory sessionFactory;
     
     
     //在静态代码块里面创建sessionFactory
     static
     {
          try
          {     
               sessionFactory = new Configuration().configure()
                         .buildSessionFactory();
          }

          catch (Exception ex)
          {
               System.err.println("构造SessionFactory异常发生: " + ex.getMessage());
          }

     }
     //创建两个方法管理session
     public static Session openSession()
     {
          Session session = sessionFactory.openSession();

          return session;
     }

     public static void closeSession(Session session)
     {
          if (null != session)
          {
               session.close();
          }
     }
}

至此我们的java类已经全部写好了 回过头去写jsp 显示页面
getPerson.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
  <head >
   
    <title > 查看用户资料 </title >
   

  </head >
 
  <body >
   用户名: < s:property value ="#request.person.username" />< br>
   密 &nbsp;&nbsp;码: < s:property value ="#request.person.password" />< br>
   年 &nbsp;&nbsp;龄: < s:property value ="#request.person.age" />< br>
   注册日期: < s:property value ="#request.person.registerDate" />< br>
  
   <a href= "listPerson"> 点击返回用户页面 </ a>
  </body >
</ html>

list.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="GB18030" %>
<%@ taglib uri= "/struts-tags" prefix ="s" %>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
  <head >
   
    <title > 查询所有用户 </title >
   
      <meta http-equiv= "pragma" content = "no-cache">
      <meta http-equiv= "cache-control" content = "no-cache">
      <meta http-equiv= "expires" content = "0">    
      <meta http-equiv= "keywords" content ="keyword1,keyword2,keyword3" >
      <meta http-equiv= "description" content = "This is my page">
      <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->

      <script type= "text/javascript">
     
     function del()
     {
            if (confirm("您確定要要刪除么?" ))
           {
                 return true ;
           }
           
            return false ;
     }
     
      </ script>


  </head >
 
  <body >
 
      <table width= "60%" align ="center" border= "1">
     
      <tr >
      <th >
     用户名
      </th >
     
      <th >
     密码
      </th >
     
      <th >
     注册日期
      </th >
     
      <th >
     年龄
      </th >
     
      <th >
     更新
      </th >
     
      <th >
     删除
      </th >
      </tr >
     
      <s:iterator value= "#request.list" id ="person" >
     
      <tr align= "center">
      <td >
     
      <s:a href= "getPerson.action?id=%{#person.id}" >
      <s:property value= "username" />
      </s:a >
     
      </td >
     
      <td >
     
      <s:property value= "password"/>
     
      </td >

      <td >
     
      <s:property value= "registerDate"/>
     
      </td >

      <td >
     
      <s:property value= "age"/>
     
      </td >
     
      <td >
     
      <s:a href= "updatePPerson.action?id=%{#person.id}" >
     
     更新
     
      </s:a >
      </td >
     
     
      <td >
     
      <s:a href= "deletePerson.action?id=%{#person.id}" onclick ="return del();" >
 
     删除
     
      </s:a >
      </td >
     
      </tr >
     
      </s:iterator >
     
      </table >
 
  </body >
</ html>

updatePerson.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
< head>

< title> 更新用户信息 </title >

</ head>

< form action ="updatePerson" method= "post">
      <s:hidden name= "id" value ="%{#request.person.id}" ></ s:hidden>

     用户名:
      <s:textfield name= "username" value ="%{#request.person.username}"
            readonly ="true" ></ s:textfield>
            < br>
     密 &nbsp;&nbsp; 码:
      <s:textfield name= "password" value ="%{#request.person.password}" ></ s:textfield>
      <br >
     年 &nbsp;&nbsp; 龄:
      <s:textfield name= "age" value ="%{#request.person.age}" ></ s:textfield>
      <br >
     注册日期
      <s:textfield name= "registerDate"
            value ="%{#request.person.registerDate}" readonly ="true" ></ s:textfield>
      <br >
      <input type= "submit" value ="更新" >

</ form>


< body>


</ body>
</ html>
现在打开你的服务器就能看到了 非常简单的小程序 看着张龙老师的视频 之后写的 因为知识有限如果有错的地方希望指正第一步:建立一个web工程 
第二部:使用myeclipse对struts2的支持


选择使用自己的lib包
如果使用myeclipse的依赖包不会帮你放到\WebRoot\WEB-INF\lib里 
在编译的时候不会出错 但是在服务器运行的时候就会找不到这些lib包而报错


接下来我们再来把Struts的依赖包放进到\WebRoot\WEB-INF\lib里面

commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.16.jar
javassist-3.11.0.GA.jar
ognl-3.0.1.jar
struts2-core-2.2.3.1.jar
xwork-core-2.2.3.1.jar



这样我们的struts的环境就算是搭建好了
myeclipse 自动帮我生成了struts.xml 和 web.xml中需要的配置


这是在web.xml下面对struts的配置
  <filter>
     <filter-name >struts2</ filter-name>
     <filter-class >
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     </filter-class >
  </filter >
  <filter-mapping >
     <filter-name >struts2</ filter-name>
     <url-pattern >/*</ url-pattern>
  </filter-mapping >

第三步添加hibernate支持
这里我们依旧吧依赖包放到web-inf里面的lib包里面 道理同上

这里是创建hibernate配置文件 继续next 

配置数据库连接 其实也可以在后面手动配置效果是一样的 接着就finish 完成配置 (后面的那个next是创建一个sessionfactory类)这里我们手动写就不用创建了

这里是myeclipse自动生成的hibernate.cfg.xml

<?xml version='1.0' encoding= 'UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory >
        <property name="dialect" >org.hibernate.dialect.MySQLDialect</ property>
        <property name="connection.url" >jdbc:mysql://localhost:3306/hibernate</property >
        <property name="connection.username" >root</ property>
        <property name="connection.password" >root</ property>
        <property name="connection.driver_class" >com.mysql.jdbc.Driver</ property>
   
    </session-factory >

</hibernate-configuration>


我们要在里面加上一句 这样我们就知道hibernate干了些什么 
<!-- 设置在使用数据库时会显示使用的sql语句    开发过程中开启  -->
            <property name="show_sql" >true</ property>

接下来我们创建hibernate与表的映射文件 Person.hbm.xml

<? xml version ="1.0" encoding= "UTF-8" ?>
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping>
      <!-- 建立表与模型类的映射  name里面填类的名字 table里面填表的名字-->
      <class name= "model.Person" table ="person" >
      <!-- 告诉hibernate 模型类里面的id对映表中的id 如果 表中名字与模型类种的名字相同就可以缺省掉 column="id"-->
            < id name ="id" column= "id" type ="int" >
                 < generator class ="increment" > <!-- 主键id的生成方式为自增 -->
                 </ generator>
            </ id>
      <!-- 这里的设置与上面相同 只是上面定义的是主键 其他都一样 -->
            < property name ="username" type= "string"></ property >
            < property name ="password" type= "string"></ property >
            < property name ="age" type= "int"></ property >
            < property name ="registerDate" type= "date"></ property >
      </class >

</ hibernate-mapping>

创建一个model类 Person 没有强制性要求和表同名 但是为了看起来方便还是尽量取相同的名字

package model;

import java.sql.Date;

/**
 *
 * @author HuangMin 添加一个模型类对应于数据库种的一张表
 */

public class Person
{
      private String id ;
     
      private int age ;

      private String username ;
     
      private String password ;

      private Date registerDate ;

      public String getId()
     {
            return id ;
     }

      public void setId(String id)
     {
            this .id = id;
     }

      public int getAge()
     {
            return age ;
     }

      public void setAge( int age)
     {
            this .age = age;
     }

      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;
     }

      public Date getRegisterDate()
     {
            return registerDate ;
     }

      public void setRegisterDate(Date registerDate)
     {
            this .registerDate = registerDate;
     }


}


创建好了之后 返回到hibernate.cfg.xml里添加一行 让hibernate知道这个文件在哪
< mapping resource = "Person.hbm.xml" />

这样我们的基本环境就配好了

第四步 编写代码
先建立一个注册页面
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
< head>
< title> 注册 </title >
</ head>
< s:actionerror cssStyle ="color:red" />
< body>
      <form action= "savePerson.action">
           用户名: < input type ="text" name= "username"> <br >
           密 &nbsp;&nbsp; 码: < input type = "password" name ="password" > < br >
           年 &nbsp;&nbsp; 龄: < input type = "text" name ="age" > < br>
            < input type ="submit" value= "提交">
      </form >
</ body>
</ html>

提交之后给savePerson.action存到数据库中然后请求转发到一个查看所有用户信息的页面 

这里使用请求转发是为了你在看到用户信息的那个页面不会重复执行开始的那个存储功能

好了我们现在配置struts.xml 添加我们的action

<? xml version ="1.0" encoding= "UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" >
< struts>
      <package name= "hibernate" extends ="struts-default" >
      <!-- 这里一般都会继承struts-default 里面定义了让struts正常工作的拦截器什么的 我们用时候记得继承就好了-->

<!--action 的配置 name 就是外面跳转到你这的名字 class是你这个action的执行类 method是你这个action调用的方法 如果你这里不写默认就调用execute -->
    
<!--result 的配置 name表示的你要跳转的方法 我们现在用的这两个是struts自带的 success 成功 input失败 后面个就是他们跳转的页面了 type 里面就是他们跳转的类型 默认是dispatcher 就是请求转发  这里用的redirect 就是重定向 -->
    
                    
                 < result name = "success" type = "redirect" >/result.jsp </ result>
     
                 < result name ="input" > /regitser.jsp</ result >
            </ action>
     
            < action name ="listPerson" class= "action.PersonAction"method ="list" >
                 < result name ="success" > /list.jsp</ result >
            </ action>

            < action name ="deletePerson" class ="action.PersonAction"method= "delete">
                 < result name ="success" type ="redirect" > listPerson</ result >
            </ action>

            < action name ="getPerson" class= "action.PersonAction" method ="getPerson" >
                 < result name ="success" > /getPerson.jsp</ result >
            </ action>

            < action name ="updatePPerson" class ="action.PersonAction"
                 method ="getPerson" >
                 < result name ="success" > /updatePerson.jsp</ result >
            </ action>

            < action name ="updatePerson" class ="action.PersonAction"method= "update">
                 < result name ="success" type ="redirectAction" > listPerson</ result >
            </ action>

      </package >

</ struts>     

跟着编写action的类
PersonAction.java


package action;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import model.Person;
import persistence.DBPerson;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport
{
     private String username;
     private String password;
     private int age;
     private int id;

     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;
     }

     public int getAge()
     {
          return age;
     }

     public void setAge(int age)
     {
          this.age = age;
     }

     public int getId()
     {
          return id;
     }

     public void setId(int id)
     {
          this.id = id;
     }
         
     public String save()
     {
          Person person = new Person();
          person.setAge(age);
          person.setId(id);
          person.setPassword(password);
          person.setUsername(username);
          
          java.sql.Date registerDate = new java.sql.Date(
                    new java.util.Date().getTime());
          person.setRegisterDate(registerDate);
          //这里在存之前先组装下对象

          DBPerson.save(person);

          // int id = person.getId();
          // person = DBPerson.getPeson(id);
          // HttpServletRequest request = ServletActionContext.getRequest();
          // request.setAttribute("person", person);

          return SUCCESS;
     }

     public String list()
     {     
          //这里得到结果的那个list的然后存到request里面

          List<Person> list = DBPerson.list();
          HttpServletRequest request = ServletActionContext.getRequest();
          request.setAttribute("list", list);
          return SUCCESS;
     }
     //删除用户
     public String delete()
     {

          Person person = new Person();
          DBPerson.delete(person, id);
          return SUCCESS;
     }
     //得到用户的详细信息
     public String getPerson()
     {
          Person person = DBPerson.getPeson(id);
          HttpServletRequest request = ServletActionContext.getRequest();
          request.setAttribute("person", person);
          return SUCCESS;
     }
     //更新操作
     public String update()
     {
          Person person = DBPerson.getPeson(id);
          person.setAge(age);
          person.setPassword(password);
          DBPerson.update(person);
          return SUCCESS;
     }
     //这里是对表单的验证
     public void validateSave()
     {
          if ("".equals(username) || "".equals(password))
          {
               addActionError("用户名或密码为空,请确认后提交!");
          }
     }

}


这些方法都是在BDPerson这个类里面写好的 这里只是调用 现在我们去写下这个类

package persistence;

import java.util.List;

import model.Person;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;

/**
 *
 * @author HuangMin 控制Person表的方法
 */

public class DBPerson
{
      /**
      * 创建新的用户
      */
      public static void save(Person person)
     {
           Session session = HibernateUtil.openSession(); // 打开session
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                session.save(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "添加用户时出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }

     }

      /**
      * 查询所有用户
      */
      @SuppressWarnings( "unchecked" )
      public static List<Person> list()
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务

           List<Person> list = null ;

            try
           {
                Query query = session.createQuery( "from Person"); // hql语句,Hibernate查询语句

                list = (List<Person>) query.list();

                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "查询用户时出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
            return list;
     }

      public static void delete(Person person, String id)
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                person = (Person) session.get(Person. class , id);
                session.delete(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "删除用户出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
     }

      public static Person getPeson(String id)
     {
           Session session = HibernateUtil.openSession();
           Transaction tx = session.beginTransaction(); // 开启事务
           Person person = null ;
            try
           {
                person = (Person) session.get(Person. class , id);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "得到用户信息出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }
            return person;
     }

      public static void update(Person person)
     {
           Session session = HibernateUtil.openSession(); // 打卡session
           Transaction tx = session.beginTransaction(); // 开启事务
            try
           {
                session.update(person);
                tx.commit();
           } catch (Exception e)
           {
                System.out.println( "更新用户信息出错!" );
                 if (null != tx)
                {
                     tx.rollback();
                }
           } finally
           {
                HibernateUtil.closeSession(session);
           }

     }
}

这里我们还需要创建一个hibernate工具类 让他创建sessionfactory 还有控制seesion的开关


package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil
{
     private static SessionFactory sessionFactory;
     
     
     //在静态代码块里面创建sessionFactory
     static
     {
          try
          {     
               sessionFactory = new Configuration().configure()
                         .buildSessionFactory();
          }

          catch (Exception ex)
          {
               System.err.println("构造SessionFactory异常发生: " + ex.getMessage());
          }

     }
     //创建两个方法管理session
     public static Session openSession()
     {
          Session session = sessionFactory.openSession();

          return session;
     }

     public static void closeSession(Session session)
     {
          if (null != session)
          {
               session.close();
          }
     }
}

至此我们的java类已经全部写好了 回过头去写jsp 显示页面
getPerson.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
  <head >
   
    <title > 查看用户资料 </title >
   

  </head >
 
  <body >
   用户名: < s:property value ="#request.person.username" />< br>
   密 &nbsp;&nbsp;码: < s:property value ="#request.person.password" />< br>
   年 &nbsp;&nbsp;龄: < s:property value ="#request.person.age" />< br>
   注册日期: < s:property value ="#request.person.registerDate" />< br>
  
   <a href= "listPerson"> 点击返回用户页面 </ a>
  </body >
</ html>

list.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="GB18030" %>
<%@ taglib uri= "/struts-tags" prefix ="s" %>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
  <head >
   
    <title > 查询所有用户 </title >
   
      <meta http-equiv= "pragma" content = "no-cache">
      <meta http-equiv= "cache-control" content = "no-cache">
      <meta http-equiv= "expires" content = "0">    
      <meta http-equiv= "keywords" content ="keyword1,keyword2,keyword3" >
      <meta http-equiv= "description" content = "This is my page">
      <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->

      <script type= "text/javascript">
     
     function del()
     {
            if (confirm("您確定要要刪除么?" ))
           {
                 return true ;
           }
           
            return false ;
     }
     
      </ script>


  </head >
 
  <body >
 
      <table width= "60%" align ="center" border= "1">
     
      <tr >
      <th >
     用户名
      </th >
     
      <th >
     密码
      </th >
     
      <th >
     注册日期
      </th >
     
      <th >
     年龄
      </th >
     
      <th >
     更新
      </th >
     
      <th >
     删除
      </th >
      </tr >
     
      <s:iterator value= "#request.list" id ="person" >
     
      <tr align= "center">
      <td >
     
      <s:a href= "getPerson.action?id=%{#person.id}" >
      <s:property value= "username" />
      </s:a >
     
      </td >
     
      <td >
     
      <s:property value= "password"/>
     
      </td >

      <td >
     
      <s:property value= "registerDate"/>
     
      </td >

      <td >
     
      <s:property value= "age"/>
     
      </td >
     
      <td >
     
      <s:a href= "updatePPerson.action?id=%{#person.id}" >
     
     更新
     
      </s:a >
      </td >
     
     
      <td >
     
      <s:a href= "deletePerson.action?id=%{#person.id}" onclick ="return del();" >
 
     删除
     
      </s:a >
      </td >
     
      </tr >
     
      </s:iterator >
     
      </table >
 
  </body >
</ html>

updatePerson.jsp
<%@ page language= "java" import = "java.util.*" pageEncoding ="UTF-8" %>
<%@ taglib prefix= "s" uri ="/struts-tags" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
< head>

< title> 更新用户信息 </title >

</ head>

< form action ="updatePerson" method= "post">
      <s:hidden name= "id" value ="%{#request.person.id}" ></ s:hidden>

     用户名:
      <s:textfield name= "username" value ="%{#request.person.username}"
            readonly ="true" ></ s:textfield>
            < br>
     密 &nbsp;&nbsp; 码:
      <s:textfield name= "password" value ="%{#request.person.password}" ></ s:textfield>
      <br >
     年 &nbsp;&nbsp; 龄:
      <s:textfield name= "age" value ="%{#request.person.age}" ></ s:textfield>
      <br >
     注册日期
      <s:textfield name= "registerDate"
            value ="%{#request.person.registerDate}" readonly ="true" ></ s:textfield>
      <br >
      <input type= "submit" value ="更新" >

</ form>


< body>


</ body>
</ html>
现在打开你的服务器就能看到了 非常简单的小程序 看着张龙老师的视频 之后写的 因为知识有限如果有错的地方希望指正