速动画教程第十五集

 

本集为第十二集Struts+Spring+Hibernate练习(完整) 续集

 

下载地址:http://sonic.peakle.net/download/sonic015.rar

 

将增加以下部份内容

 

注册新用户,用户浏览、删除用户

 

现在导入原工程

 

打开 UserDAO.java 文件,增加以下方法,此方法为增加用户

public interface UserDAO {

    public abstract boolean isValidUser(String username, String password);

    public abstract boolean addUser(User user);

}

 

打开 UserDAOImp.java 文件,添加接口方法的实现,并为方法编写以下内容:

    public boolean addUser(User user) {

        try {

            this.getHibernateTemplate().save(user);

        } catch (Exception e) {

            System.out.println(e.getMessage());

            return false;

        }

        return true;

    }

 

 

按下 Ctrl + N 使用向导创建 add.jsp AddForm( 动态 ) AddAction.java 文件 这是注册新用户的 Action

 

修改配置内容如下

< action

      attribute = "addForm"

      input = "/add.jsp"

      name = "addForm"

      path = "/add"

      scope = "request"

      validate="true"

      type = " org.springframework.web.struts.DelegatingActionProxy " />

 

第一句实现了必须对提交的数据进行验证

第二句实现了和spring集成

 

修改 AddAction.java 文件,内容如下:

 

    private UserDAO userDAO;

   

    public UserDAO getUserDAO() {

        return userDAO;

    }

    public void setUserDAO(UserDAO userDAO) {

        this.userDAO = userDAO;

    }

 

    public ActionForward execute(

        ActionMapping mapping,

        ActionForm form,

        HttpServletRequest request,

        HttpServletResponse response) {

        DynaActionForm addForm = (DynaActionForm) form;

        User user=new User();

        user.setUsername(addForm.getString("username"));

        user.setPassword(addForm.getString("password"));

        addForm.set("password", null);

        if (userDAO.addUser(user)) {

            return mapping.findForward("indexGo");

        } else {

            return mapping.getInputForward();

        }

    }

 

关于在 Action 中使用 User 对像 我在这里没有使用 IoC 方式进行管理 这不好 希望大家在正式应用中使用。

 

spring 配置文件 applicationContext.xml 中加入以下内容

       < bean name = "/add" class = "com.test.struts.action.AddAction" singleton = "false" >

       < property name = "userDAO" >

           < ref bean = "userDAOProxy" />

       </ property >

    </ bean >

 

修改 index.jsp 文件

< html >

  < head >

    < title > Struts+Spring+Hibernate </ title >

  </ head >

 

  < body >

    < a href = "login.jsp" > Login </ a >

    <br>

    <a href="add.jsp">Add User</a>

  </ body >

</ html >

 

修改 add.jsp 文件的首行 替换为以下内容 目的是为了使用 UTF-8 的编码格式显示页面

<%@ page language = "java" contentType = "text/html; charset=UTF-8" %>

 

修改 validation.xml 文件 加入以下内容

        < form name = "addForm" >

           < field property = "username" depends = "required" >

              < arg0 key = "prompt.username" />

           </ field >

           < field property = "password" depends = "required" >

              < arg0 key = "prompt.password" />

           </ field >

       </ form >

 

 

现在整个 AddUser 已经完成,如果你觉得不放心的话可以在这里停一停先进行这一段的测试!

 

创建一个Action用于查询数据,名称为 find

 

创建 display.jsp 文件,用于显示所有用户

 

创建一个 forward 指向 display.jsp 选中 redirect

 

修改 UserDAO.java 文件,增加 findAllUser() 方法,代码如下:

    public abstract List findAllUser();

 

修改 UserDAOImp.java 文件,增加 findAllUser() 方法,代码如下:

    public List findAllUser() {

        List users=this.getHibernateTemplate().find("from User u");

        if(users.size()>0)

            return users;

        return null;

    }

 

修改 struts-config.xml 文件

< action path = "/find" type = " org.springframework.web.struts.DelegatingActionProxy " />

 

修改 FindAction.java 文件 内容如下

    private UserDAO userDAO;

   

    public UserDAO getUserDAO() {

        return userDAO;

    }

    public void setUserDAO(UserDAO userDAO) {

        this.userDAO = userDAO;

    }

 

    public ActionForward execute(

        ActionMapping mapping,

        ActionForm form,

        HttpServletRequest request,

        HttpServletResponse response) {

        List users=userDAO.findAllUser();

        if(users!=null){

            HttpSession session=request.getSession();

            session.setAttribute("users",users);

            return mapping.findForward("displayGo");

        }

        return mapping.getInputForward();

    }

 

spring 配置文件 applicationContext.xml 中加入以下内容

       < bean name = "/find" class = "com.test.struts.action.FindAction" singleton = "false" >

       < property name = "userDAO" >

           < ref bean = "userDAOProxy" />

       </ property >

    </ bean >

 

修改 display.jsp 文件 内容如下

<%@ page language = "java" import = "java.util.*" pageEncoding = "UTF-8" %>

<%@ taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "bean" %>

<%@ taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html" %>

<%@ taglib uri = "http://jakarta.apache.org/struts/tags-logic" prefix = "logic" %>

< html >

  < head >

    < title > DISPLAY </ title >

  </ head >

  < body >

    < logic:present name = "users" >

    < form action = "del.do" >

    < table border = "1" >

    < logic:iterate id = "element" name = "users" >

    < tr >< td width = "100" >

       < input type = "radio" name = "select" value = " < bean:write name = "element" property = "id" /> " >

    < bean:write name = "element" property = "username" />

    </ td ></ tr >

    </ logic:iterate >

    </ table >

    < INPUT type = "submit" name = "Submit" value = "submit" >

    </ form >

    </ logic:present >

  </ body >

</ html >

 

现在整个 Display 已经完成,如果你觉得不放心的话可以在这里停一停先进行这一段的测试!

 

新建一个 Action 用于删除选中的用户,name:del

 

修改 struts-config.xml 文件中的以下内容为:

< action path = "/del" type = " org.springframework.web.struts.DelegatingActionProxy " />

 

修改 UserDAO.java 文件 增加以下方法

public interface UserDAO {

    public abstract boolean isValidUser(String username, String password);

    public abstract boolean addUser(User user);

    public abstract List findAllUser();

    public abstract boolean delUser(Integer lid);

}

 

修改 UserDAOImp.java 文件 添加新增的两个方法 并编写以下内容

    public boolean delUser(Integer lid) {

        try {

            User user=(User)this.getHibernateTemplate().load(User.class,lid);

            this.getHibernateTemplate().delete(user);

        } catch (Exception e) {

            System.out.println(e.getMessage());

            return false;

        }

        return true;

    }

 

修改 DelAction.java 文件 内容如下

public class DelAction extends Action {

 

    private UserDAO userDAO;

 

    public UserDAO getUserDAO() {

        return userDAO;

    }

 

    public void setUserDAO(UserDAO userDAO) {

        this.userDAO = userDAO;

    }

 

    public ActionForward execute(

        ActionMapping mapping,

        ActionForm form,

        HttpServletRequest request,

        HttpServletResponse response) {

        Integer selid=Integer.valueOf(request.getParameter("select"));

        if(userDAO.delUser(selid))

            return mapping.findForward("indexGo");

        return mapping.getInputForward();

    }

}

 

spring 配置文件 applicationContext.xml 中加入以下内容

       < bean name = "/del" class = "com.test.struts.action.DelAction" singleton = "false" >

       < property name = "userDAO" >

           < ref bean = "userDAOProxy" />

       </ property >

    </ bean >

 

修改 index.jsp 文件

< html >

  < head >

    < title > Struts+Spring+Hibernate </ title >

  </ head >

 

  < body >

    < a href = "login.jsp" > Login </ a >

    < br >

    < a href = "add.jsp" > Add User </ a >

    <br>

    <a href="find.do">Display</a>

  </ body >

</ html >

 

 

代码已经全部完成,可以进行测试了!!!

 

在添加用户的过程中出现了错误,就是没有验证就直接提交了,并能够提交空的用户!

这里解决的方式有多种,修改程是肯定的了,还有就是在设计数据库的时候加上限制如将 username 和 password 为字设置为 not null

 

将修改这一段

< form-bean name = "addForm" type = "org.apache.struts.action.DynaActionForm" >

      < form-property name = "password" type = "java.lang.String" />

      < form-property name = "username" type = "java.lang.String" />

    </ form-bean >

 

修改后

< form-bean name = "addForm" type = " org.apache.struts.validator.DynaValidatorForm " >

      < form-property name = "password" type = "java.lang.String" />

      < form-property name = "username" type = "java.lang.String" />

    </ form-bean >

 

可以了,这集就讲到这里了!下集再见!