源码下载 :http://download.csdn.net/download/knight_black_bob/9332935
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <!-- org.apache.struts2.dispatcher.FilterDispatcher --> <!-- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/database jdbc.username=root jdbc.password=root
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <!-- 加载DB配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties"></property> </bean> <!--第一个数据源,采用XAPool链接池 --> <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"> <property name="dataSource"> <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"> <!-- <property name="transactionManager" ref="jotm" /> --> <property name="driverName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> </bean> </property> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
applicationContext-action.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName" > <!-- <bean id="otherUserAction" class="com.fusionability.user.action.OtherUserAction" scope="prototype"></bean> --> <bean id="userAction" class="com.bp.user.action.UserAction" scope="prototype"></bean> </beans>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <!-- <bean id="activationDao" class="com.fusionability.user.dao.impl.ActivationDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplateFusionWeb" /> </bean> --> <bean id="userDao" class="com.bp.user.daoImpl.UserDaoImple"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> </beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <!-- <bean id="managerAppService" class="com.fusionability.system.service.impl.ManagerAppServiceImpl"></bean> --> <bean id="userService" class="com.bp.user.serviceImpl.UserServiceImpl"></bean> </beans>
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.action.extension" value="action" /> <constant name="struts.i18n.encoding" value="utf-8" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.configuration.xml.reload" value="false" /> <constant name="struts.devMode" value="true" /> <include file="struts-interceptor.xml"></include> <include file="struts-exception.xml"></include> <include file="struts-action-*.xml"></include> </struts>
struts-interceptor.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> <package name="interceptorPackage" extends="struts-default"> <interceptors> <interceptor name="authInterceptor" class="com.bp.base.interceptor.AuthInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="authInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> </package> </struts>
struts-exception.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> <package name="exceptionPackage" extends="interceptorPackage"> <global-results> <result name="login" type="redirect">/user/login.jsp</result> <result name="error" type="redirect">/user/login.jsp</result> <result name="error-500" type="redirect">/user/login.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="error-500" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> </package> </struts>
struts-action-user.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> <package name="userPackage" namespace="/user" extends="exceptionPackage"> <!-- <action name="sdkDownload" class="sdkDownloadAction"> result的Type必须为stream <result name="success" type="stream"> <param name="contentType"> application/octet-stream;charset=ISO-8859-1 </param> <param name="contentDisposition">attachment;fileName="${fileName}"</param> <param name="inputName">downloadFile</param> </result> </action> <action name="otherUserAction" class="otherUserAction" method="toOtherRegPage"> <result name="other_login">/user/other_login.jsp</result> </action> --> <action name="userAction" class="userAction" method="login"> <result name="error">/user/login.jsp</result> <result name="success" type="redirect">/user/userListAction.action</result> </action> <action name="userListAction" class="userAction" method="userList"> <result name="error">/user/userList.jsp</result> <result name="success" >/user/userList.jsp</result> </action> <action name="toeditAction" class="userAction" method="toedit"> <result name="success" >/user/editUser.jsp</result> </action> <action name="editAction" class="userAction" method="edit"> <result name="success" type="redirect">/user/userListAction.action</result> <result name="error" type="redirect">/user/toeditAction.action?id=${id}</result> </action> <action name="deleteAction" class="userAction" method="delete"> <result name="success" type="redirect">/user/userListAction.action</result> </action> <action name="toaddAction" class="userAction" method="toAdd"> <result name="success" >/user/addUser.jsp</result> </action> <action name="addAction" class="userAction" method="add"> <result name="error" >/user/addUser.jsp</result> <result name="success" type="redirect">/user/userListAction.action</result> </action> </package> </struts>
UserAction
package com.bp.user.action; import java.util.List; import org.apache.log4j.Logger; import org.springframework.ui.ModelMap; import com.bp.base.action.BaseAction; import com.bp.entity.User; import com.bp.user.service.UserService; public class UserAction extends BaseAction { private static Logger logger = Logger.getLogger(UserAction.class); public UserService userService; public User user; public int id; public String login(){ User user1 = userService.findUserByNameAndPoss(user); if(null == user1){ return "error"; } return "success"; } public String userList(){ List <User> users = userService.findAllUsers(); logger.info("users:"+users); getRequest().setAttribute("users", users); //getSession().setAttribute("users", users); return "success"; } public String toedit(){ User user1 = userService.findUserById(new User(this.id)); System.out.println(user1); getRequest().setAttribute("user", user1); return "success"; } public String edit(){ logger.info("user:"+user.toString()); boolean flag = userService.updateUser(user); if(!flag){ getRequest().setAttribute("id", user.getId()); return "error"; } return "success"; } public String delete(){ boolean flag = userService.deleteUser(new User(id)); return "success"; } public String toAdd(){ return "success"; } public String add(){ User user1 = userService.addUser(user); if(null == user1){ return "error"; } return "success"; } @Override public String execute() throws Exception { return super.execute(); } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
User
package com.bp.entity; import java.io.Serializable; @SuppressWarnings("serial") public class User implements Serializable { private int id; private String userName; private String password; public User() { } public User(int id) { this.id = id; } public User(String userName, String password) { this.userName = userName; this.password = password; } 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; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", password=" + password + "]"; } }
UserDao
package com.bp.user.dao; import java.util.List; import org.springframework.stereotype.Repository; import com.bp.entity.User; public interface UserDao { public int addUser(User user); public User findUserById(User user); public User findUserByName(User user); public List<User> findAllUsers(); public int updateUser(User user); public int deleteUser(User user); public User findUserByNameAndPoss(User user); }
UserDaoImple
package com.bp.user.daoImpl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import com.bp.entity.User; import com.bp.user.dao.UserDao; public class UserDaoImple implements UserDao { private JdbcTemplate jdbcTemplate; @Override public int addUser(User user) { String addSql = "insert into user (userName,password) values(?,?)"; int rs = jdbcTemplate.update(addSql, new Object[]{user.getUserName(),user.getPassword()}); return rs ; } @Override public User findUserById(User user) { String selectSql = "select * from user where id= ?"; List<User> users = jdbcTemplate.query(selectSql,new Object[]{user.getId()}, new UserRowMap()); return null != users && users.size() != 0 ? users.get(0) : null; } @Override public User findUserByName(User user) { String selectSql = "select * from user where userName= ?"; List<User> users = jdbcTemplate.query(selectSql,new Object[]{user.getUserName()}, new UserRowMap()); return null != users && users.size() != 0 ? users.get(0) : null; } @Override public List<User> findAllUsers() { String selectSql = "select * from user"; List<User> users = jdbcTemplate.query(selectSql, new UserRowMap()); return users; } @Override public int updateUser(User user) { String addSql = "update user set user.password = ?,user.userName = ? where user.id = ?"; int rs = jdbcTemplate.update(addSql, new Object[]{user.getPassword(),user.getUserName(),user.getId()}); return rs ; } @Override public int deleteUser(User user) { String addSql = "delete from user where user.id = ?"; int rs = jdbcTemplate.update(addSql, new Object[]{user.getId()}); return rs ; } class UserRowMap implements RowMapper{ @Override public User mapRow(ResultSet rs, int index) throws SQLException { User user = new User(); user.setId(rs.getInt("id")); user.setUserName(rs.getString("userName")); user.setPassword(rs.getString("password")); return user; } } @Override public User findUserByNameAndPoss(User user) { String selectSql = "select * from user where user.userName= ? and user.password =?"; List<User> users = jdbcTemplate.query(selectSql, new Object[]{user.getUserName(),user.getPassword()}, new UserRowMap()); return null != users && users.size() != 0 ? users.get(0) : null; } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } }
UserService
package com.bp.user.service; import java.util.List; import com.bp.entity.User; public interface UserService { public User addUser(User user); public User findUserById(User user); public User findUserByName(User user); public User findUserByNameAndPoss(User user); public List<User> findAllUsers(); public boolean updateUser(User user); public boolean deleteUser(User user); }
UserServiceImpl
package com.bp.user.serviceImpl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.bp.entity.User; import com.bp.user.dao.UserDao; import com.bp.user.service.UserService; public class UserServiceImpl implements UserService { public UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public User addUser(User user) { return userDao.addUser(user)>0 ? userDao.findUserByName(user): null; } @Override public User findUserById(User user) { return userDao.findUserById(user); } @Override public User findUserByName(User user) { return userDao.findUserByName(user); } @Override public List<User> findAllUsers() { return userDao.findAllUsers(); } @Override public boolean updateUser(User user) { return userDao.updateUser(user)>0; } @Override public boolean deleteUser(User user) { return userDao.deleteUser(user)>0; } @Override public User findUserByNameAndPoss(User user) { return userDao.findUserByNameAndPoss(user); } }
BaseAction
package com.bp.base.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class BaseAction extends ActionSupport{ public HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } public HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } public ServletContext getContext() { return ServletActionContext.getServletContext(); } public HttpSession getSession() { return getRequest().getSession(); } }
AuthInterceptor
package com.bp.base.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class AuthInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { return invocation.invoke(); } }