您当前的位置: 首页 >  spring

Charge8

暂无认证

  • 1浏览

    0关注

    447博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot 整合JSP

Charge8 发布时间:2019-05-18 23:03:28 ,浏览量:1

SpeingBoot 官方是不推荐使用jsp这个引擎模板的,所有它的默认配置中是没有配置jsp支持的。

一、SpringBoot 整合JSP

1、在pom.xml文件中导入jsp依赖的jar包,一个是jstl标签,一个是jsp的引擎

        
            org.apache.taglibs
            taglibs-standard-spec
            1.2.5
        
        
            org.apache.taglibs
            taglibs-standard-impl
            1.2.5
        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            8.5.34
        

2、在SpringBoot的全局配置文件中修改SpringBoot的默认视图解析器的前缀和后缀

======= YAML 文件=====
server:
  port: 80

## jsp支持
spring:
  mvc:
    view:
      prefix: /WEB-INF/view/
      suffix: .jsp

## 关闭默认模板引擎
thymeleaf:
    cache: false
    enable-spring-el-compiler: false

======= properties 文件=====
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp

spring.thymeleaf.cache=false
spring.thymeleaf.enabled=false

或者通过JavaConfig的方式:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    /**
     * 配置视图解析器
     *
     * @return
     */
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

}

或者

@Configuration
public class WebConfig implements  WebMvcConfigurer{

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/jsp/", ".jsp");
    }

}

3、创建一个Controller测试

@Controller
public class IndexController {

    @GetMapping("/indexJsp")
    public String indexJsp(Model model){
        User user = new User();
        user.setUsername("张三");
        user.setAge(18);
        user.setBirthday(new Date());
        model.addAttribute("users", user);
        return "indexJsp";
    }
}

4、在 src/main/webapp/WEB-INF/view/ 下创建 jsp文件

自己需要创建webapp,下面又演示。





    JSP


    JSP 页面
        ${users}
        用户名:${users.username}
        年龄:${users.age}
        生日:${users.birthday}

访问项目:

       

 

二、创建 JSP 时,需要进行一点设置

     方式一:创建 webapp文件夹,然后设置

选择 File  -> project structure

    

  设置完成之后就可以创建 JSP 文件啦

    

方式二:在 Modules中直接创建配置

选中module,按F4,或者File -> project Structure。然后同上图,创建webapp,并设置。

 

  ends ~

关注
打赏
1664721914
查看更多评论
立即登录/注册

微信扫码登录

0.0407s