Struts2+Spring+Ibatis整合

zxpzj11 贡献于2010-11-24

作者 asus  创建于2009-07-02 08:53:00   修改者asus  修改于2009-07-02 09:22:00字数12454

文档摘要:
关键词:

 Struts2+ibatis+spring整合开发 1. 工程结构图 2. 配置文件 web.xml index.jsp contextConfigLocation /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* 如图: Struts.xml /success.jsp /login.jsp show.action /error.jsp update.jsp show.action /error.jsp show.action /error.jsp /list.jsp /error.jsp 如图: Struts.properties #struts.url.http.port=8080 struts.devMode=true struts.configuration.xml.reload=true struts.locale=zh_CN struts.i18n.encoding=UTF-8 struts.objectFactory=spring 如图: sql-map-config.xml 如图: applicationContext.xml classpath:sql-map-config.xml 如图: 3. Login.java package cn.hsw.action; import java.util.List; import cn.hsw.model.User; import cn.hsw.service.IUserService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.Preparable; public class Login extends ActionSupport implements ModelDriven, Preparable { private IUserService userService=null; private String id; private int pageIndex=1; private String pageBar; private List list; private User user; public void prepare()throws Exception{ if(id==null||id.length()==0){ user=new User(); }else{ user=getUserService().getUserById(Integer.parseInt(id)); } } public String execute()throws Exception{ if(getUserService().isLogin(user)){ return SUCCESS; } return INPUT; } public String save()throws Exception{ if(getUserService().insertUser(user)){ return SUCCESS; } return ERROR; } public String edit(){ return SUCCESS; } public String update()throws Exception{ if(getUserService().updateUser(user)){ return SUCCESS; } return ERROR; } public String delete()throws Exception{ if(getUserService().deleteUser(Integer.parseInt(id))){ return SUCCESS; } return ERROR; } public String findAllUser()throws Exception{ try { list=getUserService().getAllUser(); } catch (Exception e) { } return SUCCESS; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public String getPageBar() { return pageBar; } public void setPageBar(String pageBar) { this.pageBar = pageBar; } public List getList() { return list; } public void setList(List list) { this.list = list; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public void setUserService(IUserService userService) { this.userService = userService; } public IUserService getUserService() { return userService; } public User getModel() { // TODO Auto-generated method stub return user; } } 如图: IUserDAO.java package cn.hsw.dao; import java.util.List; import cn.hsw.model.User; public interface IUserDAO { public List getAllUser(); public User getUserById(Integer id); public boolean isLogin(User user); public boolean insertUser(User user); public boolean updateUser(User user); public boolean deleteUser(Integer id); } 如图: UserDAO.java package cn.hsw.dao; import java.util.List; import org.springframework.orm.ibatis.SqlMapClientTemplate; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import cn.hsw.model.User; import com.ibatis.sqlmap.client.SqlMapClient; public class UserDAO implements IUserDAO { private SqlMapClient client = null; public boolean deleteUser(Integer id) { try { client.delete("deleteUser", id); return true; } catch (Exception e) { return false; } } public List getAllUser() { List list=null; try{ list=client.queryForList("getAllUser"); }catch(Exception e){ e.getStackTrace(); } return list; } public User getUserById(Integer id) { User user=null; try { user=(User) client.queryForObject("getUserById",id); return user; } catch (Exception e) { e.getStackTrace(); } return user; } public boolean insertUser(User user) { try{ client.insert("insertUser",user); return true; }catch(Exception e){ return false; } } public boolean isLogin(User user) { try { User u=(User) client.queryForObject("checkUser",user); if(u!=null){ return true; } } catch (Exception e) { e.getStackTrace(); } return false; } public boolean updateUser(User user) { try { client.update("updateUser",user); return true; } catch (Exception e) { return false; } } public void setClient(SqlMapClient client) { this.client = client; } } 如图: User.java package cn.hsw.model; public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 如图: user.xml 如图: IUserService.java package cn.hsw.service; import java.util.List; import cn.hsw.model.User; public interface IUserService { public List getAllUser(); public User getUserById(Integer id); public boolean isLogin(User user); public boolean insertUser(User user); public boolean updateUser(User user); public boolean deleteUser(Integer id); } 如图: UserService.java package cn.hsw.service; import java.util.List; import cn.hsw.dao.IUserDAO; import cn.hsw.dao.UserDAO; import cn.hsw.model.User; public class UserService implements IUserService { private IUserDAO userDAO=null; public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public boolean deleteUser(Integer id) { return userDAO.deleteUser(id); } public List getAllUser() { return userDAO.getAllUser(); } public User getUserById(Integer id) { return userDAO.getUserById(id); } public boolean insertUser(User user) { return userDAO.insertUser(user); } public boolean isLogin(User user) { return userDAO.isLogin(user); } public boolean updateUser(User user) { return userDAO.updateUser(user); } } 如图: 4. 页面 adduser.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> My JSP 'adduser.jsp' starting page 如图: error.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> My JSP 'error.jsp' starting page This is my JSP page.
如图: list.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> My JSP 'list.jsp' starting page
userid username password
update delete
如图: login.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> My JSP 'login.jsp' starting page 如图: success.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> success 显示用户列表
添加用户 如图: update.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> update.jsp 如图: 5. jar包 6. 表user CREATE TABLE `user` ( `id` int(11) NOT NULL auto_increment, `username` varchar(20) NOT NULL default '', `password` varchar(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 15 金币 [ 分享文档获得金币 ] 7 人已下载

下载文档