1、Struts2+Hibernate 整合应用开发步骤有时候让人很郁闷,看了一大篇的文字说明,结果弄个整合还是没成功,还不如言简意赅的舒服,下面就简单学习下 Struts2+Hibernate 的整合。其实就七个步骤:第一步:添加所需的 jar 包。第二步:创建实体类以及对应的映射文件。第三步:配置 hibernate.cfg.xml 配置文件。第四步:配置 struts.xml 配置文件。第五步:创建相应的 action。第六步:在 web.xml 中配置启动 Struts2 框架的过滤器。第七步:编写 JSP 显示页面。-具体细节及代码如下(代码仅供参考):第一步:jar 包的添加这里不多说
2、。第二步:实体类 Message.javajava view plaincopy1. /* 2. * ClassName: Message.java 3. * Created on 2011 4. * Copyrights 2011 All rights reserved. 5. * site: http:/ 6. * email: 7. */ 8. package com.yjd.nb.domain; 9. 10.import java.io.Serializable; 11.import java.util.Date; 12. 13./* 14. * 留言实体类 15. * 16. *
3、 author yjd 17. */ 18.public class Message implements Serializable 19. 20. private static final long serialVersionUID = 6617253915349620625L; 21. private Integer id; / OID 22. private String nickname; / 昵称 23. private boolean gender; / 性别 24. private String header_img; / 头像 25. private String qq; /
4、qq 26. private String email; / 邮箱号 27. private String content; / 留言内容 28. private String ip; / 发布者的 IP 地址 29. private Date pubTime; / 留言时间 30. private String reply;/ 管理员回复内容 31. private Date replyTime; / 回复时间 32. 33. public Integer getId() 34. return id; 35. 36. 37. public void setId(Integer id) 38.
5、 this.id = id; 39. 40. 41. public String getNickname() 42. return nickname; 43. 44. 45. public void setNickname(String nickname) 46. this.nickname = nickname; 47. 48. 49. public boolean isGender() 50. return gender; 51. 52. 53. public void setGender(boolean gender) 54. this.gender = gender; 55. 56.
6、57. public String getHeader_img() 58. return header_img; 59. 60. 61. public void setHeader_img(String headerImg) 62. header_img = headerImg; 63. 64. 65. public String getQq() 66. return qq; 67. 68. 69. public void setQq(String qq) 70. this.qq = qq; 71. 72. 73. public String getEmail() 74. return ema
7、il; 75. 76. 77. public void setEmail(String email) 78. this.email = email; 79. 80. 81. public String getContent() 82. return content; 83. 84. 85. public void setContent(String content) 86. this.content = content; 87. 88. 89. public String getIp() 90. return ip; 91. 92. 93. public void setIp(String i
8、p) 94. this.ip = ip; 95. 96. 97. public Date getPubTime() 98. return pubTime; 99. 100. 101. public void setPubTime(Date pubTime) 102. this.pubTime = pubTime; 103. 104. 105. public String getReply() 106. return reply; 107. 108. 109. public void setReply(String reply) 110. this.reply = reply; 111. 112
9、. 113. public Date getReplyTime() 114. return replyTime; 115. 116. 117. public void setReplyTime(Date replyTime) 118. this.replyTime = replyTime; 119. 120. 121. Override 122. public String toString() 123. return “Message content=“ + content + “, email=“ + email +“, gender=“ 124. + gender + “, header
10、_img=“ + header_img + “, id=“ +id 125. + “, ip=“ + ip + “, nickname=“ + nickname + “, pubTime=“ 126. + pubTime + “, qq=“ + qq + “; 127. 128. 129. 实体类映射文件 Message.hbm.xml.html view plaincopy1. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 第三步:Hibernate 配置文件 hibernate.cfg.xml.h
11、tml view plaincopy1. 4. 5. 6. 7. 8. 9. com.mysql.jdbc.Driver 10. 11. 12. jdbc:mysql:/s2h-model 13. 14. root 15. root 16. 17. 18. 19. org.hibernate.dialect.MySQLDialect 20. 21. 22. 23. 24. org.hibernate.connection.C3P0ConnectionProvider 25. 26. 5 27. 30 28. 1800 29. 50 30. 31. 32. update 33. true 34.
12、 true 35. 36. 37. 38. 39. 40. 第四步:Struts2 配置文件 struts.xml.html view plaincopy1. 2. 5. 6. 标记来对default.properties 中的常量属性做覆盖性配置,最常更改的几个常量属性是: 8. 1.struts.i18n.encoding=UTF-8:请求消息的编码方式 9. 2.struts.action.extension=action:指定被 struts2 处理的请求后缀类型。多个用逗号隔开。如:action,do,go 10. 3.struts.configuration.xml.reload=
13、false:当 struts.xml 改动后,是否重新加载。默认值为 false(生产环境下使用), 开发阶段最好打开 11. 4.struts.devMode=false:是否使用 struts 的开发模式。开发模式会有更多的调试信息。默认值为 false(生产环境下使用), 开发阶段最好打开 12. 5.struts.serve.static.browserCache=true:设置浏览器是否缓存静态内容。默认值为 true(生产环境下使用 ),开发阶段最好关闭。 13. 6.struts.objectFactory=spring:指定由 spring 负责 action 对象的创建 14
14、. 7.struts.enable.SlashesInActionNames=false:启用 Action 的 name是否支持斜线(/) 15. 8.struts.enable.DynamicMethodInvocation=false:启用动态方法调用 16. 9.struts.ui.theme=simple:UI 主题类型 17. 18. 19. 20. 21. 22. 23. 24. 31. 32. 33. /main.jsp 34. 35. 36. 37. 39. /index.action 40. 41. 42. 第五步:创建 action 类。跳转到首页的 action:jav
15、a view plaincopy1. /* 2. * ClassName: IndexAction.java 3. * Created on 2011 4. * Copyrights 2011 All rights reserved. 5. * site: http:/ 6. * email: 7. */ 8. package com.yjd.nb.web.action; 9. 10.import org.apache.struts2.ServletActionContext; 11. 12.import com.opensymphony.xwork2.ActionSupport; 13.
16、import mon.PageModel; 14.import com.yjd.nb.domain.Message; 15.import com.yjd.nb.service.MessageService; 16. 17./* 18. * 首页的请求处理类 19. * 20. * author yjd 21. */ 22.public class IndexAction extends ActionSupport 23. private static final long serialVersionUID = -3634083788212536690L; 24. private Message
17、Service messageService = new MessageService(); 25. private int pageNo = 1; 26. private int pageSize = 10; 27. 28. public String execute() 29. PageModel pm = messageService.findByPage(pageNo, pageSize); 30. 31. ServletActionContext.getRequest().setAttribute(“pm“, pm); 32. / ActionContext.getContext()
18、.put(“pm“, pm); 33. return SUCCESS; 34. 35. 36. public MessageService getMessageService() 37. return messageService; 38. 39. 40. public void setMessageService(MessageService messageService) 41. this.messageService = messageService; 42. 43. 44. public int getPageNo() 45. return pageNo; 46. 47. 48. pu
19、blic void setPageNo(int pageNo) 49. this.pageNo = pageNo; 50. 51. 52. public int getPageSize() 53. return pageSize; 54. 55. 56. public void setPageSize(int pageSize) 57. this.pageSize = pageSize; 58. 59. 实体操作 action:java view plaincopy1. /* 2. * ClassName: MsgAction.java 3. * Created on 2011 4. * Co
20、pyrights 2011 All rights reserved. 5. * site: http:/ 6. * email: 7. */ 8. package com.yjd.nb.web.action; 9. 10.import java.util.Date; 11. 12.import org.apache.struts2.ServletActionContext; 13. 14.import com.opensymphony.xwork2.ActionSupport; 15.import com.yjd.nb.domain.Message; 16.import com.yjd.n
21、b.service.MessageService; 17. 18./* 19. * 前台针对 Msg 操作的处理类 20. * 21. * author yjd 22. */ 23.public class MsgAction extends ActionSupport 24. private static final long serialVersionUID = -5736885633645314440L; 25. private MessageService messageService = new MessageService(); 26. private Message msg; 2
22、7. 28. / 添加实体 29. public String add() 30. msg.setIp(ServletActionContext.getRequest().getRemoteAddr(); 31. msg.setPubTime(new Date(); 32. 33. messageService.add(msg); 34. return “list“; 35. 36. 37. public Message getMsg() 38. return msg; 39. 40. 41. public void setMsg(Message msg) 42. this.msg = msg
23、; 43. 44. 45. 第六步:在 web.xml 中配置启动 Struts2 框架的过滤器。html view plaincopy1. 2. 7. 8. 9. struts2 10. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 11. 12. 13. 14. struts2 15. /* 16. 17. 18. 19. index.jsp 20. 21. 第七步:编写 JSP 显示页面。首页跳转 index.jsphtml view plaincopy1. 2. -% 3. 主页:main.j
24、sphtml view plaincopy1. 2. 3. 4. 5. 6. 在线留言系统 7. 8. 9. 10.bodyfont-size: 12px 11.tablefont-size: 12px 12.afont-size:12px 13currentfont-size:12px; 14. 15. 16. 17. 18. 19. 20. 21. 22. 请 签 写 留 言 24. 25. 您的称呼: 26. 27. 28. 您的性别: 29. 30. 男 女 31. 32. 33. 选择头像: 34. 1.gif 2.gif 3.gif 4.gif 5.gif 6.gif 7.gif
25、8.gif 9.gif 10.gif 11.gif 12.gif 13.gif 14.gif 15.gif 16.gif 17.gif 18.gif 19.gif 20.gif 55. 56. 您的 qq: 57. 58. 59. 您的邮箱: 60. 61. 62. 留言内容: 63. 64. 65. 66. 67. 68. 69. 70. 71. 留 言 列 表【管理登录】73. 74. 75. 76. 你好 : $msg.nickname$msg.gender ? “帅哥“ : “靓妹“ 78. 79. 发表于:来自:$msg.ip 83. 84. $msg.content 85. 86. 管理员回复: 87. 88. 89. 90. 91. 92. 93. 项目结构如图: