Java 解析PDF内容
我们在进行web开发时经常会涉及到图片上传的问题,特别是利用富文本编辑器进行图片上传,业界也有很多的富文本编辑器,有ueditor、fckeditor、wangEditor。这里我们选择wangEditor作为示例,因为wangEditor是一个轻量级的富文本编辑器已经可以满足我们大部分的需求。这里结合spring boot实现图片上传,有了图片上传其实文件的上传也是一回事了。 ##wangEditor 目前wangEditor已经时v3版本了,这里给出链接wangEditor,上面有详细的介绍。 ##spring boot 我们依然采用spring boot作为服务端开发 首先我们在resources目录添加了一个configure.properties文件里面只有一行,定义了我们上传文件存储的目录
##用于存储系统中可能涉及到的各种配置信息 material.upload_dir=/home/jacky/upload-dir
有时候我们会遇到这么一些需求,在filter中获取一些参数进行处理,同时将处理好的参数重新添加到request对象中,这时候我们在filter中直接使用request.setAttribute()是无效的。我们怎么来解决这个问题呢,j2ee已经给我们提供了解决的办法,使用HttpServletRequestWrapper类来解决向request添加额外参数的功能。
环境:spring boot
之前我介绍了在spring boot中使用thymeleaf模板,这次我会给大家介绍在spring boot中使用freemarker模板技术,同时利用freemarker生成静态html页面。生成静态html页面就能实现网站的静态化进而提高网站的访问速度以及提高SEO能力。 首先在pom.xml中添加依赖 ##添加依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
##application配置 在application.properties中添加freemarker的配置参数
在我们使用Spring boot进行web开发时都会遇到引入各类js、css、html、image等各种静态资源文件,这时候我们就需要进行相应的配置来允许应用访问这些静态资源。 我们还是基于之前的采用thymeleaf 作为模板的 spring boot来开发 Spring Boot使用Thymeleaf
之前给大家介绍了在spring boot开发中使用jsp模板,但是jsp目前并不是spring boot推荐的做法,同时使用jsp还有一些限制而Thymeleaf则是spring boot官方推荐的模板引擎。 对于Thymeleaf我就不过多介绍网上资料很多,我们直接进入正题,在spring boot中使用Thymeleaf 开发环境
spring boot + Themeleaf 3
先上pom.xml
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.8.RELEASE</version>
</dependency>
我在这里并没有引入spring boot 官方的thymeleaf包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
而是引入Thymeleaf的包
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.8.RELEASE</version>
</dependency>
因为目前spring boot官方的依然引入的是thymeleaf 2.1.5这个版本的包而不是最新的3.0.8,而Thymeleaf 2 和Thymeleaf 3之间的差别比较大。
引入好包之后我们就可以开始进行配置了,首先是spring boot的主类
package com.hw.myp2c;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Myp2cMainApplication {
public static void main(String[] args) {
SpringApplication.run(Myp2cMainApplication.class, args);
}
}
package com.hw.myp2c;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Myp2cMainApplication.class);
}
}
很简单的控制类
package com.hw.myp2c.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("")
public class MainController {
@GetMapping
public String main(Model model){
model.addAttribute("w","Welcome Thymeleaf!");
return "main";
}
}
然后是Thymeleaf的模板html文件,注意目录是在main下建立以下目录webapp/WEB-INF/templates,所有的html模板都放在templates下面供Thymeleaf引擎解析
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${w} + '!'" />
</body>
</html>
好了这是我们需要建立的各类文件,下面我们来做一些配置让spring boot识别并整合ThymeleafThymeleaf的配置有两种方法:
application.properties配置 javabean配置
两种方法各有优势,如果仅仅是想简单的使用Thymeleaf那就用第一种配置方法,如果想进行一些定制化的配置那就采用第二种方法 首先我们介绍第一种配置方法,很简单就是在application.properties文件中添加如下内容就行了
spring.thymeleaf.cache=true spring.thymeleaf.check-template=true spring.thymeleaf.check-template-location=true spring.thymeleaf.enabled=true spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.mode=HTML spring.thymeleaf.prefix=/WEB-INF/templates/ spring.thymeleaf.suffix=.html
这样我们就配置好了直接执行mvn spring-boot:run测试运行结果
Hello, Welcome Thymeleaf!!
下面我们介绍第二种配置方法,基于javabean的配置,首先我们创建一个类SpringWebConfig.class,在这个类里面完成Thymeleaf的配置
package com.hw.myp2c;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
@Configuration
@EnableWebMvc
@ComponentScan
public class SpringWebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{
private ApplicationContext applicationContext;
public SpringWebConfig() {
super();
}
public void setApplicationContext(final ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
/* ******************************************************************* */
/* GENERAL CONFIGURATION ARTIFACTS */
/* Static Resources, i18n Messages, Formatters (Conversion Service) */
/* ******************************************************************* */
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/images/**").addResourceLocations("/images/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("Messages");
return messageSource;
}
@Bean
public SpringResourceTemplateResolver templateResolver(){
// SpringResourceTemplateResolver automatically integrates with Spring's own
// resource resolution infrastructure, which is highly recommended.
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
// HTML is the default value, added here for the sake of clarity.
templateResolver.setTemplateMode(TemplateMode.HTML);
// Template cache is true by default. Set to false if you want
// page to be automatically updated when modified.
templateResolver.setCacheable(true);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine(){
// SpringTemplateEngine automatically applies SpringStandardDialect and
// enables Spring's own MessageSource message resolution mechanisms.
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
// Enabling the SpringEL compiler with Spring 4.2.4 or newer can
// speed up execution in most scenarios, but might be incompatible
// with specific cases when expressions in one template are reused
// across different data types, so this flag is "false" by default
// for safer backwards compatibility.
templateEngine.setEnableSpringELCompiler(false);
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolver(){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
}
分别配置了对静态资源(js/css/images)的处理、templateResolver(模板解析器)、templateEngine(模板引擎)、viewResolver(视图解析器)
我记得在spring boot出来之前做框架整合是一件非常不愉快的事情,各种jar包版本冲突是非常糟糕的体验,同时那时候主要还是通过xml文件来进行配置,各种配置文件十分繁多,hibernate需要,spring需要,如果用struts还需要,总之就是一个及其麻烦的事情。 当我第一次见到spring boot的时候我们被惊艳到了,原来开发可以如此的流畅,真正的约定大于配置,各种方便的注解,包整合十分愉快。 这篇文章就是简单记录一下利用最简单的spring boot快速开发一个web小应用。
开发环境spring boot + maven
##(一) 磁盘准备
通过cfdisk分区工具将硬盘进行分区操作,总计分为4个区如下所示
/deve/sda1 efi 分区
/dev/sda2 swap 分区
/dev/sda3 root 分区
/dev/sda4 home 分区
在利用spring data jpa开发的时候为了解决一些复杂的查询需求这时候我们需要引入本地查询nativeQuery 参照官方的例子
Spring Data是Spring提供的对数据库的访问方式而Spring Data JPA是Spring Data的一部分,是JPA的Spring 实现方案。通过Spring Data JPA可以很方便的利用spring-framew构建基于数据库的应用程序。 Spring Data JPA是JPA的spring实现因此包含了很多详细的应用方式,特别是各种类型的查询检索方式,本文只是简单的涉及数据库的增删改查也就是CRUD操作。
代码环境:maven+spring-boot