依赖流程
控制器action依赖service层
业务逻辑层service依赖dao层
持久层dao依赖spring提供的类
UserDAOImpl
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:dao接口实现
*/
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
@Override
public void save(User user) {
getHibernateTemplate().save(user);
}
@Override
public void update(User user) {
getHibernateTemplate().update(user);
}
@Override
public void delete(Long id) {
User user = findByKey(id);
if (user != null) {
getHibernateTemplate().delete(user);
}
}
@Override
public User findByKey(Long id) {
return getHibernateTemplate().get(User.class, id);
}
@Override
public List<User> findAll() {
return getHibernateTemplate().loadAll(User.class);
}
}
UserServiceImpl
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:service接口实现
*/
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public void save(User user) {
userDAO.save(user);
}
@Override
public void update(User user) {
userDAO.update(user);
}
@Override
public void delete(Long id) {
userDAO.delete(id);
}
@Override
public User findByKey(Long id) {
return userDAO.findByKey(id);
}
@Override
public List<User> findAll() {
return userDAO.findAll();
}
}
UserAction
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:控制器类
*/
public class UserAction extends ActionSupport {
private UserService userService;
private User user;
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public String execute() throws Exception {
ActionContext.getContext().put("users", userService.findAll());
return SUCCESS;
}
@Override
public String input() throws Exception {
if (user != null && user.getId() != null) {
user = userService.findByKey(user.getId());
}
return INPUT;
}
public String save() throws Exception {
if (user == null) {
return execute();
}
if (user.getId() == null) {
userService.save(user);
} else {
userService.update(user);
}
return "redirect";
}
public String delete() throws Exception {
if (user != null && user.getId() != null) {
userService.delete(user.getId());
}
return "redirect";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<display-name>struts2-spring3-hibernate3-xml</display-name>
<!-- 告诉spring从哪里加载配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 初始化spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
userList.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>用户列表</title>
</head>
<body>
<s:a href="user_input.action">添加</s:a>
<table border="1">
<tr>
<th>编号</th>
<th>名称</th>
<th>年龄</th>
<th>操作</th>
</tr>
<s:iterator value="#users">
<tr>
<td><s:property value="id" /></td>
<td><s:property value="name" /></td>
<td><s:property value="age" /></td>
<td>
<s:a href="user_input.action?user.id=%{id}">编辑</s:a>
<s:a href="user_delete.action?user.id=%{id}">删除</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
userInput.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>用户列表</title>
</head>
<body>
<s:form action="user_save">
<s:hidden name="user.id"/>
<s:textfield label="用户名" name="user.name"/><br/>
<s:textfield label="年龄" name="user.age"/><br/>
<s:submit value="保存"/>
</s:form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 关闭动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 开启开发模式 -->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<!-- 列表显示 -->
<result>/WEB-INF/pages/userList.jsp</result>
<!-- 新增/编辑页面显示 -->
<result name="input">/WEB-INF/pages/userInput.jsp</result>
<!-- 保存或者删除之后进行重定向 -->
<result name="redirect" type="redirectAction">user</result>
</action>
</package>
</struts>
其他关联项目
- Struts2 Spring3 Hibernate3 集成xml版本-1
http://blog.csdn.net/je_ge/article/details/54429976
源码地址
https://github.com/je-ge/framework
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!