"Web" supports JSP+MVC access

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.