java

位置:IT落伍者 >> java >> 浏览文章

struts2 + spring + hibernate&


发布日期:2021年03月02日
 
struts2 + spring + hibernate&
struts释出已经很久了虽然自己现在作GUI开发不过有时间还是学习下web开发现在就将我使用myeclipse工具应用struts + spring + hibernate 实现CRUD操作的步骤一一纪录下来为初学者少走弯路略尽绵薄之力!

首先myeclipse中web工程目录结构如下图:

使用myeclipse开发hibernate和spring的操作我就不详细说了网上的教程很多如果有不明白的可以咨询我呵呵

其中持久类AbstractTestTestTestDAOTesthbmxml都是myeclipse的hibernate工具生成的TestAction类是struts的核心处理类代码如下:

packagecomyangqiangstrutsdemoweb;

importjavautilCollection;

importjavautilList;

importorgapachelogjLogger;

importcomopensymphonyxworkActionSupport;

importcomyangqiangstrutsdemodomainTest;

importcomyangqiangstrutsdemodomainTestDAO;

/***//**

*描述:

*@authorStoneyang创建日期:

*@versionpatternStudy

*技术支持:<a>;/a>

*/

publicclassTestActionextendsActionSupport{

privatestaticfinalLoggerlog=LoggergetLogger(TestActionclass);

privateIntegerid;

privateInteger[]ids;

protectedTestDAOtestDao;

privateTesttest;

privateCollection<Test>testColl;

/***//**

*描述return返回ids

*@authorStoneyang

*@date

*/

publicInteger[]getIds(){

returnids;

}

/***//**

*描述:设置ids的值

*@paramids

*@authorStoneyang

*@date

*/

publicvoidsetIds(Integer[]ids){

thisids=ids;

}

/***//**

*描述return返回testColl

*@authorStoneyang

*@date

*/

publicCollection<Test>getTestColl(){

returntestColl;

}

/***//**

*描述:设置testColl的值

*@paramtestColl

*@authorStoneyang

*@date

*/

publicvoidsetTestColl(Collection<Test>testColl){

thistestColl=testColl;

}

/***//**

*描述return返回id

*@authorStoneyang

*@date

*/

publicIntegergetId(){

returnid;

}

/***//**

*描述:设置id的值

*@paramid

*@authorStoneyang

*@date

*/

publicvoidsetId(Integerid){

thisid=id;

}

/***//**

*描述return返回testDao

*@authorStoneyang

*@date

*/

publicTestDAOgetTestDao(){

returntestDao;

}

/***//**

*描述:设置testDao的值

*@paramtestDao

*@authorStoneyang

*@date

*/

publicvoidsetTestDao(TestDAOtestDao){

thistestDao=testDao;

}

/***//**

*描述return返回test

*@authorStoneyang

*@date

*/

publicTestgetTest(){

returntest;

}

/***//**

*描述:设置test的值

*@paramtest

*@authorStoneyang

*@date

*/

publicvoidsetTest(Testtest){

thistest=test;

}

publicStringload(){

test=getTestDao()findById(id);

returnSUCCESS;

}

@SuppressWarnings(unchecked)

publicStringlist(){

testColl=getTestDao()findByExample(newTest());

returnSUCCESS;

}

publicStringstore(){

getTestDao(rge(test);

returnSUCCESS;

}

publicStringremove(){

for(inti=size=idslength;i<size;i++){

getTestDao()delete(getTestDao()findById(ids[i]));

}

returnSUCCESS;

}

}

applicationContextxml 主要是工具生成 的只是将配置文件路径改下代码如下:

<?xmlversion=encoding=UTF?>

<!DOCTYPEbeansPUBLIC//SPRING//DTDBEAN//ENbeansdtd>

<beans>

<beanid=struts

class=orgspringframeworkormhibernateLocalSessionFactoryBean>

<propertyname=configLocation>

<!改变了一下>

<value>/WEBINF/classes/hibernatecfgxml</value>

</property>

</bean>

<beanid=testDao

class=comyangqiangstrutsdemodomainTestDAO>

<propertyname=sessionFactory>

<refbean=struts/>

</property>

</bean></beans>

strutsxml (变化不大写过以前struts配置文件的不难理解)

<?xmlversion=encoding=UTF?>

<!DOCTYPEstrutsPUBLIC

//ApacheSoftwareFoundation//DTDStrutsConfiguration//EN

dtd>

<struts>

<packagename=struts_crudextends=strutsdefaultnamespace=/test>

<actionname=Listclass=comyangqiangstrutsdemowebTestActionmethod=list>

<result>listjsp</result>

</action>

<actionname=Editclass=comyangqiangstrutsdemowebTestActionmethod=load>

<result>editjsp</result>

</action>

<actionname=Storeclass=comyangqiangstrutsdemowebTestActionmethod=store>

<resulttype=redirect>Listaction</result>

</action>

<actionname=Removeclass=comyangqiangstrutsdemowebTestActionmethod=remove>

<resulttype=redirect>Listaction</result>

</action>

</package>

</struts>

strutsproperties

strutsobjectFactory=spring

webxml

<?xmlversion=encoding=UTF?>

<webappversion=

xmlns=

xmlns:xsi=instance

xsi:schemaLocation=;

app__xsd>

<displayname>Strutscrud例程</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

<%@pagelanguage=javacontentType=text/html;charset=utfpageEncoding=utf%>

<%@taglibprefix=suri=/strutstags%>

<!DOCTYPEhtmlPUBLIC//WC//DTDXHTMLTransitional//ENtransitionaldtd>

<htmlxmlns=>

<head>

<title>BookList</title>

<styletype=text/css>

table{}{

border:pxsolidblack;

bordercollapse:collapse;

}

tabletheadtrth{}{

border:pxsolidblack;

padding:px;

backgroundcolor:#cccccc;

}

tabletbodytrtd{}{

border:pxsolidblack;

padding:px;

}

</style>

</head>

<body>

<h>BookList</h>

<s:formaction=Removetheme=simple>

<tablecellspacing=>

<thead>

<tr>

<th>勾选</th>

<th>ID</th>

<th>名称</th>

<th>作者</th>

</tr>

</thead>

<tbody>

<s:iteratorvalue=testColl>

<tr>

<td><inputtype=checkboxname=idsvalue=<s:propertyvalue=id/>/></td>

<td><s:propertyvalue=id/></td>

<td><s:propertyvalue=name/></td>

<td><s:propertyvalue=author/></td>

<td>

<a<s:urlaction=Edit><s:paramname=idvalue=id/></s:url>>

Edit

</a>

&nbsp;

<a<s:urlaction=Remove><s:paramname=idsvalue=id/></s:url>>

Delete

</a>

</td>

</tr>

</s:iterator>

</tbody>

</table>

<s:submitvalue=Remove/><a>AddTest</a>

</s:form>

</body>

</html>

editjsp

<%@pagelanguage=javacontentType=text/html;charset=utfpageEncoding=utf%>

<%@taglibprefix=suri=/strutstags%>

<!DOCTYPEhtmlPUBLIC//WC//DTDXHTMLTransitional//ENtransitionaldtd>

<htmlxmlns=>

<head>

<title>Book</title>

</head>

<body>

<h>

<s:iftest=null==test>

AddBook

</s:if>

<s:else>

EditBook

</s:else>

</h>

<s:formaction=Store>

<s:hiddenname=testidlabel=ID/>

<s:textfieldname=testnamelabel=书名/>

<s:textfieldname=testauthorlabel=作者/>

<s:submit/>

</s:form>

</body>

</html>

               

上一篇:Spring声明式事务管理源码解读之事务开始

下一篇:五步解释Struts处理请求的过程