一尘不染

Spring无法使用javax.servlet编译JSP

jsp

我有一个spring项目,一切正常。

我的gradle依赖项编译具有以下内容

compile 'org.springframework:spring-core:4.1.2.RELEASE',
        'org.springframework:spring-web:4.1.2.RELEASE',
        'org.springframework:spring-webmvc:4.1.2.RELEASE',
        'org.springframework:spring-orm:4.1.2.RELEASE',
        'org.springframework:spring-context:4.1.2.RELEASE',
        'org.springframework:spring-tx:4.1.2.RELEASE',
        'commons-dbcp:commons-dbcp:1.4',
        'mysql:mysql-connector-java:5.1.6',
        'org.hibernate:hibernate-core:4.3.7.Final'

我的web.xml是

<web-app
    id="WebApp_ID"
    version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Test Restful Application</display-name>

    <servlet>
        <servlet-name>test-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>test-servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/test-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>/WEB-INF/view/default.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/view/default.jsp</location>
    </error-page>

</web-app>

豆子是

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.example" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>

    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        id="sessionFactory">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

    <tx:annotation-driven />
    <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        id="transactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean class="com.example.dao.TestDAO" id="testDAO">
        <constructor-arg ref="sessionFactory" />
    </bean>

</beans>

一切工作都很棒。

但是我想看看我能否获得访客的IP地址。

因此,我抬起头来,发现需要使用HttpServletRequest和使用需要的东西,javax.servlet因此我进行了修改gradle.build和添加
'javax.servlet:servlet-api:2.5'。除了build.gradle外,我没有修改任何一行代码,现在我得到了HTTP Status 500 - Unable to compile class for JSP:
org.apache.jasper.JasperException: Unable to compile class for JSP:

谁能解释我为什么以及如何得到它,什么原因造成的,以及如何解决它?对我来说,添加单个库会导致所有内容停止工作是没有意义的。

感谢任何人的帮助,我迷失了方向,无法找到解决方案。

编辑:

这是堆栈跟踪

Stacktrace:] with root cause
 org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: [42] in the generated java file: [system\tomcat\Tomcat_(1)_web\work\Catalina\localhost\ROOT\org\apache\jsp\WEB_002dINF\view\default_jsp.java]
The method getDispatcherType() is undefined for the type HttpServletRequest

Stacktrace:
    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

显然,它将jsp视为.java文件。

编辑2:

这是我的jsp文件

<html>
<head>
    <title>Error</title>
</head>
<body>
<h3>Wrong page</h3>
</body>
</html>

阅读 253

收藏
2020-06-10

共1个答案

一尘不染

解决方法:还原您的更改。您已经依赖javax.servlet

javax.servlet:javax.servlet-api:3.0.1是的依赖项org.springframework:spring- web:4.1.2.RELEASE(请参见“依赖于”部分),因此可以作为传递性依赖项使用。

为什么要这样做:同一jar有两个相互冲突的依赖关系(HttpServletRequest在Servlet API
3.0中有getDispatcherType()方法,而在API
2.5中没有方法)。从Gradle用户指南中

应该检测到同一罐的版本冲突,或者解决该冲突或引起异常。如果您不使用传递性依赖项管理,则不会检测到版本冲突,并且类路径的偶然顺序通常会决定哪种版本的依赖项将获胜。在一个有许多开发人员更改依赖关系的大型项目中,成功的构建将很少而且相差甚远,因为依赖关系的顺序可能会直接影响构建是成功还是失败(或者错误在生产中出现还是消失)。

2020-06-10