天天看點

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裡面去。具體的配置如上,不過看懂估計比較難,希望這是一個過度。