It is easy to configure access to JSP pages when using SPRINGMVC directly, but because spring boot uses an inline servlet container, the support for the JSP is not very good and the JSP is not recommended. But in order to satisfy this need to return JSP page and want to enjoy spring boot to avoid the trouble of various configurations, through the various experiments, there are two methods for reference, in the following two methods are described before the first declaration of the dependency is as follows:
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid></Dependency><Dependency> <groupId>Org.apache.tomcat.embed</groupId> <Artifactid>Tomcat-embed-jasper</Artifactid> <Scope>Provided</Scope></Dependency>
spring-boot-starter-webis used to support Web application development, after the import will automatically import the embedded servlet container, for example Tomcat , but note that at this time there is no Jasper to parse the JSP page module, so you can see that the embedded is servlet容器 not all imported, but the module part of the import, Jasper need us to import manually. If you also want to use the JSTL library, you can continue importing:
< Dependency > < groupId >javax.servlet</groupId> <Artifactid >jstl</artifactid></Dependency >
1. Return to JSP page in Project
Because the directory structure of the spring boot and the normal Web application directory structure is different, in order to return the JSP, the first to set up the corresponding Web application directory, /src/main under the WEB-INF/jsp directory, form /src/main/WEB-INF/jsp , and then put our JSP page below.
Then continue with the configuration file as follows:
Application.properties
spring.mvc.view.prefix=/web-inf/jsp/spring.mvc.view.suffix=.jsp
or APPLICATION.YML.
Spring: MVC: view: prefix:/web-inf/jsp/ suffix:. jsp
This is similar to the configuration when using SPRINGMVC only, and you can see that the InternalResourceViewResolver View parser is being used. Test with code:
@Controller
Public class Springbootcontroller {
@RequestMapping ("/index")
Public String Index () {
return "main";
}
}
The above will return the /src/main/WEB-INF/jsp/main.jsp view.
however , the return page will only work if the Spring boot program is run directly in the Eclipse IDE, and if the package is packaged as a jar, it will not contain the WEB-INF/jsp directory we created , and there will be no JSP pages that we define. So it will return 404状态码 , so this method is actually meaningless, but it can be solved:
① to change the way the jar package to the war package
② Modifying the Pom.xml file configuration
<!--ignore no web. XML Warning -<plugin> <groupId>Org.apache.maven.plugins</groupId> <Artifactid>Maven-war-plugin</Artifactid> <Configuration> <Failonmissingwebxml>False</Failonmissingwebxml> </Configuration> </plugin>
<Resource> <Directory>Src/main/resources</Directory> <includes> <include>**/*</include> </includes></Resource> <!--copy the JSP files to the Meta-inf directory when packaging - <Resource> <!--Specifies the resource file under which directory the resources plug-in handles - <Directory>Src/main/webapp</Directory> <!--Note This must be placed in this directory to be accessible to - <TargetPath>Meta-inf/resources</TargetPath> <includes> <include>**/**</include> </includes> </Resource>
③ static file storage directory src/main/resources, the default static resource file location is/static
JSP directly referencing static files
<src= "jquery-3.1.1.min.js" type= "Text/javascript"> </script><src= "main.js" type = "Text/javascript"></script>
④ into a war package, everything works.
2
, return to JSP page after packaging
If you want to run your code after packaging, Java -jar ***.jar Use this method to /src/main/resources/ create the following directory in, and META-INF/resources/WEB-INF/jsp then configure the application.properties same as the first method. However mvn package , this will include the directory created above when you use packaging, as follows:
At this point, the java -jar SpringBootTemplate-0.0.1-SNAPSHOT.jar application can be deployed happily, by the way, in the JSP page you can also use the EL expression to get the data stored in the model.
"Web" supports JSP+MVC access