`
fishergay
  • 浏览: 33123 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Struts2.12+Spring3.2+hibernate4.2集成4

    博客分类:
  • SSH
 
阅读更多

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 3jsp页面.

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的比较麻烦。

不知道其他大大们,平时写技术博客是不是这样子,如果有好的办法请告知! 写的过程中有好几次都想放弃不写了,不过最后还是坚持下来了以后在看别人写的博客的时候,得尊重别人的成果,感谢他们的无私的分享。

 

 

  • 大小: 65.6 KB
  • 大小: 4.7 KB
  • 大小: 5.2 KB
分享到:
评论
1 楼 sunlongan666 2013-05-24  
非常感谢,认真看过,非常不错,表示支持

相关推荐

Global site tag (gtag.js) - Google Analytics