与前面的一篇mybatis 3.2.7 与 spring mvc 3.x、logback整合 相比,只是web层的MVC前端框架,从spring mvc转换成struts 2.x系列,变化并不大
一、pom.xml 配置
1 24 4.0.0 5infosky 6struts2-helloworld 7war 81.0 910 141.6 112.3.16.3 123.2.8.RELEASE 1315 16 <!-- db driver --> 17 219 22018 22 23 24 <!-- log --> 25commons-dbcp 19commons-dbcp 201.4 2126 30 31org.slf4j 27slf4j-api 281.7.7 2932 36 37ch.qos.logback 33logback-core 341.1.2 3538 42 43 <!-- spring --> 44ch.qos.logback 39logback-classic 401.1.2 4145 49 50org.apache.struts 46struts2-spring-plugin 472.3.16.3 4851 55 56org.springframework 52spring-context 53${spring.version} 5457 61 62org.springframework 58spring-beans 59${spring.version} 6063 67 68org.springframework 64spring-core 65${spring.version} 6669 73 74org.springframework 70spring-web 71${spring.version} 7275 79 80org.springframework 76spring-context 77${spring.version} 7881 85 86org.springframework 82spring-context-support 83${spring.version} 8487 91 92org.springframework 88spring-expression 89${spring.version} 9093 97 98org.springframework 94spring-jdbc 95${spring.version} 9699 103 104org.springframework 100spring-tx 101${spring.version} 102105 109 110 111org.springframework 106spring-aop 107${spring.version} 108112 116 117org.aspectj 113aspectjweaver 1141.7.3 115118 122 123 <!-- oracle --> 124aopalliance 119aopalliance 1201.0 121125 129 130 <!-- mybatis --> 131com.oracle 126ojdbc6 12711.2.0.3 128132 136 137org.mybatis 133mybatis-spring 1341.2.2 135138 142 143org.mybatis 139mybatis 1403.2.7 141144 148 149 <!-- struts --> 150com.github.miemiedev 145mybatis-paginator 1461.2.15 147151 155 156org.freemarker 152freemarker 1532.3.19 154157 161 162org.apache.struts 158struts2-core 1592.3.16.3 160163 167 168org.apache.struts.xwork 164xwork-core 1652.3.16.3 166169 173 174ognl 170ognl 1713.0.6 172175 179 180commons-beanutils 176commons-beanutils 1771.8.3 178181 185 186commons-collections 182commons-collections 1833.2.1 184187 191 192commons-fileupload 188commons-fileupload 1891.3 190193 197 198commons-io 194commons-io 1952.2 196199 203 204org.apache.commons 200commons-lang3 2013.1 202205 209 210 <!-- Servlet --> 211javassist 206javassist 2073.12.1.GA 208212 217 218javax.servlet 213servlet-api 2142.5 215provided 216221 244 245222 <!-- 开发环境 --> 223 234dev 224225 229 <!-- 默认激活本环境 --> 230jdbc:oracle:thin:@172.21.129.51:1521:orcl 226ckg_rpt 227ckg_rpt 228231 233true 232235 <!-- 测试环境 --> 236 243test 237238 242jdbc:oracle:thin:@172.21.129.51:1521:orcl 239ckg_rpt 240ckg_rpt 241246 265247 252248 251src/main/resources 249true 250253 264254 263org.apache.maven.plugins 255maven-compiler-plugin 2562.5.1 257258 2621.6 2591.6 260utf-8 261266 269infosky 267268
主要是maven的关键依赖项,不要漏掉就行
二、web.xml
1 25 6 struts2-helloworld 7 89 12logbackConfigLocation 10classpath:logback.xml 1113 15 16com.cnblogs.yjmyzz.util.LogbackConfigListener 1417 20contextConfigLocation 18classpath:spring.xml 1921 23 24org.springframework.web.context.ContextLoaderListener 2225 32 33struts2 26org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 27 <!--28 --> 31loggerFactory 29com.opensymphony.xwork2.util.logging.slf4j.Slf4jLoggerFactory 3034 37 38struts2 35/* 3639 41 42index.html 40
注意上面被注释的一段:虽然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 23 4 6/ 57 10true 8true 9struts2-helloworld 11 <!----> 12 acmlibrary
注意:第8行
同样,如果部署在jboss下时,由于jboss默认提供的SLF4J的实现与logback有冲突,需要在WEB-INF下放置jboss-deployment-structure.xml,排除jboss自带的jar包
1 23 4 115 106 7 8 9
三、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 213 14 15 16 1718
运行时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"> 56 7 8 9 1410 12 13/WEB-INF/views/home/HelloWorld.jsp 11
注:第9行
四、MyBatis配置
这跟上一篇完全相同,就不再重复了
五、logback.xml配置
1 24 5 6 7 8 9 10 11 16 1712 15%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 13 1419 37 38${USER_HOME}/${FILE_NAME}.log 20 2122 27 28${USER_HOME}/${byDay}/${FILE_NAME}-${byDay}-%i.log.zip 23 241 2510 2630 325MB 3133 36%d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{150} - %msg%n 34 3539 41 4240 43 4544
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/97/viewspace-2801083/ ,如需转载,请注明出处,否则将追究法律责任。