1.新建一个springboot工程,并在pom文件中引入UReport2的jar包依赖,ureport2选用2.2.9版本,
poi选用3.15以上的版本,我选择的3.17版本
<!--ureport--> <dependency> <groupId>com.bstek.ureport</groupId> <artifactId>ureport2-console</artifactId> <version>2.2.9</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
2.新建一个类UreportConfig,并实现BuildinDatasource接口,其中,ServletRegistrationBean需要注入到spring容器中,目的是访问ureport报表的请求url规则,同时通过@ImportResource注解扫描ureport2源码包中的ureport-console-context.xml配置进入到容器中
package org.fh.config;
import com.bstek.ureport.console.UReportServlet;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import java.sql.Connection;
import java.sql.SQLException;
@ImportResource("classpath:ureport-console-context.xml")
@Configuration
@EnableAutoConfiguration
public class UreportConfig implements BuildinDatasource {
@Resource
public DataSource dataSource;
private Logger log = LoggerFactory.getLogger(getClass());
@Bean //定义ureport的启动servlet
@SuppressWarnings("unchecked")
public ServletRegistrationBean ureportServlet(){
return new ServletRegistrationBean(new UReportServlet(),"/ureport/*");
}
@Override
public String name() {
return "myDatasource";
}
@Override
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
log.error("Ureport 数据源 获取连接失败!");
e.printStackTrace();
}
return null;
}
}
如果是ssm框架可以通过在web.xml中进行这样的配置。同上
<servlet>
<servlet-name>ureportServlet</servlet-name>
<servlet-class>com.bstek.ureport.console.UReportServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ureportServlet</servlet-name>
<url-pattern>/ureport/*</url-pattern>
</servlet-mapping>
如果是ssm框架,可以通过spring.xml文件的方式。将ureport-console-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"
default-lazy-init="true">
<import resource="classpath:ureport-console-context.xml"/>
</beans>
其中name的配置是配置默认数据源
@Override
public String name() {
return "myDatasource";
}
访问的网址就可以看到这样的操作
因为我的项目通过java类注入的方式配置了端口和项目访问名称,8081端口,fh-admin-system作为根路径
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> myWebServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.setPort(8081);
factory.setContextPath("/fh-admin-system");
}
};
}
相当于, 在application.xml中配置,通过启动springboot项目,自动装配扫描是否有配置。
server.port=8081 server.servlet.context-path=/fh-admin-system
ureport2主页