jsp不编译的原因

今天一个朋友一直为jsp不编译所困扰,后来远程登录到他的电脑忙碌一番 ,关于jsp不编译的解决办法google一下就有一大堆解决方案,这次的异常也着实让我头疼一番,但功夫还是不负有心人。

项目异常代码如下:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 23 in the generated java file
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

Stacktrace:
        org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
        org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
        org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
        org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
        org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
        org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
        org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
   
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 在Spring Boot中,使用JSP需要添加Tomcat Jasper依赖。并且需要在application.properties文件中添加以下配置: ``` spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp ``` 这样配置后,JSP文件需要手动编译才能生效,但是可以通过以下两种方式实现JSP自动编译: 1. 添加JSP自动编译插件 在pom.xml文件中添加以下插件: ```xml <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <id>tomcat-run</id> <goals> <goal>run</goal> </goals> <phase>pre-integration-test</phase> <configuration> <fork>true</fork> <port>8080</port> <path>/</path> <systemProperties> <spring.profiles.active>dev</spring.profiles.active> </systemProperties> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 执行以下命令即可启动Tomcat并自动编译JSP: ``` mvn tomcat7:run ``` 2. 手动编译JSP 在开发时,可以使用IDE的自动编译功能或者使用Maven的插件手动编译JSP。 在pom.xml文件中添加以下插件: ```xml <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> ``` 执行以下命令即可手动编译JSP: ``` mvn clean compile ``` ### 回答2: Spring Boot本身不支持JSP自动编译,因为Spring Boot默认使用的是嵌入式的Servlet容器,如Tomcat或Jetty等,而这些容器不直接支持JSP的自动编译。 不过,如果你有需要使用JSP的情况下,仍然可以在Spring Boot中使用JSP,但需要手动配置一些额外的设置。 首先,在你的Spring Boot项目中添加依赖项,如下所示: ```xml <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> ``` 接着,你需要在application.properties或application.yml中进行配置,指定JSP的位置和配置,如下所示: ```properties spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp ``` 然后,在你的Spring Boot项目中创建一个包路径为/WEB-INF/views/的文件夹,在该文件夹下添加你的JSP视图文件。 最后,你需要创建一个Controller类来处理对应的请求,并返回JSP视图,如下所示: ```java @Controller public class MyController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello World!"); return "hello"; } } ``` 在上述示例中,hello方法处理了对应路径为/hello的请求,并将一个名为message的数据添加到模型中,然后返回名为hello的JSP视图。 需要注意的是,为了使JSP生效,你需要将Spring Boot应用程序打包为WAR文件,而不是使用默认的JAR文件。这通常涉及到一些构建工具的配置,如Maven或Gradle,具体的操作方式请参考你所使用的构建工具的文档。 总之,尽管Spring Boot本身并不直接支持JSP的自动编译,但你仍然可以使用JSP来开发Spring Boot应用程序,只需要进行一些额外的配置和设置即可。 ### 回答3: Spring Boot其实并不支持JSP的自动编译。在Spring Boot中,默认情况下,它是不支持JSP的,而是推荐使用Thymeleaf或Freemarker等模板引擎来替代JSP。 然而,如果你非常需要使用JSP,你仍然可以在Spring Boot中使用JSP,只需要进行一些额外的配置。 首先,你需要在pom.xml文件中添加相关的依赖: ```xml <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> ``` 然后,在application.properties文件中添加以下配置来启用JSP支持: ```properties spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp spring.mvc.view.contentType=text/html;charset=UTF-8 ``` 接下来,你需要在项目的src/main/webapp目录下创建一个WEB-INF文件夹,并在其中创建一个名为"views"的文件夹,用于存放JSP文件。 最后,你需要在你的控制器类中编写相应的请求处理方法,并返回对应的JSP视图,例如: ```java @Controller public class MyController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello World"); return "hello"; } } ``` 这样,当你访问"/hello"路径时,它将自动查找并渲染名为"hello.jsp"的视图文件,并将"Hello World"传递给该视图。 需要注意的是,由于Spring Boot不支持JSP的自动编译,你需要重新启动应用程序才能看到JSP文件的变化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值