struts+ibatis+spring整合开发
web.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>Struts.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.objectFactory.spring.autoWire" value="type" /> <constant name="struts.objectFactory" value="spring" /> <include file="struts-default.xml"/> <package name="struts2" extends="struts-default"> <default-interceptor-ref name="paramsPrepareParamsStack" /> <!-- aciton的class为applicationContext.xml中的注册名 --> <action name="login" method="save"> <result name="success" type="redirect-action">show.action</result> <result name="error">/error.jsp</result> </action> <action name="edit" method="edit"> <result name="success">update.jsp</result> </action> <action name="update" method="update"> <result name="success" type="redirect-action">show.action</result> <result name="error">/error.jsp</result> </action> <action name="delete" method="delete"> <result name="success" type="redirect-action">show.action</result> <result name="error">/error.jsp</result> </action> <action name="show" method="findAllUser"> <result name="success">/list.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts> Struts.properties#struts.url.http.port=8080struts.devMode=truestruts.configuration.xml.reload=truestruts.locale=zh_CNstruts.i18n.encoding=UTF-8struts.objectFactory=springapplicationContext.xml<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byType" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/debug" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <bean id="client" ref="client" /> </bean> <bean id="userService" ref="userDao"></property> </bean> <bean id="LoginAction" ref="userService"></property> </bean> </beans> 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<User>, Preparable {private IUserService userService=null;private String id;private int pageIndex=1;private String pageBar;private List<User> 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<User> getList() {return list;}public void setList(List<User> 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 stubreturn user;}}IUserDAO.javapackage cn.hsw.dao;import java.util.List;import cn.hsw.model.User;public interface IUserDAO {public List<User> 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);}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<User> getAllUser() {List<User> 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;}}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<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="User"> <typeAlias alias="user" type="cn.hsw.model.User"/> <!-- 查询表中记录条数 --> <select id="recordCount" resultparameterresultresultparameterresultresultparameterparameterparameterparameterimport="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags" %><html> <head> <title>My JSP 'adduser.jsp' starting page</title> </head> <body> <s:form action="save" method="post"> <s:textfield label="username" name="username"/> <s:password label="password" name="password"/> <s:submit value="save"/> </s:form> </body></html>error.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags" %><html> <head> <title>My JSP 'error.jsp' starting page</title> </head> <body> This is my JSP page. <br> </body></html>list.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags" %><html> <head> <title>My JSP 'list.jsp' starting page</title> </head> <body> <s:form action="show" method="post"> <table width="50%" align="center"> <tr> <td align="center">userid</td> <td align="center">username</td> <td align="center">password</td> </tr> <s:iterator value="list" id="user" status="st"> <tr> <td align="center"><s:property value="id"/></td> <td align="center"><s:property value="username"/></td> <td align="center"><s:property value="password"/></td> <td align="center"> <s:url id="update" action="edit"> <s:param name="id"> <s:property value="id"/> </s:param> </s:url> <s:a href="%{update}">update</s:a> </td> <td align="center"> <s:url id="delete" action="delete"> <s:param name="id"> <s:property value="id" /> </s:param> </s:url> <s:a href="%{delete}">delete</s:a> </td> </tr> </s:iterator> <tr> <td colspan="4"> <s:property value="#request.pageBar" escape="false"/> </td> </tr> </table> </s:form> </body></html>login.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags" %><html> <head> <title>My JSP 'login.jsp' starting page</title> </head> <body> <s:form action="login" method="post"> <s:textfield label="username" name="username"/> <s:password label="password" name="password"/> <s:submit value="submit"/> </s:form> </body></html>success.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags" %><html> <head> <title>success</title> </head> <body> <a href="show.action">显示用户列表</a></br> <a href="adduser.jsp">添加用户</a> </body></html>update.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags" %><html> <head> <title>update.jsp</title> </head> <body> <s:form action="update" method="post"> <s:textfield name="id" label="ID" value="%{id}" readonly="true"/> <s:textfield name="username" label="UserName" value="%{username}" required="true"/> <s:textfield name="password" label="Password" value="%{password}" required="true"/> <s:submit value="update"/> </s:form> </body></html> 表userCREATE 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