spring boot集成struts2

java技术文章

2018-10-20

249

0

大家好,今天跟大家说说spring boot怎么集成struts2,因为spring boot大多数使用注解的原因(虽然spring boot任然支持xml配置),并且集成struts2资料不多,所以本人花了不少时间。现在写出来和大家分享一下。

一、我的项目结构

二、使用maven引入依赖的jar包

我这里就只写当前实例需要的jar包,如果需要其他的jar包,请自行添加

<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>mcr</groupId>
  <artifactId>mco.webbase</artifactId>
  <version>1.0.0</version>
 	 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
	<dependencies>
		<!-- 表示可以发布web程序 自动启动一个tomcat -->
	    <dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-web</artifactId>
	    </dependency>
		<!-- 添加转译jsp的jar -->
	    <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- 自动监听配置文件和jsp -->
		<dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-devtools</artifactId>
	        <optional>true</optional>
	    </dependency>
		<dependency>
		  <groupId>org.apache.struts</groupId>
		  <artifactId>struts2-core</artifactId>
		  <version>2.3.28</version>
		</dependency>
		<dependency>
		  <groupId>org.apache.struts</groupId>
		  <artifactId>struts2-spring-plugin</artifactId>
		  <version>2.3.28</version>
		</dependency>
		<dependency>
		  <groupId>org.apache.struts</groupId>
		  <artifactId>struts2-convention-plugin</artifactId>
		  <version>2.3.28</version>
		</dependency>
	</dependencies>
</project>

三、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>

	<constant name="struts.devMode" value="true" />
    <constant name="struts.objectFactory" value="spring" />
	<constant name="struts.objectFactory.spring.autoWire" value="name" />
	<constant name="struts.action.extension" value="do," />
	<!--这里表示扫描actions目录下的Action类-->
	<constant name="struts.convention.package.locators" value="actions" />
    <constant name="struts.convention.result.path" value="/" />
    <constant name="struts.action.excludePattern" value="/js/*,/css/*" />
    <constant name="struts.serve.static.browserCache " value="false" />
    <constant name="struts.convention.classes.reload" value="false" />
	<!-- 
		SpringBoot集成struts2这个必须要配置  
		表示不把SpringBoot内置Tomcat的类加载器排除在外  
		 默认是true 将类加载器排除了所以就算上面配置
		如果这个没有配置就算运行不报错但是Action还是访问不了 
	-->
    <constant name="struts.convention.exclude.parentClassLoader" value="false"></constant>
</struts>

四、过滤器Filter添加

因为spring boot不再使用web.xml文件,所以我们需要在代码中添加struts2过滤器,将下面代码添加到main类中

@Bean
public FilterRegistrationBean filterRegistrationBean() {
	FilterRegistrationBean registrationBean = new FilterRegistrationBean();
	StrutsPrepareAndExecuteFilter struts = new StrutsPrepareAndExecuteFilter();
	registrationBean.setFilter(struts);
	registrationBean.setOrder(1);
	return registrationBean;
}

我的入口类代码如下:

/**
 * Project Name:mco.webbase
 * File Name:WebApplication.java
 * Package Name:com.mco
 * Date:2018年8月27日下午9:58:03
 * Copyright (c) 2018, 649445087@qq.com All Rights Reserved.
 *
*/

package com.mco;

import org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * ClassName:WebApplication 

 * Function: TODO ADD FUNCTION. 

 * Reason:	 TODO ADD REASON. 

 * Date:     2018年8月27日 下午9:58:03 

 * @author   LUOFUJIA
 * @version  
 * @jdk version JDK 1.8
 * @see 	 
 */
@SpringBootApplication
@EnableAutoConfiguration(exclude= {FreeMarkerAutoConfiguration.class,RestTemplateAutoConfiguration.class,SecurityAutoConfiguration.class,SecurityFilterAutoConfiguration.class})
@ComponentScan(basePackages= {"com.mco"})
@EnableJpaRepositories(basePackages = {"com.mco"})
@EntityScan(basePackages = {"com.mco"})
public class WebApplication  extends SpringBootServletInitializer {
    
    private final static Logger logger = LoggerFactory.getLogger(WebApplication.class);
    
    public WebApplication(){
        
    }
    
    public static void main(String args[]) {
        logger.info("WebBaseApplication start..........................................");
        new WebApplication().run(args);
        logger.info("WebBaseApplication end..........................................");
    }

    public void run(String args[]) {
        SpringApplication application = new SpringApplication(WebApplication.class);
        application.run(args);
    }

    
    @Override   
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }
    
    @SuppressWarnings({"rawtypes", "unchecked" })
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        StrutsPrepareAndExecuteFilter struts = new StrutsPrepareAndExecuteFilter();
        registrationBean.setFilter(struts);
        registrationBean.setOrder(1);
        return registrationBean;
    }
 
}

五、添加Action类

/**
 * Project Name:mco.webbase
 * File Name:TestAction.java
 * Package Name:com.mco.webbase.web
 * Date:2018年10月17日下午10:06:15
 * Copyright (c) 2018, 649445087@qq.com All Rights Reserved.
 *
*/

package com.mco.web.actions;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

/**
 * ClassName:TestAction 

 * Function: TODO ADD FUNCTION. 

 * Reason:	 TODO ADD REASON. 

 * Date:     2018年10月17日 下午10:06:15 

 * @author   LUOFUJIA
 * @version  
 * @jdk version JDK 1.8
 * @see 	 
 */

@Controller
public class TestAction extends ActionSupport{
    
    
    @Action(value="test-list",results = {
                    @Result(name="success",location="/hello.jsp")}) 
    public String list(){
//       //获取 输出流
        OutputStream os;
        try {
            os = ServletActionContext.getResponse().getOutputStream();
            os.write("hello.jsp".getBytes());
        }
        catch (IOException e) {
            
        }
        return NONE;
    }
}

六、访问项目

输入地址:http://127.0.0.1:8090/mcoBase/test-list.do

效果如下:

欢迎访问:www.hongfu951.com博客,查看更多文章

发表评论

全部评论:0条

鸿福951

努力打造一个好用的webui

热评文章

推荐文章