Spring Boot2 with JSP View


前言:

Spring Boot2 使用JSP视图创建Web应用程序

  • JDK 8+或OpenJDK 8+
  • Maven 3+

1.从Spring Initializer构造项目结构:

Spring Boot2 with JSP View_第1张图片

2.pom.xml



   4.0.0
   
      org.springframework.boot
      spring-boot-starter-parent
      2.1.6.RELEASE
       
   
   com.eprogrammerz.examples.spring
   spring-boot-jsp
   0.0.1-SNAPSHOT
   spring-boot-jsp
   Example Spring Boot with JSP view

   
      1.8
   

   
      
         org.springframework.boot
         spring-boot-starter-web
      

      
         org.springframework.boot
         spring-boot-starter-tomcat
         provided
      

      
         org.apache.tomcat.embed
         tomcat-embed-jasper
         provided
      

      
         org.springframework.boot
         spring-boot-starter-test
         test
      
   

   
      
         
            org.springframework.boot
            spring-boot-maven-plugin
         
      
   

3.Configuration

3.1 Application Configurations(使用SpringBootServletInitializer加载)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootJspApplication extends SpringBootServletInitializer {
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(SpringBootJspApplication.class);
   }

   public static void main(String[] args) {
      SpringApplication.run(SpringBootJspApplication.class, args);
   }

}

3.2 application.properties

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

3.3 Code

controller:

@Controller
public class HelloController {
    @GetMapping({"/", "/hello"})
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
        model.addAttribute("name", name);
        return "hello";
    }
}



jsp:




    
    Hello ${name}!


    

Hello ${name}!

4. maven运行,访问 localhost:8080 应用: 

mvn clean spring-boot:run

 

你可能感兴趣的:(Java,微服务)