前些天做了个学生信息的管理系统 无非是些信息的增删改查 这次用了ssh2不用模拟数据库了
注意点:
1.jar包
2.applicationContext.xml struts.xml hibernate.cfg.xml mode中类和映射文件User.hbm.xml
3.配置稍稍有点不同
用hibernateTemplate配置了sessionFactory在dao中配置了hibernateTemplate 也可以直接配置seesionFactory
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
4.在struts.xml中配置action 中的class必须和对应在applicationContext.xml的<bean id="***Action" class="com.***.action.**Action"> id的名字相对应,这样网页提交的action才能用spring实例化的对象
5.在(用User做对象)UserAction 中 声明userService 或者在UserService中声明UserDAO的时候最好用接口声明
用的是动态代理 当然用类直接声明也是可以的 用的就是CGILB代理
6.dao要extends HibernateDaoSupport
下面放spring 和struts的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="userAction" class="com.aowin.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean id="userService" class="com.aowin.serviceimpl.UserServiceImpl"> <property name="userDAO" ref="userDAO"></property> </bean> <bean id="userDAO" class="com.aowin.daoimpl.UserDAOImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 负责开启、提交事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager"></property> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames" value="*Service"></property> <property name="interceptorNames" value="transactionInterceptor"></property> </bean> </beans>
struts
<?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.devMode" value="true" /> <package name="default" namespace="/user" extends="struts-default"> <action name="userlist" class="userAction" method="getUsers"> <result>/list.jsp</result> </action> <action name="input" class="userAction" method="addInput"> <result>/input.jsp</result> </action> <action name="update" class="userAction" method="update"> <result>/update.jsp</result> </action> <action name="updatedo" class="userAction" method="updatedo"> <result name="input">/result.jsp</result> <result>/result.jsp</result> </action> <action name="delete" class="userAction" method="delete"> <result>/result.jsp</result> </action> <action name="add" class="userAction" method="addUser"> <!-- 配置重复提交的拦截器 --> <interceptor-ref name="token"></interceptor-ref> <!-- 配置全局的拦截器(有单独的拦截器默认的就会被替换掉,注意此时一定要加上) --> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="input">/result.jsp</result> <result name="success">/result.jsp</result> <result name="invalid.token">/error.jsp</result> </action> </package> </struts>
dao
import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.aowin.idao.IUserDAO; import com.aowin.model.User; public class UserDAOImpl extends HibernateDaoSupport implements IUserDAO { public void addUser(User user) { this.getHibernateTemplate().save(user); } public void deleteUser(User user) { this.getHibernateTemplate().delete(user); } public List<User> getAllUser() { String hql = "from User" ; List<User> list = this.getHibernateTemplate().find(hql); return list; } public void updateUser(User user) { this.getHibernateTemplate().update(user); } public User getOneUser(int id) { String hql = "select u from User u where u.id=?"; List<User> list = this.getHibernateTemplate().find(hql, id); return list.get(0); } }
action 中部分代码是老师的:
import java.util.List; import com.aowin.iservice.IUserService; import com.aowin.model.User; import com.aowin.serviceimpl.UserServiceImpl; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class UserAction extends ActionSupport { private int id; private String message; private User user; private List<User> userList; private IUserService userService; public IUserService getUserService() { return userService; } public void setUserService(IUserService userService) { this.userService = userService; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getId() { return id; } public void setId(int id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } /** * 查询所有用户 * * @return */ public String getUsers() { userList = userService.getUserList(); return Action.SUCCESS; } /** * 准备添加用户 * * @return */ public String addInput() { return Action.SUCCESS; } /** * 添加用户 * * @return */ public synchronized String addUser() { userList = userService.getUserList(); // 业务逻辑的验证 if (userList.contains(this.user)) { this.message = "已经存在相同用户,不能添加"; return "input"; } // 增加用户 userService.addMessage(user.getUsername(), user.getAddress()); this.message = "添加用戶成功"; return Action.SUCCESS; } /** * 验证表单输入的合法性 */ public void validateAddUser() { if (null == this.user) { this.message = "请输入合法的用户名,地址"; // 强制去找input试图 this.addFieldError("", ""); } else if (null == user.getAddress() || "".equals(user.getAddress())) { this.message = "请输入合法的用户名或者地址"; this.addFieldError("", ""); } else if (null == user.getUsername() || "".equals(user.getAddress())) { this.message = "请输入合法的用户名或者地址"; this.addFieldError("", ""); } } /** * 准备修改用户 * * @return */ public String update() { for (User u : userService.getUserList()) { if (u.getId() == this.id) { user = u; } } return Action.SUCCESS; } /** * 修改用户 * * @return */ public String updatedo() { // 用户名 密码没有实际修改就提交 for (User u : userService.getUserList()) { if (u.getId() == this.id && u.getUsername().equals(user.getUsername()) && u.getAddress().equals(user.getAddress())) { this.message = "您没有做修改"; return Action.INPUT; } } // 用户名密码确实有修改过 if (userService.getUserList().contains(this.user)) { this.message = "存在相同的用户名、地址,不能修改"; return Action.INPUT; } userService.updateMessage(id, user.getUsername(), user .getAddress()); this.message = "修改用户成功"; return Action.SUCCESS; } /** * 验证修改用户 */ public void validateUpdatedo() { // 验证表单输入域的合法性 validateAddUser(); } /** * 删除用户 * * @return */ public String delete() { userService.delMessage(id); this.message = "删除成功"; return Action.SUCCESS; } }显示用的是jsp页面 可以用S标签 也可以用一般的html标签