ITPub博客

首页 > IT职业 > IT生活 > spring3.2.x+struts2+mybatis3.x整合

spring3.2.x+struts2+mybatis3.x整合

IT生活 作者:boss_ch 时间:2021-09-09 09:14:41 0 删除 编辑

与前面的一篇mybatis 3.2.7 与 spring mvc 3.x、logback整合 相比,只是web层的MVC前端框架,从spring mvc转换成struts 2.x系列,变化并不大

一、pom.xml 配置

  1 
  2 
  4     4.0.0
  5     infosky
  6     struts2-helloworld
  7     war
  8     1.0
  9     
 10         1.6
 11         2.3.16.3
 12         3.2.8.RELEASE
 13     
 14     
 15 
 16         <!-- db driver -->
 17         
 18             commons-dbcp
 19             commons-dbcp
 20             1.4
 21         
 22 
 23 
 24         <!-- log -->
 25         
 26             org.slf4j
 27             slf4j-api
 28             1.7.7
 29         
 30 
 31         
 32             ch.qos.logback
 33             logback-core
 34             1.1.2
 35         
 36 
 37         
 38             ch.qos.logback
 39             logback-classic
 40             1.1.2
 41         
 42 
 43         <!-- spring -->
 44         
 45             org.apache.struts
 46             struts2-spring-plugin
 47             2.3.16.3
 48         
 49 
 50         
 51             org.springframework
 52             spring-context
 53             ${spring.version}
 54         
 55 
 56         
 57             org.springframework
 58             spring-beans
 59             ${spring.version}
 60         
 61 
 62         
 63             org.springframework
 64             spring-core
 65             ${spring.version}
 66         
 67 
 68         
 69             org.springframework
 70             spring-web
 71             ${spring.version}
 72         
 73 
 74         
 75             org.springframework
 76             spring-context
 77             ${spring.version}
 78         
 79 
 80         
 81             org.springframework
 82             spring-context-support
 83             ${spring.version}
 84         
 85 
 86         
 87             org.springframework
 88             spring-expression
 89             ${spring.version}
 90         
 91 
 92         
 93             org.springframework
 94             spring-jdbc
 95             ${spring.version}
 96         
 97 
 98         
 99             org.springframework
100             spring-tx
101             ${spring.version}
102         
103 
104         
105             org.springframework
106             spring-aop
107             ${spring.version}
108         
109 
110 
111         
112             org.aspectj
113             aspectjweaver
114             1.7.3
115         
116 
117         
118             aopalliance
119             aopalliance
120             1.0
121         
122 
123         <!-- oracle -->
124         
125             com.oracle
126             ojdbc6
127             11.2.0.3
128         
129 
130         <!-- mybatis -->
131         
132             org.mybatis
133             mybatis-spring
134             1.2.2
135         
136 
137         
138             org.mybatis
139             mybatis
140             3.2.7
141         
142 
143         
144             com.github.miemiedev
145             mybatis-paginator
146             1.2.15
147         
148 
149         <!-- struts -->
150         
151             org.freemarker
152             freemarker
153             2.3.19
154         
155 
156         
157             org.apache.struts
158             struts2-core
159             2.3.16.3
160         
161 
162         
163             org.apache.struts.xwork
164             xwork-core
165             2.3.16.3
166         
167 
168         
169             ognl
170             ognl
171             3.0.6
172         
173 
174         
175             commons-beanutils
176             commons-beanutils
177             1.8.3
178         
179 
180         
181             commons-collections
182             commons-collections
183             3.2.1
184         
185 
186         
187             commons-fileupload
188             commons-fileupload
189             1.3
190         
191 
192         
193             commons-io
194             commons-io
195             2.2
196         
197 
198         
199             org.apache.commons
200             commons-lang3
201             3.1
202         
203 
204         
205             javassist
206             javassist
207             3.12.1.GA
208         
209 
210         <!-- Servlet -->
211         
212             javax.servlet
213             servlet-api
214             2.5
215             provided
216         
217 
218     
219 
220     
221         
222             <!-- 开发环境 -->
223             dev
224             
225                 jdbc:oracle:thin:@172.21.129.51:1521:orcl
226                 ckg_rpt
227                 ckg_rpt
228             
229             <!-- 默认激活本环境 -->
230             
231                 true
232             
233         
234         
235             <!-- 测试环境 -->
236             test
237             
238                 jdbc:oracle:thin:@172.21.129.51:1521:orcl
239                 ckg_rpt
240                 ckg_rpt
241             
242         
243     
244 
245     
246         
247             
248                 src/main/resources
249                 true
250             
251         
252         
253             
254                 org.apache.maven.plugins
255                 maven-compiler-plugin
256                 2.5.1
257                 
258                     1.6
259                     1.6
260                     utf-8
261                 
262             
263         
264     
265     
266         infosky
267         
268     
269

主要是maven的关键依赖项,不要漏掉就行

二、web.xml

 1 
 2 
 5 
 6     struts2-helloworld
 7 
 8     
 9         logbackConfigLocation
10         classpath:logback.xml
11     
12     
13         com.cnblogs.yjmyzz.util.LogbackConfigListener
14     
15     
16     
17         contextConfigLocation
18         classpath:spring.xml
19     
20     
21         org.springframework.web.context.ContextLoaderListener
22     
23 
24     
25         struts2
26         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
27         <!-- 
28             loggerFactory
29             com.opensymphony.xwork2.util.logging.slf4j.Slf4jLoggerFactory
30          -->
31     
32 
33     
34         struts2
35         /*
36     
37 
38     
39         index.html
40     
41 
42 


注意上面被注释的一段:虽然xwork提供了com.opensymphony.xwork2.util.logging.slf4j.Slf4jLoggerFactory这个类用于整合SLF4J兼容的各种log实现(比如:logback),但我在实际中发现,logback.xml配置文件始终不起作用,运行时一直加载的是logback的默认配置,所以建议改用自已开发的LogbackConfigListener(代码已经在上一篇2.2中给出)

解释:web.xml中主要干了3件事:监听logback配置文件、监听Spring配置文件、注册Struts2的入口Filter

三、weblogic.xml与jboss-deployment-structure.xml

在weblogic下以war包部署时,由于war是一个压缩包,不会自动展开,所以一些依赖于物理文件路径的代码,可能会有问题,比如报一些:

java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded

之类的错误,解决办法:在WEB-INF下放置一个名为weblogic.xml的文件

 1 
 2 
 3     
 4         /
 5     
 6     
 7         true
 8         true
 9     
10     struts2-helloworld
11     <!--  acmlibrary  -->
12 


注意:第8行true,这里告诉weblogic显示war包的真实路径,以便相关代码在取文件路径时能拿到物理路径。

同样,如果部署在jboss下时,由于jboss默认提供的SLF4J的实现与logback有冲突,需要在WEB-INF下放置jboss-deployment-structure.xml,排除jboss自带的jar包

 1 
 2 
 3     
 4         
 5             
 6             
 7             
 8             
 9         
10     
11 

三、struts2中的Action处理及相关配置

先来一个Action的基类

 1 package com.cnblogs.yjmyzz.action;
 2 
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 
 6 import com.opensymphony.xwork2.ActionSupport;
 7 
 8 public class BaseAction extends ActionSupport {
 9 
10     private static final long serialVersionUID = -4320398837758540242L;
11     protected Logger logger = LoggerFactory.getLogger(this.getClass());
12 }

虽然ActionSupport中有一个LOG的实例变量,但经实践,发现用LOG对象记录的日志,不受logback.xml配置文件控制,所以这里定义一个logger,以便其子类都可以正常使用logback。

其它Action都继承它

 1 package com.cnblogs.yjmyzz.action;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component("HelloWorldAction")
 6 public class HelloWorldAction extends BaseAction {
 7 
 8     private static final long serialVersionUID = -8827776224243873974L;
 9 
10     private String message;
11 
12     public String execute() throws Exception {
13 
14         //log sample
15         for (int i = 0; i trace" + i);
17             logger.debug("logback->debug" + i);
18             logger.info("logback->info" + i);
19             logger.warn("logback->warn" + i);
20             logger.error("logback->error" + i);
21         }
22 
23         return SUCCESS;
24     }
25 
26     public String sayHi() throws Exception {
27         message = "welcome to SSH!";
28         return SUCCESS;
29     }
30 
31     public String getMessage() {
32         return message;
33     }
34 
35     public void setMessage(String message) {
36         this.message = message;
37     }
38 }

注:类上面加上@Component注解,配合这个注释,Spring.xml主配置文件里,配置自动扫描,就不用手动一个一个bean的写配置了

 1 
 2 
13 
14     
15     
16 
17     
18 

运行时Spring会自动注入所有Action的实例,这样在配置struts.xml时,就可以用@Component后面name值来代替完整Action类名,简化配置


 1 
 2 struts PUBLIC
 3         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4         "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 
 7     
 8     
 9         
10             /WEB-INF/views/home/HelloWorld.jsp
11         
12 
13     
14 


注:第9行的class,如果没有Spring,应该写完整路径com.cnblogs.yjmyzz.action.HelloWorldAction,但现在只需要写HelloWorldAction就行了.

四、MyBatis配置

这跟上一篇完全相同,就不再重复了

五、logback.xml配置

 1 
 2 
 4 
 5     
 6     
 7 
 8     
 9 
10     
11         
12             %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
13             
14         
15     
16 
17     
19         ${USER_HOME}/${FILE_NAME}.log
20 
21         
22             ${USER_HOME}/${byDay}/${FILE_NAME}-${byDay}-%i.log.zip
23             
24             1
25             10
26         
27 
28         
30             5MB
31         
32         
33             %d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{150} - %msg%n
34             
35         
36     
37 
38     
39                 
40     
41 
42     
43         
44     
45 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/97/viewspace-2801083/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: vue相关
请登录后发表评论 登录
全部评论

注册时间:2019-02-22

  • 博文量
    83
  • 访问量
    466