Spring Boot Integrated JSP

Source: Internet
Author: User

These days in the focus on the Spring Boot+shiro framework, because before the view layer with more JSP, so you want to configure the JSP in spring boot, but spring boot officially does not recommend the use of JSP, because the JSP compared to some template engine, performance is relatively low, The official recommendation is to use Thymeleaf, but the spring boot integration JSP process has been completed and recorded here.

This blog post is based on the LZ last article Spring Boot+mybatis integration, development tools are still intellij idea. The focus of this article is IntelliJ idea settings, otherwise can not jump to the JSP page, reported 404.

Pom.xml added tomcat support and JSTL tag Library

<!--Tomcat support--><dependency>    <groupId>org.apache.tomcat.embed</groupId>    < Artifactid>tomcat-embed-jasper</artifactid>    <!--<scope>provided</scope>--></ dependency><!--jstl Tag library--><dependency>    <groupId>javax.servlet</groupId>    < Artifactid>jstl</artifactid></dependency>

Second, configure the JSP path in APPLICATION.YML

Spring:  MVC:    View:      # page Default prefix directory       /web-inf/jsp/      # response page default suffix      suffix:. JSP

Third, create the webapp/web-inf/jsp directory below Src/main to store our JSP pages.

index.jsp

<%@ Page Language="Java"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML><Head><title>Spring Boot Sample</title></Head><Body>Time : ${time}<BR>Message: ${message}</Body></HTML>

page1.jsp

<%@ Page Language="Java"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML><Head><title>Spring Boot Sample</title></Head><Body>    <H1>${content}</H1></Body></HTML>

Iv. writing a Controller test

 PackageCom.test.controller;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;Importjava.util.Date;ImportJava.util.Map;/** * @authorXiaodongdong * @description: Test controller jumps to JSP page * @create 2017-11-13 11:36 **/@Controller Public classPagecontroller {//read configuration from application.yml, such as no default value of Hello Jsp@Value ("${application.hello:hello Jsp}")    PrivateString Hello = "Hello Jsp"; /*** Default Page <br/> * @RequestMapping ("/") and @RequestMapping are different * if you do not write the parameters, then the global default page, join the input 404 page, will also automatically access to this page.     * If the parameter "/" is added, it is considered to be the root page only. * This method can be accessed via localhost:8080 or Localhost:8080/index*/@RequestMapping (Value= {"/", "/index"})     PublicString index (map<string, object>model) {        //directly returns the string, the framework defaults to the Spring.view.prefix directory (index splicing spring.view.suffix) page//This example is/web-inf/jsp/index.jspModel.put ("Time",NewDate ()); Model.put ("Message", This. Hello); return"Index"; }    /*** Response to JSP page Page1*/@RequestMapping ("/page1")     PublicModelandview Page1 () {//Page Location/web-inf/jsp/page/page.jspModelandview Mav =NewModelandview ("Page/page1"); Mav.addobject ("Content", hello); returnMav; }    /*** Response to JSP page Page1 (can directly use the model package content, directly return the page string)*/@RequestMapping ("/page2")     PublicString Page2 (model model) {//Page Location/web-inf/jsp/page/page.jspModel.addattribute ("content", Hello + "(second type)"); return"Page/page1"; }}

If you Baidu, most of the tutorial is so tell you, nothing new, but LZ tried several times did not succeed, some data said jar package does not support jsp,jsp need to run in ServletContext, The war package needs to run on the server server such as Tomcat (where the jar and war refer to the settings of <packaging>jar</packaging> in Pom.xml), which is not very troublesome in the development environment, In fact, just set the IntelliJ idea on it.

Intellij Idea Toolbar File->project Structure, select the modules in the pop-up page, select the Web in the middle column (without the "+" sign new), then set the deployment descriptors and the Web Resource directories (this variable should be the default), where deployment descriptors points to the project name/src/main/webapp/web-inf/ Web. XML, which currently does not have XML, will be created automatically, Resource directories by default, without modification.

Then you can test the browser input http://localhost:8080 Http://localhost:8080/page1 http://localhost:8080/page2 see the effect.

Since spring boot does not recommend the use of JSP, it can only be replaced by thymeleaf, of course, other such as Freemarker can also be selected.

Spring Boot Integrated JSP

Related Article

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.