Struts2 项目出现错误 - 包 javax.servlet.jsp 不存在
我一直在 Netbeans7 中设置一个 Struts2 项目,并且实际上使用 Maven 很好地设置了该项目。
当项目正在运行时,这不会造成太大的问题,但我只是在我的 jsp 文件中收到此错误,并显示为红色下划线。
package javax.servlet.jsp does not exist
我用谷歌搜索了一下,最终得到了一个解决方案,让我将 jsp-api
添加到我的依赖项中。一旦我在 netbeans 中添加了这个,就不再显示错误,所以我构建并再次运行它,但现在我收到服务器错误。
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:95)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
在这里环顾四周后,我遇到了这个问题。我在这里经历了三个解决方案:
- 我正在使用 Tomcat 7
- 我的 web.xml servlet 版本是 2.5(我认为)
- 我不太明白这一点,但认为可能是这样,因为我确实放置了
jsp-api。 jar
在我的依赖项中,servlet-api.jar
也在那里。
因此,我尝试从依赖项中删除 jsp-api.jar
和 servlet-api.jar
,看看这是否可以解决问题。这让我回到了原来的问题。
这并不是什么大问题,因为项目运行良好并且 struts 正在工作。但这很烦人,我想摆脱它,因为我发现它可能会在以后引起问题。因此,如果有人能想到我没有尝试过的东西,那就太棒了!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>New tradesman experience</display-name>
<description>New tradesman experience</description>
<!-- listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<!-- context params -->
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles-tradesman.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-tradesman.xml</param-value>
</context-param>
<!-- filters -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- filter mappings -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
I've been setting up a Struts2 Project in Netbeans7 and actually got the project set up fine using Maven.
This isn't causing much of an issue as the project is working, but Im just getting this error in my jsp file as a red underline.
package javax.servlet.jsp does not exist
I googled a bit and ended up with a solution that had me adding in jsp-api
into my dependencies. Once I added this in netbeans stopped showing me the error so i built and ran it again but now I get a server error.
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:95)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
After looking around here for a bit I came accross this question. I went through the three solutions here:
- I am using Tomcat 7
- My web.xml servlet version is 2.5 (i think)
- I don't really understand this but thought it might be the case as I did put
jsp-api.jar
in my dependencies andservlet-api.jar
is also there.
So I tried removing both jsp-api.jar
and servlet-api.jar
from my dependencies to see if this would solve the problem. This just took me back to my original problem.
It's not a showstopper as the project is running fine and struts is working. But it is annoying and I would like to get rid of it as I can see it potentially causing problems later on. So if someone could think of something I haven't tried then that would be awesome!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>New tradesman experience</display-name>
<description>New tradesman experience</description>
<!-- listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<!-- context params -->
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles-tradesman.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-tradesman.xml</param-value>
</context-param>
<!-- filters -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- filter mappings -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更改maven中jsp-api和servlet-api的范围,将其设置为provided。
try to change the scope of jsp-api and servlet-api in maven, set it provided.