Springboot integrated JSP (with source code) + encountered pits

Source: Internet
Author: User

1, the overall step

(1) Create MAVEN Web project;

(2) Add dependency in Pom.xml file;

(3) Configuring Application.properties support JSP

(4) Writing test controller

(5) Writing JSP pages

(6) Writing the startup class App.java

2. New SPRINGINITIALZR

3. Pom file
    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactid>spring-boot-starter-tomcat </artifactId>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>org.apache.tomcat.embed</groupId>            <artifactid>tomcat-embed-jasper</ artifactid>        </dependency>        <dependency>            <groupid>org.springframework.boot </groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test< /scope>        </dependency>    </dependencies>

4. application.properties file
# page Default prefix directory spring.mvc.view.prefix=/web-inf/jsp/# response page default suffix spring.mvc.view.suffix=.jsp# custom properties, Can read Application.hello=hello GOD in the controller
5. controller file
Package Com.example;import Org.springframework.beans.factory.annotation.value;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import java.util.map;/** * Created by gensis on 2016/9/9. */@Controllerpublic class Hellocontroller {    //Read the configuration from application.properties, such as the default value of Hello    @Value ("${ Application.hello:Hello} ")    private String hello;    @RequestMapping ("/hellojsp") public    String hellojsp (map<string, object> Map) {        System.out.println (" Hellocontroller.hellojsp (). hello= "+ Hello";        Map.put ("Hello", hello);        return "hellojsp";    }}
6. JSP page
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
7. Problems encountered 1, Spring boot:unable to start embeddedwebapplicationcontext due to missing embeddedservletcontainerfactory bean

Comment out all the <scope>provided</scope> in POM

2, reported 404

<1> Note Controller and Restcontroller differences

<2> Check Application.properties

<3> Check if POM is missing support JSP package

3, Failed to Introspect annotated methods on class Org.springframework.boot.web.support.SpringBootServletInitializer
Org.springframework.beans.factory.BeanDefinitionStoreException:Failed to parse Configuration class [ Com.example.SampleWebJspApplication]; Nested exception is java.lang.IllegalStateException:Failed to Introspect annotated methods on class Org.springframework. Boot.web.support.SpringBootServletInitializer at Org.springframework.context.annotation.ConfigurationClassParser.parse (configurationclassparser.java:187) ~[ Spring-context-4.3.2.release.jar:4.3.2.release] At Org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions ( configurationclasspostprocessor.java:321) ~[spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry ( configurationclasspostprocessor.java:243) ~[spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors (Postprocessorregistrationdelegate.java:273) ~[spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors ( postprocessorregistrationdelegate.java:98) ~[spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors ( abstractapplicationcontext.java:681) ~[spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.context.support.AbstractApplicationContext.refresh (abstractapplicationcontext.java:523) ~[ Spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.boot.SpringApplication.refresh ( springapplication.java:759) [Spring-boot-1.4.0.release.jar:1.4.0.release] at Org.springframework.boot.SpringApplication.refreshContext (springapplication.java:369) [ Spring-boot-1.4.0.release.jar:1.4.0.release] at Org.springframework.boot.SpringApplication.run ( springapplication.java:313) [spring-boot-1.4.0.release.jar:1.4.0.release] at Org.springframework.boot.SpringApplication.run (springapplication.java:1185) [ Spring-boot-1.4.0.release.jar:1.4.0.release] at Org.springframework.boot.SpringApplication.run ( springapplication.java:1174) [Spring-boot-1.4.0.release.jar:1.4.0.release] at Com.example.SampleWebJspApplication.main (SAMPLEWEBJSPAPPLICATION.JAVA:20) [Classes/:na] at SUN.REFLECT.NATIVEMETHODACCESSORIMPL.INVOKE0 (Native Method) ~[na:1.8.0_77] at Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62) ~[na:1.8.0_77] at Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43) ~[na:1.8.0_77] at Java.lang.reflect.Method.invoke (method.java:498) ~[na:1.8.0_77] at Com.intellij.rt.execution.application.AppMain.main (appmain.java:147) [idea_rt.jar:na]caused by: Java.lang.IllegalStateException:Failed to Introspect annotated methods on class Org.springframework.boot.web.support.SpringBootServletInitializer at Org.spriNgframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods (standardannotationmetadata.java:163) ~[ Spring-core-4.3.2.release.jar:4.3.2.release] At Org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass ( configurationclassparser.java:301) ~[spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass ( configurationclassparser.java:237) ~[spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.context.annotation.ConfigurationClassParser.parse (configurationclassparser.java:204) ~[ Spring-context-4.3.2.release.jar:4.3.2.release] At Org.springframework.context.annotation.ConfigurationClassParser.parse (configurationclassparser.java:173) ~[ Spring-context-4.3.2.release.jar:4.3.2.release] ... Common frames omitted

Solution: Add the following dependencies

        <dependency>            <groupId>org.apache.tomcat.embed</groupId>            <artifactId> Tomcat-embed-jasper</artifactid>        </dependency>
4, Java.lang.noclassdeffounderror:javax/servlet/servletcontext
caused by:java.lang.noclassdeffounderror:javax/servlet/servletcontext at    Java.lang.Class.getDeclaredMethods0 (Native Method) ~[na:1.8.0_77] at    java.lang.Class.privateGetDeclaredMethods (class.java:2701) ~[na:1.8.0_77] At    java.lang.Class.getDeclaredMethods (class.java:1975) ~[na:1.8.0_77]    at Org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods (Standardannotationmetadata.java : ~[spring-core-4.3.2.release.jar:4.3.2.release]    ... Common frames omittedcaused By:java.lang.ClassNotFoundException:javax.servlet.ServletContext at    Java.net.URLClassLoader.findClass (urlclassloader.java:381) ~[na:1.8.0_77] at    java.lang.ClassLoader.loadClass (classloader.java:424) ~[na:1.8.0_77] at    sun.misc.launcher$appclassloader.loadclass (Launcher.java:331) ~[na : 1.8.0_77] at    java.lang.ClassLoader.loadClass (classloader.java:357) ~[na:1.8.0_77]    ... Common frames omitted

Solution: Add

        <dependency>            <groupId>javax.servlet</groupId>            <artifactid>javax.servlet-api </artifactId>        </dependency>        <dependency>            <groupid>javax.servlet</ groupid>            <artifactId>jstl</artifactId>        </dependency>

Remove the <scope>provided</scope> from other dependencies

8. Source Address

https://github.com/Genesisxu/SpringSeries/tree/master/SpringBoot-jsp

Springboot integrated JSP (with source code) + encountered pits

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.