Struts2 has a DI container internally, and you can read it from the configuration file you use and instantiate the specified class. The function of DI container can be replaced with other DI container, and Spring framework and JSR-330 (Guice) can also be used. Struts2 plug-in is prepared for each container, so if you want to use DI of Spring framework, for example, install Spring plug-in.
By installing the Spring plugin, you can get the following features.
… And that ** most can be replaced with the Spring framework and Spring subprojects **.
Unfortunately, it doesn't provide Spring Boot functionality, but it has more powerful features than the Struts2 standard DI container, so there's no reason not to use it. The corresponding Spring version is up to 4.2 series in Struts2 official, but in fact 4.3 series and later are also available so far.
It's easy to install, just get the plugin and add the settings.
Introduces the exact same Struts2-Spring-plugin as the Struts2 version. The following is the case to specify with maven. Add to the child elements of <dependencies>
.
pom.xml
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
There are two ways to modify the configuration file.
First, add the following line to struts.properties, which is one of the Struts2 settings. The location is arbitrary.
struts.properties
struts.objectFactory=org.apache.struts2.spring.StrutsSpringObjectFactory
Next, set the Spring context and listener in web.xml (deployment descriptor). The following settings are the settings to read the classpath /spring/applicationContext.xml and the xml file starting with applicationContext- as the Spring configuration file.
web.xml
<web-app>
<display-name>prototype_app</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml,classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
That's all for the settings to use. After this, set applicationContext.xml to register the beans to be put in Spring management.
Classes under Spring management can be used by @Autowired
in Action class. The following example is a simple list search sample.
SampleAction.java
@Namespace("/")
@Results({@Result(name=ActionSupport.SUCCESS, location="list")})
@Log4j2
public class DisplayListAction extends ActionSupport {
@Action("list")
public String execute() throws Exception {
products = service.search();
log.info("- search:{}" , products);
return SUCCESS;
}
@Autowired
ProductService service;
@Getter @Setter
List<SampleProduct> products;
}
The Service class (ProductService) used from this Action class returns a fixed list like this:
ProductService.java
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
public List<SampleProduct> search() {
List<SampleProduct> resultList =
Arrays.asList(
new SampleProduct().setProduct("A-1", "Prototype X-290PA1", 10, true, true),
new SampleProduct().setProduct("A-2", "Prototype X-290PA2", 20, true, true),
new SampleProduct().setProduct("B-3", "Prototype X-B3", 10, true, true),
new SampleProduct().setProduct("B-4", "Prototype X-B4", 30, true, true),
new SampleProduct().setProduct("C-5", "Prototype X-C5", 10, true, false),
new SampleProduct().setProduct("C-6", "Prototype X-C6", 40, true, false),
new SampleProduct().setProduct("D-7", "Prototype X-D7", 10, false, true),
new SampleProduct().setProduct("D-8", "Prototype X-D8", 50, false, true),
new SampleProduct().setProduct("E-9", "Prototype X-E9", 10, true, true),
new SampleProduct().setProduct("Z-1000", "Prototype Z-1000", 0, false, false)
);
return resultList;
}
}
The Sample Product that appears here is a simple Data Transfer Object. Since it is a search result, you can make it a Value Object.
Unfortunately JavaConfig cannot be used, so define it in the xml file. Since component-scan can be used, beans can be registered at once.
applicationContext-service.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
default-autowire="byName"
> <!--Service class definition to be managed by Spring-->
<context:component-scan base-package="seren.struts2.sampleapp.service" use-default-filters ="false" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
</beans>
The target of component-scan is registered the class with @Service
annotation in base-package.
That's all for the settings. It's easy once you make it v (・ ω ・ |
https://github.com/A-pZ/struts2-spring-sample
You can also use Thymeleaf3 from Struts2. You can't use Struts2 tags at all, but Thymeleaf will make it easier to write a View. It will be very useful when switching from Struts2 to Spring framework etc.
http://qiita.com/alpha_pz/items/5a43b4abb07141becbcd
Recommended Posts