一、pom.xml引入struts2-spring-pluginjava
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.5.18</version> </dependency>
二、struts.xml添加常量struts.objectFactory.spring.autoWire.alwaysRespect,使Spring的自动注入老是有效spring
<constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />
经过上述两个配置,便可在Action中实现自动注入。测试例子:apache
在application.properties配置:app
destPath = application.properties
新建util文件夹,新建工具类StrutsConfigjsp
import java.util.Properties; @Component public class StrutsConfig { @Value("${destPath}") private String destPath; public String getDestPath() { System.out.println(destPath); return destPath; } }
新建action类UploadFileAction工具
import com.struts2demo.demo.util.StrutsConfig; import org.springframework.beans.factory.annotation.Autowired; import java.io.File; public class UploadFileAction { @Autowired private StrutsConfig strutsConfig; public String excute() { System.out.println(strutsConfig.getDestPath()); return "error"; } }
struts.xml和jsp的编写就不写了,测试效果: 测试