Struts2学习-ssh框架
SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架。
http://www.cnblogs.com/laibin/p/5847111.html
1.新建以下的类
2.接口
UserDao负责数据
package com.nf.dao; import com.nf.entity.User; public interface UserDao { //这是数据库层面的操作, //往往编写dao的人,不用考虑业务逻辑的 //单对象例如,User,以及List,例如UserList //编写增删改查 public User getUserById(String id); //public boolean delUserById(); //public boolean updateUser(); }
UserDaoImpl继承UserDao接口
package com.nf.dao.impl; import com.nf.dao.UserDao; import com.nf.entity.User; public class UserDaoImpl implements UserDao{ public User getUserById(String id) { //写JDBC或者hibernate等代码,获取user //模拟从数据库已经取了一个User User user = new User(); user.setUserName("小李"); user.setUserPassword("123456"); return user; } }
UserService有一个方法,获取id
UserServiceImpl继承方法并new一个获取的userDao
package com.nf.service.impl; import com.nf.dao.UserDao; import com.nf.entity.User; import com.nf.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao = null; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public String getUserNameById(String id) { User user = userDao.getUserById(id); if ("123456".equals(user.getUserPassword())){ return user.getUserName()+"(弱密码)"; } else { return user.getUserName(); } } }
3.service
需要一个service,就定义service的接口为属性,UserAction
private UserService userService = null; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } @Override public String execute() throws Exception { //userService=new UserServiceImpl(); String userName = userService.getUserNameById("123"); System.out.println(userName); return this.NONE; }
4.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.objectFactory" value="spring"></constant> <package name="myPackage" extends="struts-default"> <action name="index" class="userAction"></action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <beans 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.xsd"> <bean id="userAction" class="com.nf.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean id="userService" class="com.nf.service.impl.UserServiceImpl" scope="prototype"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="com.nf.dao.impl.UserDaoImpl" scope="prototype"></bean> </beans>
ps:有一个web.xml
<!--1个spring的监听器--> <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>
地址:https://gitee.com/MuNianShi/ssh001.git