© 版權聲明:本文為博主原創文章,轉載請注明出處
1.搭建環境
Maven:3.3.9
Struts2:2.5.10
Spring:4.3.8.RELEASE
Hibernate:5.1.7.Final
MySQL:5.7.17
2.說明
本文只有完整代碼。具體搭建步驟請參考《Maven環境下搭建SSH框架之Spring整合Struts2》和《Maven環境下搭建SSH框架之Spring整合Hibernate》
此代碼只新增了簡單的日志功能。
3.項目結構
4.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.ssh</groupId> <artifactId>SSH</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <properties> <!-- 統一源碼的編碼方式 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 統一各個框架版本 --> <struts.version>2.5.10</struts.version> <spring.version>4.3.8.RELEASE</spring.version> <hibernate.version>5.1.7.Final</hibernate.version> </properties> <dependencies> <!-- Junit依賴 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Spring 核心依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring web依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring整合ORM框架依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- Struts2 核心依賴 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <!-- Struts2和Spring整合依賴 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts.version}</version> </dependency> <!-- Hibernate 核心依賴 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- MySQL 依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency> <!-- C3P0 依賴 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5</version> </dependency> <!-- AspectJ依賴 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <!-- SLF4J依賴 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> </dependencies> <build> <plugins> <!-- 統一源代碼編譯輸出的JDK版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <!-- 打包時跳過單元測試 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!-- 集成Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/${project.artifactId}</path> </configuration> </plugin> </plugins> </build> </project>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 配置Struts2過濾器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置Spring的監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring配置文件所在路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
6.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>新增商品界面</title> </head> <body> <h1>新增商品</h1> <s:actionmessage/> <s:form action="product_save" method="post" namespace="/" theme="simple"> <table width="600px"> <tr> <th>商品名稱</th> <td><s:textfield name="pname"/></td> <td><font color="red"><s:fielderror fieldName="pname"/></font></td> </tr> <tr> <th>商品價格</th> <td><s:textfield name="price"/></td> <td><font color="red"><s:fielderror fieldName="price"/></font></td> </tr> <tr> <th colspan="2"> <input type="submit" value="保存"/> </th> <th> </th> </tr> </table> </s:form> </body> </html>
7.Product.java
package org.ssh.product.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import org.hibernate.annotations.GenericGenerator; @Entity public class Product { @Id @GeneratedValue(generator = "pid") @GenericGenerator(name = "pid", strategy = "native") private int pid;// 商品ID @Column(length = 100) private String pname;// 商品名稱 private double price;// 商品價格 public Product() { } public Product(String pname, double price) { this.pname = pname; this.price = price; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
8.ProductDao.java
package org.ssh.product.dao; import org.ssh.product.model.Product; /** * 商品操作-持久層接口 * */ public interface ProductDao { void saveProduct(Product product); }
9.ProductDaoImpl.java
package org.ssh.product.dao.impl; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.stereotype.Repository; import org.ssh.product.dao.ProductDao; import org.ssh.product.model.Product; /** * 商品信息-服務層實現 * */ @Repository public class ProductDaoImpl implements ProductDao { private HibernateTemplate template; @Autowired public ProductDaoImpl(SessionFactory sessionFactory) { this.template = new HibernateTemplate(sessionFactory); } @Override public void saveProduct(Product product) { template.save(product); } }
10.ProductService.java
package org.ssh.product.service; import org.ssh.product.model.Product; /** * 商品操作-服務層接口 * */ public interface ProductService { void saveProduct(Product product); }
11.ProductServiceImpl.java
package org.ssh.product.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.ssh.product.dao.ProductDao; import org.ssh.product.model.Product; import org.ssh.product.service.ProductService; @Service public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override public void saveProduct(Product product) { productDao.saveProduct(product); } }
12.ProductAction.java
package org.ssh.product.action; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.ssh.product.model.Product; import org.ssh.product.service.ProductService; import com.opensymphony.xwork2.ActionSupport; /** * 商品操作-控制層 * */ @Controller @Scope("prototype") public class ProductAction extends ActionSupport { private static final long serialVersionUID = 1L; @Autowired private ProductService productService; private String pname; private double price; /** * 保存商品操作 * * @return */ public String saveProduct() { Product product = new Product(pname, price); productService.saveProduct(product); this.addActionMessage("保存成功..."); return SUCCESS; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public void validate() { if(pname == null || "".equals(pname.trim())) { this.addFieldError("pname", "商品名稱不能為空"); } } }
13.struts.xml
<?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> <!-- 默認訪問頁面 --> <package name="default" extends="struts-default" namespace="/"> <default-action-ref name="default"/> <action name="default"> <result>/WEB-INF/view/index.jsp</result> </action> </package> <!-- 商品相關請求轉發 --> <!-- Struts2在2.5版本后添加strict-method-invocation(嚴格方法訪問),默認為true,不能使用動態方法調用功能,故需設為false --> <package name="product" extends="struts-default" namespace="/" strict-method-invocation="false"> <!-- 保存商品 --> <action name="product_*" class="productAction" method="{1}Product"> <result>WEB-INF/view/index.jsp</result> <result name="input">WEB-INF/view/index.jsp</result> </action> </package> <!-- 引入資源文件 --> <constant name="struts.custom.i18n.resources" value="messageResource"></constant> </struts>
14.messageResource.properties
invalid.fieldvalue.price = \u5546\u54c1\u4ef7\u683c\u8f93\u5165\u683c\u5f0f\u6709\u8bef
15.jdbc.properties
jdbc.url=jdbc:mysql:///ssh?useSSL=true&characterEncoding=UTF-8 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=***
16.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 開啟包掃描,並注冊注解 --> <context:component-scan base-package="org.ssh.*"/> <!-- 引入屬性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置C3P0連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 數據庫連接相關信息 --> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置Hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 注入連接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置Hibernate屬性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop><!-- 是否展示SQL --> <prop key="hibernate.hbm2ddl.auto">update</prop><!-- 是否自動創建表結構 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> </props> </property> <!-- 掃描並加載注解過的實體類 --> <property name="packagesToScan" value="org.ssh.*.model"/> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!-- 注入SessionFactory --> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置事務增強 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 配置需要進行事務管理的方法,和事務傳播行為 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置切面 --> <aop:config> <!-- 配置切入點 * org.ssh.service.*+.*(..) *:表示方法的作用域,*表示所有 org.ssh.service.*:表示org.ssh.service下的任何包 org.ssh.service.*+:表示org.ssh.service下的任何包及其子包 *(..):*表示任何方法,(..)表示方法的任何參數 --> <aop:pointcut id="pointcut" expression="execution(* org.ssh.*.service.*+.*(..))"/> <!-- 適配切入點和事務增強 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
17.log4j.properties
log4j.rootCategory=INFO, console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %t %c{2}:%L - %m%n