Spring Boot implements JSP analysis

Spring Boot makes it easy to create independent, production-level Spring-based applications that can be "run". Most Spring Boot applications require very little Spring configuration.

Function
to create a separate Spring applications
directly embedded Tomcat, Jetty or Undertow (do not need to deploy the WAR file)
provides easy to use Maven POM to simplify your configuration
automatically configures Spring as much as possible
to provide production-ready features such as indicators, health checks and externalized Configuring
absolutely no code generation and does not require XML configuration. (taken from official documents)

When we were working on Spring, we found that there are many configurations whether using xml or java, and all kinds of errors will be reported if we are not careful.
And if you use Spring Boot indeed a lot of convenience, at least a lot less configuration, we can go directly to the official website to generate a run for it.

Not much to say, let's just look at a simple demo.

1.pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>spring-boot-example</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-boot-example Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- Tomcat Embed -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2. The application.properties file is mainly to configure the JSP path, prefix and suffix, equivalent to the function of springmvc-servlet.xml.

spring.mvc.view.prefix=/jsp/
spring.mvc.view.suffix=.jsp
#自定义error需要
server.error.whitelabel.enabled = false

3. HelloController:
You need to pay attention here. If you want to define your own error, you need to implement ErrorController, because the Spring Boot system will map /error by default.


@Controller
public class HelloController implements ErrorController{
    
    

    @RequestMapping("/")
    public String showIndex() {
        return "index";
    }

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

    @RequestMapping("/error")
    public String showError() {
        return getErrorPath();
    }

    @Override
    public String getErrorPath() {
    return "error";
    }
}

4.error.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
I'm error
</body>
</html>

5.hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
I'm ${name}
</body>
</html>

6.index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="/hello?name=mike">click here</a>
</body>
</html>

7. Start the class WebApplication.java and define the main method to start.
Of course, you can also use maven to start: mvn spring-boot:run
or package start:
mvn package
java -jar target/spring-boot-example-0.0.1-SNAPSHOT.jar

Here the SpringBootApplication annotation already contains @SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan and other annotations

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

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

10. Finally start the test:
input: http://localhost:8080/
homepage:
Write picture description here

Click link: display according to the parameters you passed
Write picture description here

错误:http://localhost:8080/error
Write picture description here

Reference: spring official website
mkyong

At this point, the basic framework has been set up. It can be seen that we have basically no configuration and quickly set up the environment, and if it is to be integrated with other frameworks such as mybatis, there are also related jar packages that can be introduced.

Guess you like

Origin blog.csdn.net/u010857795/article/details/77072912