天天看点

struts2+spring+hibernate实现ARDU功能(ssh整合)

         学了一个星期的struts2收获还是不少啊,因为本人也是得到高手的指点才得意那么快完整一个简单的ARDU基本功能列表,这不刚弄完就迫不及待的发布出来跟大家分享。现在我发现写博客的好处了,就是一来可以给自己的学的东西作为一个笔记整体,二来还可以和大家讨论讨论。因为我本人在网上找struts2相关的学习资料实在是少,所以就想到如果我自己会的东西,而网上资料很少,就应该共享出来给大家,以免也有人跟我一样在黑暗中摸索着,如果没人指点,学东西遇到困难就很容易放弃,希望大家不要见笑。也许我这些东西在有些看来并不是什么可以炫耀的东西,但我的目的知识为了让那些跟我同样的学者有个方便之处。

         好了,闲话就不多说,由于这里没法把东西上传上去,所以只能把代码一步一步的贴上来了。

        从这个例子中,大家可以明显看到struts2的突出优点,实现零配置的愿望就为期不远了,所以那些跟我一样怕配置的学者,这真是个值得高兴的事。而且在整个程序中贯串了注解这样一个简便的方法,确实是非常方便,省了许多的后台配置。

         首先从web.xml来看吧:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 配置spring的监听器 -->

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath*:org/test/spring/applicationContext*.xml</param-value>

    </context-param>

    <!-- 开启监听 -->

    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

    <!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->

    <filter>

        <filter-name>lazyLoadingFilter</filter-name>

        <filter-class>

            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

        </filter-class>

    </filter>

    <!-- 设置监听加载上下文 -->

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>

            org.apache.struts2.dispatcher.FilterDispatcher

        </filter-class>

        <init-param>

   <param-name>actionPackages</param-name>

   <param-value>org.test.action</param-value>

  </init-param> 

    </filter>

    <filter-mapping>

    <filter-name>lazyLoadingFilter</filter-name>

    <url-pattern>*.action</url-pattern>

    </filter-mapping>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>

  return list;

 }

 @Transactional(readOnly = true)

 public Book getBookById(int id) {

  System.out.println("book id:" + id);

  return dao.get(id);

 }

 public void savebook(Book book) throws Exception {

  dao.save(book);

 }

 public void delbook(Book book) throws Exception {

  dao.delete(book);

 }

}

package org.test.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;

import org.test.dao.ITestDAO;

import org.test.vo.User;

public class LoginService {

 private ITestDAO itestdao;

 public void setItestdao(ITestDAO itestdao) {

  this.itestdao = itestdao;

 }

 public boolean userlogin(User user) throws Exception {

  boolean flag = false;

  String name = user.getUsername();

  String pwd = user.getPassword();

  String sql = "from User as t where t.username = '" + name

    + "' and t.password = '" + pwd + "'";

  List<User> list = (List<User>) itestdao.query(sql); 

  if (list != null && list.size() > 0) {

   return true;

  } else {

   return flag;

  }

 }

}

org.test.dao包下:

package org.test.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BookDAO extends HibernateDaoSupport implements ITestBook{

 public boolean AddObject(Object obj) throws Exception { 

  this.getHibernateTemplate().save(obj);

  return true;

 }

 public boolean DelObject(Object obj) throws Exception {

  this.getHibernateTemplate().delete(obj);

  return true;

 }

 public Object query(String HQL) throws Exception {

  List list = this.getHibernateTemplate().find(HQL);

  return list;

 }

}

package org.test.dao;

public interface ITestBook {

 public Object query(String HQL) throws Exception ;

 public boolean AddObject(Object obj) throws Exception ;

 public boolean DelObject(Object obj) throws Exception ;

}

package org.test.dao;

public interface ITestDAO {

 public Object query(String HQL) throws Exception ;

}

package org.test.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class TestDAO extends HibernateDaoSupport implements ITestDAO{

 public Object query(String HQL) throws Exception {

  List list = this.getHibernateTemplate().find(HQL);

  return list;

 }

}

其实这里的bookdao在程序中没有用到,因为在后面我发现了在一个包里提供了一个DAO,非常的实用,所以我全部继续这个了,具体的在BookService,java文件中能找到,具体包的请参看springside3-core-3.0.3.jar中的SimpleHIbernateTemplate.class(非常重要的一个类);

这是信息显示列表页面:

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

<%@ taglib prefix="s" uri="/struts-tags"%>

<%

 String path = request.getContextPath();

 String basePath = request.getScheme() + "://"

   + request.getServerName() + ":" + request.getServerPort()

   + path + "/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

 <head>

  <base href="<%=basePath%>" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >

  <title>index</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" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >

 -->

 </head>

 <body>

  <s:form action="list.action">

   <table width="700" height="600" align="center">

    <tr>

     <td align="center" valign="middle">

      <table width="700" height="200" >

       <tr align="center" valign="middle">

        <td width="100">

        </td>

        <td width="100">

        </td>

        <td colspan="3">

         图书基本信息列表

        </td>

        <td width="100">

        </td>

        <td width="100">

        </td>

       </tr>

       <tr align="center" valign="middle">

        <td width="100">

        </td>

        <td colspan="2">

         目前登陆在线的用户:

        </td>

        <td width="100">

         ${sessionScope.userbean.username}

        </td>

        <td width="100">

        </td>

        <td width="100">

         <a href="book-input.jsp" target="_blank" rel="external nofollow" >新增数据</a>

        </td>

        <td width="100">

        </td>

       </tr>

       <tr align="center" valign="middle">

        <td width="100">

         选择

        </td>

        <td width="100">

         图书编号

        </td>

        <td width="100">

         作者

        </td>

        <td width="100">

         书名

        </td>

        <td width="100">

         价格

        </td>

        <td width="100">

         修改

        </td>

        <td width="100">

         删除

        </td>

       </tr>

       <s:iterator  value="booklist" id="b">

        <tr align="center" valign="middle">

         <td width="100">

          <input type="checkbox" name="id" value='<s:property value="#b.bookid" />' />

         </td>

         <td width="100">

          <s:property value="#b.bookid"/>

         </td>

         <td width="100">

          <s:property value="#b.author"/>

         </td>

         <td width="100">

          <s:property value="#b.bookname"/>

         </td>

         <td width="100">

          <s:property value="#b.price"/>

         </td>

         <td width="100">

          <a href='book!input.action?id=${b.bookid}'>修改</a>

         </td>

         <td width="100">

          <a href='book!delete.action?id=${b.bookid}'>删除</a>

         </td>

        </tr>

       </s:iterator>

      </table>

     </td>

    </tr>

   </table>

  </s:form>

 </body>

</html>

修改和添加页面:

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

<%@ taglib prefix="s" uri="/struts-tags"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >

    <title>My JSP 'add.jsp' starting page</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" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >

 -->

  </head>

  <body>

    <s:form action="book!save.action">

     <s:if test="id != null"><input type="hidden" name="id" value="${param.id }"/></s:if>

       <s:textfield name="book.author" label="作者"></s:textfield>

       <s:textfield name="book.bookname" label="书名"></s:textfield>

       <s:textfield name="book.price" label="价格"></s:textfield>

       <s:submit name="提交"></s:submit>

    </s:form>

  </body>

</html>

登陆页面:

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

<%@ taglib prefix="s" uri="/struts-tags"%>

<%

 String path = request.getContextPath();

 String basePath = request.getScheme() + "://"

   + request.getServerName() + ":" + request.getServerPort()

   + path + "/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

 <head>

  <base href="<%=basePath%>" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >

  <title>My JSP 'login.jsp' starting page</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" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >

 -->

 </head>

 <body>

     <h1>

   用户登陆

  </h1>

      <s:form action="login.action">  

  <s:textfield name="user.username" label="username"></s:textfield>

  <s:password name="user.password" label="password"></s:password>

  <s:submit name="submit"></s:submit>

   </s:form>

 </body>

</html>

这个例子我会上传到csdn里面去。具体的配置如上,不过看懂估计比较难,希望这是一个过度。