springboot学习(五)spring boot 整合jsp

阅读更多

最近在学习springboot,根据网上各位大神的博客以及官网例子,经历了小白的各种艰难险阻,尚未达到理想效果。spring boot整合jsp,可是把我整够了,各种搜学习资料,但是我不乐意直接放大神们的代码,所以我小坑略多。

  1. pom.xml需要引入相关支持
    
    
    	4.0.0
    
    	com.example
    	spring-boot-sample-jsp1
    	0.0.1-SNAPSHOT
    	war
    
    	spring-boot-sample-jsp1
    	Demo project for Spring Boot
    
    	
    		org.springframework.boot
    		spring-boot-starter-parent
    		1.5.7.RELEASE
    		 
    	
    
    	
    		UTF-8
    		UTF-8
    		1.8
    	
    
    	
    		
    			org.springframework.boot
    			spring-boot-starter-web
    		
    
    		
    			org.springframework.boot
    			spring-boot-starter-tomcat
    			provided
    		
    		
    			org.springframework.boot
    			spring-boot-starter-test
    			test
    		
    
    		
    		
    			org.springframework.boot
    			spring-boot-starter-tomcat
    			provided
    		
    
    		
    		
    			org.apache.tomcat.embed
    			tomcat-embed-jasper
    			required
    		
    	
    
    	
    		
    			
    				org.springframework.boot
    				spring-boot-maven-plugin
    			
    		
    	
    
    
     
  2. 测试controller,需要留意这里要注解是@Controller,不是@RestController,否则怎么配置都不会返回页面的,后者返回的是内容,各位可以去自习了解下
    package com.example.springbootsamplejsp1;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /** 
     * 功能说明
     * @date 2017年9月22日 下午3:00:06
     * @author wusj 
     * @version 1.0
     */
    @Controller
    public class TestController {
    
        @RequestMapping("/index")
        public String index(Map map){
            map.put("name", "wusj");
            return "index";
        }
    }
    
  3. 新建maven项目时,需要选择war,而非jar,我不知道jar包该如何弄。我一开始哪里都对的,就是项目是jar包,而非war包,一直报错:This application has no explicit mapping for /error, so you are seeing this as a fallback
    springboot学习(五)spring boot 整合jsp_第1张图片
  4. 启动类需要继承SpringBootServletInitializer,或者单独写个类继承它ServletInitializer.java
    package com.example.springbootsamplejsp1;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    
    import com.example.SpringBootSampleJsp1Application;
    
    public class ServletInitializer extends SpringBootServletInitializer {
    
    	@Override
    	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    		return application.sources(SpringBootSampleJsp1Application.class);
    	}
    
    }
    
  5. 配置文件需要配置相关信息
    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
  6. 以上都做到了还出不来的话,初步判断可能rp不如我好。
  7. 希望大家不要走弯路,嘻嘻。项目结构如下
    springboot学习(五)spring boot 整合jsp_第2张图片
     
  • springboot学习(五)spring boot 整合jsp_第3张图片
  • 大小: 3.3 KB
  • springboot学习(五)spring boot 整合jsp_第4张图片
  • 大小: 22.2 KB
  • 查看图片附件

你可能感兴趣的:(springboot,jsp)