因为最近有点闲,所以又拿起了原来折磨我千百遍的springboot,作为一个做后端的人,本来不应该关心前端支持的这些事的,但是公司项目必须要用jsp,而我们对springboot推荐的thymeleaf也不是很熟悉,所以也就只能继续使用jsp了。我原来在学习springboot的时候,一直就卡到了jsp这里,在查了无数资料没有用后,还是自己找到了原因。这里首先说一下基本构建(我用的springboot版本是2.0.2):
1、新建项目,添加jsp支持
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> <!-- 如果部署到tomcat,这里必须为provided -->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
这里包含了支持jsp所需的依赖包
2、把jsp页面放到webapp目录下,这个目录需要建在项目根目录:

3、在配置文件中添加配置:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
4、在程序起始类继承SpringBootServletInitializer,重写configure方法
@SpringBootApplication
@MapperScan(basePackages = "com.pcbwx.jsp.dao") // mybatis包路径
public class SystemStart extends SpringBootServletInitializer {
private static Logger logger = LoggerFactory.getLogger(SystemStart.class);
public static void main(String[] args) throws Exception {
SpringApplication springApplication = new SpringApplication(SystemStart.class);
springApplication.run(args);
logger.info("系统已经启动");
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SystemStart.class);
}
}
一般完成这4步jsp构建就可以了,你可以通过视图访问页面
@Controller
public class WelcomeController {
// 登录页
@GetMapping("/login")
public String login() {
return "login";
}
}

但是springboot还是有很多坑的,因为springboot不建议用jsp,所以会有很多的依赖会与jsp相冲突,我这里只说一种情况,也是我遇到的情况。
因为我需要把所写接口中的null转化成"";所以我配置了这个类:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport{
// 把返回Json中的null换为""
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
也就是因为这个WebMvcConfigurationSupport类,与jsp相冲突,所以我在加入这个配置以后,原本可以访问的页面也报错了。

后来我把WebMvcConfigurationSupport换成了WebMvcAutoConfiguration,就没有再出现原来的问题:
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcAutoConfiguration {
// 把返回Json中的null换为""
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
我在网上查资料的时候,也看到有的人因为在项目中加入了thymeleaf的依赖导致jsp页面不能访问。但是因为我没有用thymeleaf,所以也就没有遇到这个问题,在这里只是把这个可能导致的原因提一下。
这里附上程序源码:https://github.com/HeyuRise/jsp
网友评论