[Spring boot] web App returns JSP page

Source: Internet
Author: User

A colleague created a spring boot project and uploaded it to SVN. I need to write a page. After downloading, it is always impossible to configure the direct return JSP page in the controller method.

Depressed for an afternoon, finally fix the problem. Record it here.

Target: Configure the direct return JSP page in the Controller method

Add the Src/main/webapp folder to the project, nothing to say.

The different implementations of @controller annotations and @restcontroller annotations are described in detail below.

@Controller annotations

1. Configuration in the Application.properties file

# Configure the location of the JSP file, the default location is: Src/main/webapp
spring.view.prefix:/pages/ #指向jsp文件位置: src/main/webapp/pages

# Configure the suffix of the JSP file
spring.view.suffix:.jsp

2. Configuration in controller files

 import   Org.springframework.stereotype.Controller;  import   org.springframework.web.bind.annotation.RequestMapping;  import   Org.springframework.web.bind.annotation.RequestMethod; @Controller  public  class   Examplecontroller {@RequestMapping (value  = "/initpage", Method = public   String Doview () { return  "index"; //  accessible to: src/main/webapp/pages/index.jsp  }}  

3. Start with the application [main] method

4. Access the URL, access to the JSP page

http://localhost:8080/initpage

@RestController annotations

1. Configuration in the Application.properties file

# Configure the location of the JSP file, the default location is: src/main/webappspring.mvc.view.prefix:/pages/  #指向jsp文件位置: src/main/webapp/  Pages    NOTE!!!!! The property name is not the same as the @controller configuration, more "MVC" # Configure the suffix of the JSP file spring.mvc.view.suffix:.jsp # There's    just as much "mvc" Yo

2. controller file configuration, also accessible to: src/main/webapp/pages/index.jsp

Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RestController;ImportOrg.springframework.web.servlet.ModelAndView; @RestController Public classOvalalarmcontroller {@RequestMapping (value= "/initpage", method =requestmethod.get) PublicModelandview Doview () {Modelandview mv=NewModelandview ("Index"); returnMV; }}

3. Start with the application [main] method

4. Access the URL, access to the JSP page

Http://localhost:8080/initpage

Also encountered a problem:

Spring boot returns the JSP file download directly.

Reason:

The JSP file is not parsed, just add the following configuration in the Pom.xml file. OK, problem solved.

    <Dependency>        <groupId>Org.apache.tomcat.embed</groupId>        <Artifactid>Tomcat-embed-jasper</Artifactid>        <version>7.0.59</version>    </Dependency>

[Spring boot] web App returns JSP page

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.