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