1.前面几个章节已经集成了SSH2了, 这一章节讨论测试action类 和3个框架整合是否正常使用。
2.贴一下完整需要的jar
3.1.添加Action 类和 相关jsp
package com.lysoft.web.action.user; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.lysoft.bean.user.User; import com.lysoft.service.user.UserService; import com.opensymphony.xwork2.ActionSupport; /** * 用户管理 * @author liang * */ public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; private static Log LOG = LogFactory.getLog(UserAction.class); private User user; private Integer[] ids; private List<User> userList; @Resource private UserService userService; public String userList() { try { userList = userService.listAll(); } catch (Exception e) { e.printStackTrace(); LOG.debug("获取用户列表失败!"); } return SUCCESS; } public String add() { try { user.setCreateTime(new Date()); userService.add(user); } catch (Exception e) { e.printStackTrace(); LOG.debug("添加用户失败!"); } return SUCCESS; } public String updateInput() { user = userService.find(user.getId().intValue()); return SUCCESS; } public String update() { try { User oldUser = userService.find(user.getId()); oldUser.setUserName(user.getUserName()); oldUser.setFirstName(user.getFirstName()); oldUser.setCreateTime(new Date()); userService.update(oldUser); } catch (Exception e) { e.printStackTrace(); LOG.debug("更新用户失败!"); } return SUCCESS; } public String delete() { try { userService.delete(ids); } catch (RuntimeException e) { e.printStackTrace(); LOG.debug("删除用户成功!"); } return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer[] getIds() { return ids; } public void setIds(Integer[] ids) { this.ids = ids; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } public String addInput() { return SUCCESS; } }
struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="do" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.configuration.xml.reload" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <package name="globalcfg" extends="struts-default" namespace="/"> <!-- exception page--> <global-results> <result name="exceptionPage"> <param name="location">/common/error.jsp</param> </result> </global-results> <!-- global exception--> <global-exception-mappings> <exception-mapping name="exception" result="exceptionPage" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> </package> <package name="user" namespace="/user" extends="globalcfg"> <action name="userList" class="com.lysoft.web.action.user.UserAction" method="userList"> <result name="success">/WEB-INF/page/user/user_list.jsp</result> </action> <action name="addInput" class="com.lysoft.web.action.user.UserAction" method="addInput"> <result name="success">/WEB-INF/page/user/add_input.jsp</result> </action> <action name="add" class="com.lysoft.web.action.user.UserAction" method="add"> <result name="success" type="redirectAction">userList</result> </action> <action name="updateInput" class="com.lysoft.web.action.user.UserAction" method="updateInput"> <result name="success">/WEB-INF/page/user/update_input.jsp</result> </action> <action name="update" class="com.lysoft.web.action.user.UserAction" method="update"> <result name="success" type="redirectAction">userList</result> </action> <action name="delete" class="com.lysoft.web.action.user.UserAction" method="delete"> <result name="success" type="redirectAction">userList</result> </action> </package> </struts>
jsp 代码
在WEB-INF下面添加page目录,并且在page目录下面添加 add_input.jsp
update_input.jsp user_list.jsp 3个jsp页面.
user_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户管理</title> </head> <body> <form id="form1" name="form1" action="delete.do" method="post"> <table width="40%" align="center" border="1"> <tr align="center"> <td>序号</td> <td>账号</td> <td>姓名</td> <td>时间</td> <td>操作</td> </tr> <s:iterator value="userList"> <tr align="center"> <td><s:property value="id"/></td> <td><s:property value="userName"/></td> <td><s:property value="firstName"/></td> <td><s:property value="createTime"/></td> <td> <a href="updateInput.do?user.id=<s:property value="id"/>">更新</a> <a href="delete.do?ids=<s:property value="id"/>">删除</a> </td> </tr> </s:iterator> <s:if test="null == userList || userList.isEmpty()"> <tr> <td colspan="7" align="center">没有可显示的记录!</td> </tr> </s:if> </table> </form> <table align="center" border="0"> <tr> <td> <input type="button" onclick="javascript:window.location.href='addInput.do'" value="添 加"/> </td> </tr> </table> </body> </html>
add_input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加用户</title> </head> <body> <form action="add.do" method="post"> <table align="center" border="1"> <tr> <td align="center">添加用户</td> </tr> </table> <br/> <table align="center" border="1"> <tr> <td align="center">账号</td> <td align="center">姓名</td> </tr> <tr> <td align="center"><input type="text" name="user.userName" /></td> <td align="center"><input type="text" name="user.firstName" /></td> </tr> </table> <table align="center" border="0"> <tr> <td> <input type="submit" value="保 存" id="saveId"/> </td> </tr> </table> </form> </body> </html>
update_input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>更新用户</title> </head> <body> <form action="update.do" method="post"> <input type="hidden" name="user.id" value="<s:property value="user.id"/>" /> <table align="center" border="1"> <tr> <td align="center">更新用户</td> </tr> </table> <br/> <table align="center" border="1"> <tr> <td align="center">账号</td> <td align="center">姓名</td> </tr> <tr> <td align="center"><input type="text" name="user.userName" value="<s:property value="user.userName"/>" /></td> <td align="center"><input type="text" name="user.firstName" value="<s:property value="user.firstName"/>" /></td> </tr> </table> <table align="center" border="0"> <tr> <td> <input type="submit" value="更 新" id="saveId"/> </td> </tr> </table> </form> </body> </html>
3. 启动tomcat,在浏览器中输入你的访问地址如::
http://localhost:8080/SSH2/user/userList.do
添加一条记录
测试成功! 删除记录也没有问题,此致已经完成所有整合操作。可以在此模板上方便加入jpa2.0 或其他框架整合。
总结:
写一下第一次写博客的感受吧, 发觉写博客很麻烦,特别是写技术博客,有需要贴源代码,还有提供图片参考, 每个例子都是自己辛苦的做过实验的,还要整合在一起。我写了4篇文章就话了我5个小时, 而且前提是这些代码都是之前已经弄好了, 我在写博客的时候又重新操作了一次, 我这些都是先写在word里面,然后再上传到iteye的比较麻烦。
不知道其他大大们,平时写技术博客是不是这样子,如果有好的办法请告知! 写的过程中有好几次都想放弃不写了,不过最后还是坚持下来了, 以后在看别人写的博客的时候,得尊重别人的成果,感谢他们的无私的分享。
相关推荐
这是一个struts 1.2 + spring 2.5 + hibernate 3.2框架demo, 运行环境为eclipse 3.2 + tomcat 5.5 + oracle 11g
Struts2.3 + Spring3.2 + Hibernate4.1 + HTML5 + CSS3开发示例代码。 其中包括如下文件: 介绍文档 Struts2.3 + Spring3.2 + Hibernate4.1 + HTML5 + CSS3开发示例.docx Eclipse工程文件 SSH2.zip Zip包是Eclipse...
struts2+spring2.5+hibernate3.2整合完整项目,带数据库脚本 ,mysql,tomcat,myeclipse完整工程
struts2.3+spring3.2+hibernate4.2整合,项目环境我搭建好了,大家可以参考学习 有好的建议希望多提出来!
struts2.5.0+spring4.1.6+hibernate4.2jar包
个人总结的搭建 Struts2.0 + Hibernate3.2 + Spring2.5 的说明 包含了所有jar包的名称以及有冲突jar包的名称(以便删除)。
前几天更换了最新版本的ssh,发现这次变化还是挺大的,费了很大劲才整理好,这是我整理的struts2.3.12+spring3.2+hibernate4.1.9 web网站框架,希望大家用的时候可以省点事吧,里面有用spring代理写的接口调用。
struts2.1 + spring 2.5 + hibernate 3.2 增删改查 详细例子
Struts2+hibernate3.2+Spring2.0架构搭建的菜鸟实例,按照每一步搭建自己的软件架构
cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip cdst ssh代码...
struts2+Spring2.5+Spring3.2)实现登录.struts2+Spring2.5+Spring3.2)实现登录.struts2+Spring2.5+Spring3.2)实现登录。
struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版 struts2 的方式自己去看简单。 spring2.5 是用注释来注入 hibernate3.2 是用ejb3注解映射关系 hibernate3 +个属性可以自动...
完整的struts1.2+hibernate3.2+spring2.5框架集成 完整的dwr2.0+spring2.5集成 处理的dwr2.0+hibernate3.2集成的问题
ssh(struts2.3.4+spring3.2+hibernate4.1.1)整合中jar包作用介绍 讲得很详细
Struts2+spring2.0+hibernate3.2整合带全部jar包 Struts2+spring2.0+hibernate3.2整合带全部jar包 Struts2+spring2.0+hibernate3.2整合带全部jar包
本demo采用struts2+spring4.0+hibernate4.2框架集成(内部包含所有jar包),有最dao最底层的代码,可在项目中直接使用,并实现了一个简单的登陆和查询,代码均测试通过,为打ssh框架的同学提供方便哦
Struts2+Spring2.5+Hibernate3.2集成实例开发2 Struts2+Spring2.5+Hibernate3.2集成实例开发2
struts2+spring2.5+hibernate3.2 找了半天没看到。自己整了一个。
1、可以运行(发布后,删除\Tomcat 6.0\webapps\ssh\WebRoot\WEB-INF\lib下的asm-2.2.3.jar) 2、采用Struts1.2 + Spring2.5 + Hibernate3.2 整合开发 3、SSH入门级实例
最新版SSH整合(Spring3.2+Struts2.3.12+Hibernate4.2)学习最新版SSH整合(Spring3.2+Struts2.3.12+Hibernate4.2)学习最新版SSH整合(Spring3.2+Struts2.3.12+Hibernate4.2)学习最新版SSH整合(Spring3.2+Struts...