分享
 
 
 

学习Struts+spring+hibernate的笔记

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

(快刀浪子原创)下面是我学习Struts+spring+hibernate的笔记.

1.WEB.XML的配置:

首先在web.xml中加入:

java代码:

1

2 <servlet>

3 <servlet-name>SpringContextServlet</servlet-name>

4 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

5 <load-on-startup>1</load-on-startup>

6 </servlet>

7

我的整个web.xml象这样:

java代码:

1

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

3 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

4 <web-app>

5 <display-name>info_web</display-name>

6 <context-param>

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

8 <param-value>/WEB-INF/applicationContext-hibernate.xml</param-value>

9 </context-param>

10 <filter>

11 <filter-name>SetCharacterEncoding</filter-name>

12 <filter-class>iclass.util.SetEncodingFilter</filter-class>

13 <init-param>

14 <param-name>encoding</param-name>

15 <param-value>GBK</param-value>

16 </init-param>

17 </filter>

18 <filter>

19 <filter-name>FilterRedirector</filter-name>

20 <filter-class>org.apache.cactus.server.FilterTestRedirector</filter-class>

21 </filter>

22 <filter-mapping>

23 <filter-name>SetCharacterEncoding</filter-name>

24 <url-pattern>/*</url-pattern>

25 </filter-mapping>

26 <filter-mapping>

27 <filter-name>FilterRedirector</filter-name>

28 <url-pattern>/FilterRedirector</url-pattern>

29 </filter-mapping>

30 <servlet>

31 <servlet-name>SpringContextServlet</servlet-name>

32 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

33 <load-on-startup>1</load-on-startup>

34 </servlet>

35 <servlet>

36 <servlet-name>ServletRedirector</servlet-name>

37 <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>

38 </servlet>

39 <servlet>

40 <servlet-name>JspRedirector</servlet-name>

41 <jsp-file>/jspRedirector.jsp</jsp-file>

42 </servlet>

43 <servlet>

44 <servlet-name>action</servlet-name>

45 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

46 <init-param>

47 <param-name>config</param-name>

48 <param-value>/WEB-INF/struts-config.xml</param-value>

49 </init-param>

50 <init-param>

51 <param-name>debug</param-name>

52 <param-value>2</param-value>

53 </init-param>

54 <init-param>

55 <param-name>application</param-name>

56 <param-value>ApplicationResources</param-value>

57 </init-param>

58 <load-on-startup>2</load-on-startup>

59 </servlet>

60 <servlet-mapping>

61 <servlet-name>ServletRedirector</servlet-name>

62 <url-pattern>/ServletRedirector</url-pattern>

63 </servlet-mapping>

64 <servlet-mapping>

65 <servlet-name>JspRedirector</servlet-name>

66 <url-pattern>/JspRedirector</url-pattern>

67 </servlet-mapping>

68 <servlet-mapping>

69 <servlet-name>action</servlet-name>

70 <url-pattern>*.do</url-pattern>

71 </servlet-mapping>

72 <welcome-file-list>

73 <welcome-file>index.jsp</welcome-file>

74 </welcome-file-list>

75 <taglib>

76 <taglib-uri>/tags/struts-nested</taglib-uri>

77 <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>

78 </taglib>

79 <taglib>

80 <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

81 <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

82 </taglib>

83 <taglib>

84 <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

85 <taglib-location>/WEB-INF/struts-html.tld</taglib-location>

86 </taglib>

87 <taglib>

88 <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

89 <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

90 </taglib>

91 <taglib>

92 <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>

93 <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>

94 </taglib>

95 <taglib>

96 <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>

97 <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>

98 </taglib>

99 </web-app>

100

2.spring配置

配置文件为applicationContext-hibernate.xml,在此配置文件中,配了POJO层,商业逻辑层,DAO层,和事务管理

java代码:

1

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

3 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

4

5 <beans>

6 <!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->

7

8 <!-- Choose the dialect that matches your "dataSource" definition -->

9

10 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

11 <property name="driverClassName">

12 <value>org.gjt.mm.mysql.Driver</value>

13 </property>

14 <property name="url">

15 <value>jdbc:mysql://localhost:3306/info_web?useUnicode=true&characterEncoding=GBK</value>

16 </property>

17 <property name="username">

18 <value>root</value>

19 </property>

20 <property name="password">

21 <value>123456</value>

22 </property>

23 </bean>

24

25 <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

26 <property name="mappingResources">

27 <list>

28 <value>infoweb/pojo/Answer.hbm.xml</value>

29 <value>infoweb/pojo/Board.hbm.xml</value>

30 <value>infoweb/pojo/Image.hbm.xml</value>

31 <value>infoweb/pojo/Info.hbm.xml</value>

32 </list>

33 </property>

34

35 <property name="hibernateProperties">

36 <props>

37 <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>

38 <prop key="hibernate.show_sql">true</prop>

39 <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>

40 </props>

41 </property>

42

43 <property name="dataSource"><ref bean="myDataSource"/></property>

44 </bean>

45

46

47

48 <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->

49 <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">

50 <property name="sessionFactory"><ref local="mySessionFactory"/></property>

51 </bean>

52

53

54 <!-- ***** Board SERVICE ***** -->

55 <bean id="boardService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

56 <property name="transactionManager"><ref local="myTransactionManager"/></property>

57 <property name="target"><ref local="boardServiceSpring"/></property>

58 <property name="transactionAttributes">

59 <props>

60 <prop key="get*">PROPAGATION_REQUIRED,readOnly,-BoardException</prop>

61 <prop key="set*">PROPAGATION_REQUIRED,-BoardException</prop>

62 <prop key="modify*">PROPAGATION_REQUIRED,-BoardException</prop>

63 <prop key="remove*">PROPAGATION_REQUIRED,-BoardException</prop>

64 </props>

65 </property>

66 </bean>

67

68 <!-- board primary business object implementation -->

69 <bean id="boardServiceSpring" class="infoweb.service.BoardServiceSpringImpl">

70 <property name="boardTreeDAO"><ref local="boardTreeDAO"/></property>

71 </bean>

72

73

74 <!-- DAO board: Hibernate implementation -->

75 <bean id="boardTreeDAO" class="infoweb.dao.BoardTreeDAOImpl">

76 <property name="sessionFactory"><ref local="mySessionFactory"/></property>

77 </bean>

78

79 <!-- DAO info: Hibernate implementation -->

80 <bean id="infoDAO" class="infoweb.dao.InfoDAOImpl">

81 <property name="sessionFactory"><ref local="mySessionFactory"/></property>

82 </bean>

83 </beans>

84

85

3.做DAO代码,

java代码:

1

2 package infoweb.dao;

3

4 import java.util.List;

5 import java.util.Iterator;

6

7 import infoweb.pojo.Board;

8

9

10 import net.sf.hibernate.HibernateException;

11 import net.sf.hibernate.Query;

12 import net.sf.hibernate.Session;

13

14 import org.springframework.orm.hibernate.HibernateCallback;

15 import org.springframework.orm.hibernate.support.HibernateDaoSupport;

16

17

18 /**

19 * <p>Title: 版块分类DAOImpl</p>

20 * <p>Description: 用树型结构实现</p>

21 * <p>Copyright: Copyright (c) 2004</p>

22 * <p>Company: </p>

23 * @author 段洪杰

24 * @version 1.0

25 */

26

27

28 public class BoardTreeDAOImpl extends HibernateDaoSupport implements

29 IBoardTreeDAO {

...}

30

31

32 /**

33 * 构造函数

34 */

35 public BoardTreeDAOImpl() {

...}

36 super();

37 }

38

39

40 /**

41 * 通过ID取得版块

42 * @param id String

43 * @return Board

44 */

45

46 public Board getBoardById(String id) {

...}

47 Board board = (Board) getHibernateTemplate().load(Board.class, id);

48 return board;

49 }

50

51

52 /**

53 * 取根叶

54 * @return Iterator

55 */

56 public Iterator getRoots() throws HibernateException {

...}

57 String queryString =

58 "select board from Board as board where board.parentId='root' order by board.id desc";

59 List roots = getHibernateTemplate().find(queryString);

60 return roots.iterator();

61 }

62

63

64 /**

65 * 存根叶

66 * @param board Board

67 */

68 public void setRoot(Board board) {

...}

69 board.setParentId("root");

70 getHibernateTemplate().save(board);

71 }

72

73

74 /**

75 * 取子叶

76 * @param parentid String

77 * @return List

78 */

79 public Iterator getChildren(String parentid) {

...}

80 /*

81 String queryString =

82 "select board as Board where board.parent_id='parentid' order by board.id desc";

83 List children = getHibernateTemplate().find(queryString);

84 return children;

85 */

86 Board parent = (Board) getHibernateTemplate().load(Board.class, parentid);

87 return parent.getChildren().iterator();

88 }

89

90

91 /**

92 * 取子叶数

93 * @param parentid String

94 * @return int

95 */

96

97 public int getChildrenCount(String parentid) {

...}

98 /*

99 String queryString =

100 "select count(*) Board where board.parent_id='parentid' order by board.id desc";

101 List children = getHibernateTemplate().find(queryString);

102 int count = ((Integer) children.iterator().next()).intValue();

103 return count;

104 */

105 Board parent = (Board) getHibernateTemplate().load(Board.class, parentid);

106 int count = parent.getChildren().size();

107 return count;

108 }

109

110

111 /**

112 * 存子叶

113 * @param parentLeaf Leaf

114 */

115 public void setChild(Board board, String parentid) {

...}

116 board.setParentId(parentid);

117 getHibernateTemplate().save(board);

118 }

119

120

121 /**

122 *

123 * 删除该叶和它的子叶

124 * @param board Board

125 */

126 public void deleteBranch(Board board) {

...}

127 getHibernateTemplate().delete(board);

128 }

129

130

131 /**

132 * 根据子叶得到父叶

133 * @param child Board

134 * @return Board

135 */

136 public Board getParentByChild(Board child) {

...}

137 String parentId = child.getParentId();

138 Board parent = (Board) getHibernateTemplate().load(Board.class, parentId);

139 return parent;

140 }

141

142

143 /**

144 * 通过子ID得到父叶

145 * @param id String

146 * @return Board

147 */

148 public Board getParentByChildId(String id) {

...}

149 Board child = (Board) getHibernateTemplate().load(Board.class, id);

150 Board parent = (Board) getHibernateTemplate().load(Board.class,child.getParentId());

151 return parent;

152 }

153 }

154

155

4.做service层代码

java代码:

1

2 package infoweb.service;

3

4 import java.util.List;

5 import java.util.Iterator;

6 import infoweb.dao.BoardTreeDAOImpl;

7 import infoweb.dao.IBoardTreeDAO;

8 import infoweb.pojo.Board;

9 import infoweb.exception.BoardException;

10 import net.sf.hibernate.HibernateException;

11

12 /**

13 * <p>Title: </p>

14 * <p>Description: </p>

15 * <p>Copyright: Copyright (c) 2004</p>

16 * <p>Company: </p>

17 * @author 段洪杰

18 * @version 1.0

19 */

20 public class BoardServiceSpringImpl implements IBoardService {

...}

21

22 private IBoardTreeDAO boardTreeDAO;

23

24 public BoardServiceSpringImpl() {

...}

25 super();

26 }

27

28 /**

29 * 取所有roots版块

30 * @return Iterator

31 */

32 public Iterator getRoots() throws BoardException {

...}

33 Iterator roots = null;

34 try {

...}

35 roots = boardTreeDAO.getRoots();

36 } catch (Exception ex) {

37 throw new BoardException("取ROOT版块时出错! " + ex.toString());

38 }

39 return roots;

40 }

41

42 /**

43 * 增加Root新版块

44 * @param board Board

45 */

46 public void setRoot(Board board) throws BoardException {

...}

47 try {

...}

48 boardTreeDAO.setRoot(board);

49 } catch (Exception ex) {

50 throw new BoardException("增加ROOT版块时出错! " + ex.toString());

51 }

52 }

53

54 /**

55 * 删除版块 (包含下级版块)

56 * @param board Board

57 */

58 public void removeBoard(Board board) throws BoardException {

...}

59 try {

...}

60 boardTreeDAO.deleteBranch(board);

61 } catch (Exception ex) {

62 throw new BoardException("删除版块时出错! " + ex.toString());

63 }

64 }

65

66 /**

67 *

68 * @return IBoardTreeDAO

69 */

70 public IBoardTreeDAO getBoardTreeDAO() {

...}

71 return boardTreeDAO;

72 }

73

74 /**

75 *

76 * @param boardTreeDAO IBoardTreeDAO

77 */

78 public void setBoardTreeDAO(IBoardTreeDAO boardTreeDAO) {

...}

79 this.boardTreeDAO = boardTreeDAO;

80 }

81

82 }

83

84

5.做ACTION的父类

java代码:

1

2 package infoweb.web;

3

4

5 import javax.servlet.ServletContext;

6 import org.apache.struts.action.Action;

7 import org.apache.struts.action.ActionServlet;

8 import org.springframework.web.context.WebApplicationContext;

9 import org.springframework.web.context.support.WebApplicationContextUtils;

10

11 import infoweb.service.IBoardService;

12

13

14 /**

15 * <p>Title: </p>

16 * <p>Description: </p>

17 * <p>Copyright: Copyright (c) 2004</p>

18 * <p>Company: </p>

19 * @author 段洪杰

20 * @version 1.0

21 */

22

23 public class BaseAction extends Action {

...}

24

25 private IBoardService boardService;

26

27 public void setServlet(ActionServlet actionServlet) {

...}

28 super.setServlet(actionServlet);

29 ServletContext servletContext = actionServlet.getServletContext();

30 WebApplicationContext wac =

31 WebApplicationContextUtils.getRequiredWebApplicationContext(

32 servletContext);

33 this.boardService = (IBoardService) wac.getBean("boardService");

34 }

35

36 protected IBoardService getBoardService() {

...}

37 return boardService;

38 }

39

40 }

41

42

6.做action类

java代码:

1

2 package infoweb.web;

3

4 import infoweb.pojo.Board;

5 import org.apache.commons.beanutils.PropertyUtils;

6 import org.apache.struts.action.*;

7 import org.apache.log4j.Logger;

8 import javax.servlet.http.*;

9 import java.util.Iterator;

10 import java.util.Date;

11

12 /**

13 * <p>Title: </p>

14 * <p>Description: </p>

15 * <p>Copyright: Copyright (c) 2004</p>

16 * <p>Company: </p>

17 * @author 段洪杰

18 * @version 1.0

19 */

20

21

22 public class SetBoardAction extends BaseAction {

...}

23

24 private static Logger log = Logger.getLogger(SetBoardAction.class);

25

26 public ActionForward execute(ActionMapping actionMapping,

27 ActionForm actionForm,

28 HttpServletRequest httpServletRequest,

29 HttpServletResponse httpServletResponse) throws

30 Exception {

...}

31

32 // SessionBean sessionBean = (SessionBean) httpServletRequest.getSession().getAttribute("sessionBean");

33 BoardForm boardForm = (BoardForm) actionForm;

34 //String backURL = httpServletRequest.getHeader("Referer");

35 /*

36 if (sessionBean==null||!sessionBean.getIsLogon()) {

37 httpServletRequest.setAttribute("message", "系统超时,或者没有登录 .返回重新登录!");

38 httpServletRequest.setAttribute("locationFile",

39 "location='index.jsp';");

40 return actionMapping.findForward("message");

41 }

42 */

43 Board board = new Board();

44 boardForm.setCreateDate(new Date());

45 PropertyUtils.copyProperties(board, boardForm);

46 getBoardService().setRoot(board);

47

48 httpServletRequest.setAttribute("message", "版块信息录入完成!");

49 httpServletRequest.setAttribute("locationFile",

50 "<A HREF=\"javascript:history.back()\">返回</A>");

51 return (actionMapping.findForward("success"));

52 }

53

54 }

55

56

学习中ing

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有