欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解SpringBoot集成jsp(附源码)+遇到的坑

程序员文章站 2023-11-17 21:06:40
本文介绍了springboot集成jsp(附源码)+遇到的坑 ,分享给大家 1、大体步骤 (1)创建maven web project; (2)在pom.xml文件添...

本文介绍了springboot集成jsp(附源码)+遇到的坑 ,分享给大家

1、大体步骤

(1)创建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.
 */
@controller

public 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区别

  <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 omitted
caused 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。