熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

struts2 + spring + hibernate&

2013-11-23 20:16:15  來源: Java開源技術 
    struts釋出已經很久了雖然自己現在作GUI開發不過有時間還是學習下web開發現在就將我使用myeclipse工具應用struts + spring + hibernate 實現CRUD操作的步驟一一紀錄下來為初學者少走彎路略盡綿薄之力!
       首先myeclipse中web工程目錄結構如下圖:


       使用myeclipse開發hibernate和spring的操作我就不詳細說了網上的教程很多如果有不明白的可以咨詢我呵呵
      其中持久類AbstractTestTestTestDAOTesthbmxml都是myeclipse的hibernate工具生成的TestAction類是struts的核心處理類代碼如下:

package comyangqiangstrutsdemoweb;

import javautilCollection;
import javautilList;

import orgapachelogjLogger;

import comopensymphonyxworkActionSupport;
import comyangqiangstrutsdemodomainTest;
import comyangqiangstrutsdemodomainTestDAO;

/** *//**
 * 描述:
 * @author Stone yang  創建日期: 
 * @version pattern Study
 * 技術支持: <a >;/a>
 */
public class TestAction extends ActionSupport {
    private static final Logger log = LoggergetLogger(TestActionclass);

    private Integer id;
    private Integer[] ids;
    protected TestDAO testDao;
    private Test test;
    private Collection<Test> testColl;
    
    /** *//**
     * 描述 return 返回 ids
     * @author Stone yang
     * @date 
     */
    public Integer[] getIds() {
        return ids;
    }
    /** *//**
     * 描述:設置ids的值
     * @param ids
     * @author Stone yang
     * @date 
     */
    public void setIds(Integer[] ids) {
        thisids = ids;
    }
    /** *//**
     * 描述 return 返回 testColl
     * @author Stone yang
     * @date 
     */
    public Collection<Test> getTestColl() {
        return testColl;
    }
    /** *//**
     * 描述:設置testColl的值
     * @param testColl
     * @author Stone yang
     * @date 
     */
    public void setTestColl(Collection<Test> testColl) {
        thistestColl = testColl;
    }
    /** *//**
     * 描述 return 返回 id
     * @author Stone yang
     * @date 
     */
    public Integer getId() {
        return id;
    }
    /** *//**
     * 描述:設置id的值
     * @param id
     * @author Stone yang
     * @date 
     */
    public void setId(Integer id) {
        thisid = id;
    }
    /** *//**
     * 描述 return 返回 testDao
     * @author Stone yang
     * @date 
     */
    public TestDAO getTestDao() {
        return testDao;
    }
    /** *//**
     * 描述:設置testDao的值
     * @param testDao
     * @author Stone yang
     * @date 
     */
    public void setTestDao(TestDAO testDao) {
        thistestDao = testDao;
    }
    /** *//**
     * 描述 return 返回 test
     * @author Stone yang
     * @date 
     */
    public Test getTest() {
        return test;
    }
    /** *//**
     * 描述:設置test的值
     * @param test
     * @author Stone yang
     * @date 
     */
    public void setTest(Test test) {
        thistest = test;
    }
    
    public String load() {
        test = getTestDao()findById(id);
           return SUCCESS;
       }

       @SuppressWarnings(unchecked)
    public String list() {
           testColl = getTestDao()findByExample(new Test());
           return SUCCESS;
       }
           
       public String store() {
           getTestDao(rge(test);
           return SUCCESS;
       }
       
       public String remove() {
        for (int i =  size = idslength; i < size; i++) {
            getTestDao()delete(getTestDao()findById(ids[i]));
        }
        return SUCCESS;
    }

    
}

  applicationContextxml 主要是工具生成 的只是將配置文件路徑改下代碼如下:



<?xml version= encoding=UTF?>
<!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>

<beans>


    <bean id=struts
        class=orgspringframeworkormhibernateLocalSessionFactoryBean>
        <property name=configLocation>
                        <! 改變了一下>
            <value>/WEBINF/classes/hibernatecfgxml</value>
        </property>
    </bean>
    <bean id=testDao
        class=comyangqiangstrutsdemodomainTestDAO>
        <property name=sessionFactory>
            <ref bean=struts />
        </property>
    </bean></beans>

strutsxml  (變化不大寫過以前struts配置文件的不難理解)


<?xml version= encoding=UTF?>

<!DOCTYPE struts PUBLIC
    //Apache Software Foundation//DTD Struts Configuration //EN
    dtd>

<struts>
    <package name=struts_crud extends=strutsdefault namespace=/test>
        <action name=List class=comyangqiangstrutsdemowebTestAction method=list>
            <result>listjsp</result>
        </action>
        <action name=Edit class=comyangqiangstrutsdemowebTestAction method=load>
            <result>editjsp</result>
        </action>
        <action name=Store class=comyangqiangstrutsdemowebTestAction method=store>
            <result type=redirect>Listaction</result>
        </action>
        <action name=Remove class=comyangqiangstrutsdemowebTestAction method=remove>
            <result type=redirect>Listaction</result>
        </action>
    </package>
</struts>
strutsproperties


strutsobjectFactory = spring
webxml


<?xml version= encoding=UTF?>
<webapp version= 
    xmlns= 
    xmlns:xsi=instance 
    xsi:schemaLocation=;
    app__xsd>
    <displayname>Struts crud 例程</displayname>
    
    <contextparam>
        <paramname>contextConfigLocation</paramname>
        <paramvalue>classpath*:*xml</paramvalue>
    </contextparam>
    
    <listener>
        <listenerclass>orgsprntextContextLoaderListener</listenerclass>
    </listener>
    
    <filter>
        <filtername>struts</filtername>
        <filterclass>
            orgapachestrutsdispatcherFilterDispatcher
        </filterclass>
    </filter>

    <filtermapping>
        <filtername>struts</filtername>
        <urlpattern>/*</urlpattern>
    </filtermapping>

    <welcomefilelist>
        <welcomefile>listjsp</welcomefile>
    </welcomefilelist>
</webapp>

listjsp




<%@ page language=java contentType=text/html; charset=utf pageEncoding=utf %>
<%@ taglib prefix=s uri=/strutstags %>

<!DOCTYPE html PUBLIC //WC//DTD XHTML  Transitional//EN transitionaldtd>
<html xmlns=>
<head>
    <title>Book List</title>
    <style type=text/css>
        table {}{
            border: px solid black;
            bordercollapse: collapse;
        }
        
        table thead tr th {}{
            border: px solid black;
            padding: px;
            backgroundcolor: #cccccc;
        }
        
        table tbody tr td {}{
            border: px solid black;
            padding: px;
        }
    </style>
</head>
<body>    
    <h>Book List</h>
    <s:form action=Remove theme=simple>
        <table cellspacing=>
            <thead>
                <tr>
                    <th>勾選</th>
                    <th>ID</th>
                    <th>名稱</th>
                    <th>作者</th>
                </tr>
            </thead>
            <tbody>
                <s:iterator value=testColl>
                    <tr>
                        <td><input type=checkbox name=ids value=<s:property value=id /> /></td>
                        <td><s:property value=id /></td>
                        <td><s:property value=name /></td>
                        <td><s:property value=author /></td>
                        <td>
                            <a <s:url action=Edit><s:param name=id value=id /></s:url>>
                                Edit
                            </a>
                            &nbsp;
                            <a <s:url action=Remove><s:param name=ids value=id /></s:url>>
                                Delete
                            </a>
                        </td>
                    </tr>
                </s:iterator>
            </tbody>
        </table>
        <s:submit value=Remove /><a >Add Test</a>
    </s:form>    
</body>
</html>
editjsp


<%@ page language=java contentType=text/html; charset=utf pageEncoding=utf %>
<%@ taglib prefix=s uri=/strutstags %>

<!DOCTYPE html PUBLIC //WC//DTD XHTML  Transitional//EN transitionaldtd>
<html xmlns=>
<head>
    <title>Book</title>
</head>
<body>    
    <h>
        <s:if test=null == test>
            Add Book
        </s:if>
        <s:else>
            Edit Book
        </s:else>
    </h>
    <s:form action=Store >
        <s:hidden name=testid label=ID />
        <s:textfield name=testname label=書名 />
        <s:textfield name=testauthor label=作者 />
        <s:submit />
    </s:form>
</body>
</html>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28215.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.