控制器action依賴service層
業務邏輯層service依賴dao層
持久層dao依賴spring提供的類html
/** * @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);
}
}
/** * @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();
}
}
/** * @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;
}
}
<?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>
<%@ 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>
<%@ 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>
<?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>
https://github.com/je-ge/frameworkjava
若是以爲個人文章或者代碼對您有幫助,能夠請我喝杯咖啡。您的支持將鼓勵我繼續創做!謝謝!
git