a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > JSP > 正文

詳解SpringBoot集成jsp(附源碼)+遇到的坑

2024-09-05 00:23:12
字體:
供稿:網(wǎng)友

本文介紹了SpringBoot集成jsp(附源碼)+遇到的坑 ,分享給大家

1、大體步驟

(1)創(chuàng)建Maven web project;

(2)在pom.xml文件添加依賴;

(3)配置application.properties支持jsp

(4)編寫測試Controller

(5)編寫JSP頁面

(6)編寫啟動類App.java

2、新建SpringInitialzr

SpringBoot,集成,jsp,源碼

SpringBoot,集成,jsp,源碼

3、pom文件

 <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文件

# 頁面默認前綴目錄spring.mvc.view.prefix=/WEB-INF/jsp/# 響應頁面默認后綴spring.mvc.view.suffix=.jsp# 自定義屬性,可以在Controller中讀取application.hello=Hello GOD

5、Controller文件

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 { // 從 application.properties 中讀取配置,如取不到默認值為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 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"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>GOD</title></head><body>helloJsp<hr>${hello}</body></html>

7、遇到的問題

1、Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

把pom中所有的<scope>provided</scope>注釋掉

2、報404

  <1>注意controller和restcontroller區(qū)別

  <2>檢查application.properties

  <3>檢查pom是否缺支持jsp包

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] ... 17 common frames omitted

 解決方案:添加以下依賴

  <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:152) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE] ... 21 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] ... 25 common frames omitted

解決方案:添加 

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

刪除其他依賴中的<scope>provided</scope>

8、源碼地址

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關教程知識閱讀請移步到JSP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产成人精品免费视频大全最热 | 91精品国产91久久久久久不卡 | 久久久久久国产精品免费免费 | 久久久久久亚洲 | 四虎成人在线视频 | 羞羞视频在线观看视频 | 九九亚洲 | 精品国产一区二区三区日日嗨 | 国产精品视频一区二区三区 | 国产精品日产欧美久久久久 | 午夜视频网站 | 亚洲无吗电影 | 天堂一区二区三区四区 | 91精品国产综合久久久蜜臀粉嫩 | 精品国产青草久久久久福利 | 国产区福利 | 日韩欧美在线观看视频 | 亚洲国产aⅴ成人精品无吗 亚洲h | 热久久这里只有精品 | 黄色大片免费网站 | 国产视频黄在线观看 | 在线播放黄色片网站 | 亚洲一级图片 | 国产一区二区精品在线 | 黄网站在线播放 | 成人精品一区二区三区 | 成人欧美一区二区三区黑人孕妇 | 五月av| 午夜精品亚洲日日做天天做 | 中文字幕电影在线 | 欧美亚洲免费 | 视频一区二区三区中文字幕 | 亚洲精品久久久日韩美女极品合集下载 | 人人精品 | 欧美一区2区三区4区公司二百 | 国产成人8x视频一区二区 | 久久久日韩精品一区二区三区 | 精品中文字幕在线观看 | 成人免费一区二区三区视频网站 | 久久成人国产精品 | 国产精品视频十区 |