struts2释出已经很久了,虽然自己现在作GUI开发,不过有时间还是学习下web开发,现在就将我使用myeclipse工具应用struts2 + spring + hibernate 实现CRUD操作的步骤一一纪录下来,为初学者少走弯路略尽绵薄之力!
首先,myeclipse中web工程目录结构如下图:
使用myeclipse开发hibernate和spring的操作我就不详细说了,网上的教程很多,如果有不明白的可以咨询我,呵呵.
其中持久类AbstractTest,Test,TestDAO,Test.hbm.xml都是myeclipse的hibernate工具生成的.TestAction类是struts2的核心处理类,代码如下:
applicationContext.xml 主要是工具生成 的,只是将配置文件路径改下,代码如下:
struts.xml (变化不大,写过以前struts配置文件的不难理解)
struts.properties
web.xml
list.jsp
edit.jsp
首先,myeclipse中web工程目录结构如下图:
使用myeclipse开发hibernate和spring的操作我就不详细说了,网上的教程很多,如果有不明白的可以咨询我,呵呵.
其中持久类AbstractTest,Test,TestDAO,Test.hbm.xml都是myeclipse的hibernate工具生成的.TestAction类是struts2的核心处理类,代码如下:
package
com.yangqiang.strutsdemo.web;
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;
import com.yangqiang.strutsdemo.domain.Test;
import com.yangqiang.strutsdemo.domain.TestDAO;
/** */ /**
* 描述:
* @author Stone yang 创建日期:2007-4-24
* @version pattern Study
* 技术支持: <a href="http://blog.csdn.net/yq76034150">http://blog.csdn.net/yq76034150</a>
*/
public class TestAction extends ActionSupport ... {
private static final Logger log = Logger.getLogger(TestAction.class);
private Integer id;
private Integer[] ids;
protected TestDAO testDao;
private Test test;
private Collection<Test> testColl;
/** *//**
* 描述 return 返回 ids。
* @author Stone yang
* @date 2007-4-24
*/
public Integer[] getIds() ...{
return ids;
}
/** *//**
* 描述:设置ids的值。
* @param ids
* @author Stone yang
* @date 2007-4-24
*/
public void setIds(Integer[] ids) ...{
this.ids = ids;
}
/** *//**
* 描述 return 返回 testColl。
* @author Stone yang
* @date 2007-4-24
*/
public Collection<Test> getTestColl() ...{
return testColl;
}
/** *//**
* 描述:设置testColl的值。
* @param testColl
* @author Stone yang
* @date 2007-4-24
*/
public void setTestColl(Collection<Test> testColl) ...{
this.testColl = testColl;
}
/** *//**
* 描述 return 返回 id。
* @author Stone yang
* @date 2007-4-24
*/
public Integer getId() ...{
return id;
}
/** *//**
* 描述:设置id的值。
* @param id
* @author Stone yang
* @date 2007-4-24
*/
public void setId(Integer id) ...{
this.id = id;
}
/** *//**
* 描述 return 返回 testDao。
* @author Stone yang
* @date 2007-4-24
*/
public TestDAO getTestDao() ...{
return testDao;
}
/** *//**
* 描述:设置testDao的值。
* @param testDao
* @author Stone yang
* @date 2007-4-24
*/
public void setTestDao(TestDAO testDao) ...{
this.testDao = testDao;
}
/** *//**
* 描述 return 返回 test。
* @author Stone yang
* @date 2007-4-24
*/
public Test getTest() ...{
return test;
}
/** *//**
* 描述:设置test的值。
* @param test
* @author Stone yang
* @date 2007-4-24
*/
public void setTest(Test test) ...{
this.test = 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().merge(test);
return SUCCESS;
}
public String remove() ...{
for (int i = 0, size = ids.length; i < size; i++) ...{
getTestDao().delete(getTestDao().findById(ids[i]));
}
return SUCCESS;
}
}
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;
import com.yangqiang.strutsdemo.domain.Test;
import com.yangqiang.strutsdemo.domain.TestDAO;
/** */ /**
* 描述:
* @author Stone yang 创建日期:2007-4-24
* @version pattern Study
* 技术支持: <a href="http://blog.csdn.net/yq76034150">http://blog.csdn.net/yq76034150</a>
*/
public class TestAction extends ActionSupport ... {
private static final Logger log = Logger.getLogger(TestAction.class);
private Integer id;
private Integer[] ids;
protected TestDAO testDao;
private Test test;
private Collection<Test> testColl;
/** *//**
* 描述 return 返回 ids。
* @author Stone yang
* @date 2007-4-24
*/
public Integer[] getIds() ...{
return ids;
}
/** *//**
* 描述:设置ids的值。
* @param ids
* @author Stone yang
* @date 2007-4-24
*/
public void setIds(Integer[] ids) ...{
this.ids = ids;
}
/** *//**
* 描述 return 返回 testColl。
* @author Stone yang
* @date 2007-4-24
*/
public Collection<Test> getTestColl() ...{
return testColl;
}
/** *//**
* 描述:设置testColl的值。
* @param testColl
* @author Stone yang
* @date 2007-4-24
*/
public void setTestColl(Collection<Test> testColl) ...{
this.testColl = testColl;
}
/** *//**
* 描述 return 返回 id。
* @author Stone yang
* @date 2007-4-24
*/
public Integer getId() ...{
return id;
}
/** *//**
* 描述:设置id的值。
* @param id
* @author Stone yang
* @date 2007-4-24
*/
public void setId(Integer id) ...{
this.id = id;
}
/** *//**
* 描述 return 返回 testDao。
* @author Stone yang
* @date 2007-4-24
*/
public TestDAO getTestDao() ...{
return testDao;
}
/** *//**
* 描述:设置testDao的值。
* @param testDao
* @author Stone yang
* @date 2007-4-24
*/
public void setTestDao(TestDAO testDao) ...{
this.testDao = testDao;
}
/** *//**
* 描述 return 返回 test。
* @author Stone yang
* @date 2007-4-24
*/
public Test getTest() ...{
return test;
}
/** *//**
* 描述:设置test的值。
* @param test
* @author Stone yang
* @date 2007-4-24
*/
public void setTest(Test test) ...{
this.test = 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().merge(test);
return SUCCESS;
}
public String remove() ...{
for (int i = 0, size = ids.length; i < size; i++) ...{
getTestDao().delete(getTestDao().findById(ids[i]));
}
return SUCCESS;
}
}
applicationContext.xml 主要是工具生成 的,只是将配置文件路径改下,代码如下:
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="struts"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="configLocation" >
<!-- 改变了一下 -->
< value > /WEB-INF/classes/hibernate.cfg.xml </ value >
</ property >
</ bean >
< bean id ="testDao"
class ="com.yangqiang.strutsdemo.domain.TestDAO" >
< property name ="sessionFactory" >
< ref bean ="struts" />
</ property >
</ bean ></ beans >
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="struts"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="configLocation" >
<!-- 改变了一下 -->
< value > /WEB-INF/classes/hibernate.cfg.xml </ value >
</ property >
</ bean >
< bean id ="testDao"
class ="com.yangqiang.strutsdemo.domain.TestDAO" >
< property name ="sessionFactory" >
< ref bean ="struts" />
</ property >
</ bean ></ beans >
struts.xml (变化不大,写过以前struts配置文件的不难理解)
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="struts_crud" extends ="struts-default" namespace ="/test" >
< action name ="List" class ="com.yangqiang.strutsdemo.web.TestAction" method ="list" >
< result > list.jsp </ result >
</ action >
< action name ="Edit" class ="com.yangqiang.strutsdemo.web.TestAction" method ="load" >
< result > edit.jsp </ result >
</ action >
< action name ="Store" class ="com.yangqiang.strutsdemo.web.TestAction" method ="store" >
< result type ="redirect" > List.action </ result >
</ action >
< action name ="Remove" class ="com.yangqiang.strutsdemo.web.TestAction" method ="remove" >
< result type ="redirect" > List.action </ result >
</ action >
</ package >
</ struts >
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="struts_crud" extends ="struts-default" namespace ="/test" >
< action name ="List" class ="com.yangqiang.strutsdemo.web.TestAction" method ="list" >
< result > list.jsp </ result >
</ action >
< action name ="Edit" class ="com.yangqiang.strutsdemo.web.TestAction" method ="load" >
< result > edit.jsp </ result >
</ action >
< action name ="Store" class ="com.yangqiang.strutsdemo.web.TestAction" method ="store" >
< result type ="redirect" > List.action </ result >
</ action >
< action name ="Remove" class ="com.yangqiang.strutsdemo.web.TestAction" method ="remove" >
< result type ="redirect" > List.action </ result >
</ action >
</ package >
</ struts >
struts.properties
struts.objectFactory
=
spring
web.xml
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app version ="2.4"
xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< display-name > Struts2 crud 例程 </ display-name >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value > classpath*:*.xml </ param-value >
</ context-param >
< listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< filter >
< filter-name > struts2 </ filter-name >
< filter-class >
org.apache.struts2.dispatcher.FilterDispatcher
</ filter-class >
</ filter >
< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
< welcome-file-list >
< welcome-file > list.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
< web-app version ="2.4"
xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< display-name > Struts2 crud 例程 </ display-name >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value > classpath*:*.xml </ param-value >
</ context-param >
< listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< filter >
< filter-name > struts2 </ filter-name >
< filter-class >
org.apache.struts2.dispatcher.FilterDispatcher
</ filter-class >
</ filter >
< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
< welcome-file-list >
< welcome-file > list.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
list.jsp
<%
...
@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"
%>
<% ... @ taglib prefix="s" uri="/struts-tags" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > Book List </ title >
< style type ="text/css" > ...
table {...}{
border: 1px solid black;
border-collapse: collapse;
}
table thead tr th {...}{
border: 1px solid black;
padding: 3px;
background-color: #cccccc;
}
table tbody tr td {...}{
border: 1px solid black;
padding: 3px;
}
</ style >
</ head >
< body >
< h2 > Book List </ h2 >
< s:form action ="Remove" theme ="simple" >
< table cellspacing ="0" >
< 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 href ='<s:url action ="Edit" >< s:param name ="id" value ="id" /></ s:url > '>
Edit
</ a >
< a href ='<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 href ="edit.jsp" > Add Test </ a >
</ s:form >
</ body >
</ html >
<% ... @ taglib prefix="s" uri="/struts-tags" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > Book List </ title >
< style type ="text/css" > ...
table {...}{
border: 1px solid black;
border-collapse: collapse;
}
table thead tr th {...}{
border: 1px solid black;
padding: 3px;
background-color: #cccccc;
}
table tbody tr td {...}{
border: 1px solid black;
padding: 3px;
}
</ style >
</ head >
< body >
< h2 > Book List </ h2 >
< s:form action ="Remove" theme ="simple" >
< table cellspacing ="0" >
< 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 href ='<s:url action ="Edit" >< s:param name ="id" value ="id" /></ s:url > '>
Edit
</ a >
< a href ='<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 href ="edit.jsp" > Add Test </ a >
</ s:form >
</ body >
</ html >
edit.jsp
<%
...
@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"
%>
<% ... @ taglib prefix="s" uri="/struts-tags" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > Book </ title >
</ head >
< body >
< h2 >
< s:if test ="null == test" >
Add Book
</ s:if >
< s:else >
Edit Book
</ s:else >
</ h2 >
< s:form action ="Store" >
< s:hidden name ="test.id" label ="ID" />
< s:textfield name ="test.name" label ="书名" />
< s:textfield name ="test.author" label ="作者" />
< s:submit />
</ s:form >
</ body >
</ html >
<% ... @ taglib prefix="s" uri="/struts-tags" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > Book </ title >
</ head >
< body >
< h2 >
< s:if test ="null == test" >
Add Book
</ s:if >
< s:else >
Edit Book
</ s:else >
</ h2 >
< s:form action ="Store" >
< s:hidden name ="test.id" label ="ID" />
< s:textfield name ="test.name" label ="书名" />
< s:textfield name ="test.author" label ="作者" />
< s:submit />
</ s:form >
</ body >
</ html >