简体   繁体   English

Spring无法使用javax.servlet编译JSP

[英]Spring can't compile jsp with javax.servlet

I have a spring project and everything worked fine. 我有一个春季项目,一切正常。

My gradle dependencies compile has the following 我的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'

And my web.xml is 我的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>

And the beans are 豆子是

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

And everything work just wonderfully. 一切工作都很棒。

However I wanted to see if I can get ip address of visitors. 但是我想看看我能否获得访客的IP地址。

So I looked up and found out that I need to use HttpServletRequest and to use that I need javax.servlet so I modified gradle.build and added 'javax.servlet:servlet-api:2.5' . 因此,我抬起头来,发现需要使用HttpServletRequest并且需要使用javax.servlet因此我修改了gradle.build并添加了'javax.servlet:servlet-api:2.5' I have not modified a single line of code except build.gradle, and now I am getting HTTP Status 500 - Unable to compile class for JSP: org.apache.jasper.JasperException: Unable to compile class for JSP: 除build.gradle之外,我没有修改任何代码,现在我的HTTP Status 500 - Unable to compile class for JSP: org.apache.jasper.JasperException: Unable to compile class for JSP:

Can anyone explain why and how I got this and what cause it and how I can fix it? 谁能解释我为什么以及如何得到它,什么原因造成的,以及如何解决它? It is not making sense to me how adding a single library can cause everything to stop working. 对我来说,添加单个库会导致所有内容停止工作是没有意义的。

Thanks to anyone for their help, I am losing my mind without being able to find a solution. 感谢任何人的帮助,我迷失了方向,无法找到解决方案。

EDIT: 编辑:

This is the stack trace 这是堆栈跟踪

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)

Apparently it views the jsp as .java file. 显然,它将jsp视为.java文件。

EDIT2: EDIT2:

This is my jsp file 这是我的jsp文件

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

How to fix: revert your changes. 解决方法:还原您的更改。 You already have dependency on javax.servlet . 您已经依赖于javax.servlet

javax.servlet:javax.servlet-api:3.0.1 is a dependency of org.springframework:spring-web:4.1.2.RELEASE (look at section 'Depends On'), so it's available to you as a transitive dependency. javax.servlet:javax.servlet-api:3.0.1org.springframework:spring-web:4.1.2.RELEASE的依赖org.springframework:spring-web:4.1.2.RELEASE (请org.springframework:spring-web:4.1.2.RELEASE “ Depends On”部分),因此可以作为传递性依赖项使用。

Why you've got this: you have two conflicting dependencies of the same jar ( HttpServletRequest in Servlet API 3.0 does have getDispatcherType() method, while in API 2.5 it doesn't). 为什么要这样做:您在同一个jar中有两个相互冲突的依赖项(Servlet API 3.0中的HttpServletRequest确实具有getDispatcherType()方法,而在API 2.5中则没有)。 From Gradle User Guide : Gradle用户指南中

Conflicting versions of the same jar should be detected and either resolved or cause an exception. 应该检测到同一罐的版本冲突,或者解决该冲突或引起异常。 If you don't use transitive dependency management, version conflicts are undetected and the often accidental order of the classpath will determine what version of a dependency will win. 如果您不使用传递性依赖项管理,则不会检测到版本冲突,并且类路径的偶然顺序通常会决定哪种版本的依赖项将获胜。 On a large project with many developers changing dependencies, successful builds will be few and far between as the order of dependencies may directly affect whether a build succeeds or fails (or whether a bug appears or disappears in production). 在一个有许多开发人员更改依赖关系的大型项目中,成功的构建将很少而且相差甚远,因为依赖关系的顺序可能会直接影响构建是成功还是失败(或者错误是否在生产中出现或消失)。

"Apparently it views the jsp as .java file." “显然,它将jsp视为.java文件。” .

No. It translates the JSP into Java and then compiles that. 否。它将JSP转换为Java,然后进行编译。 If your JSP contains errors, then it is quite likely that those errors will result in Java code that has compilation errors in it. 如果您的JSP包含错误,那么这些错误很可能会导致Java代码中包含编译错误。

What you need to do is to look at the generated source code for the JSP, and figure out what the Java compilation errors are saying. 您需要做的是查看为JSP生成的源代码,并弄清楚Java编译错误在说什么。 Then figure out the corresponding error in your JSP, and fix the problem there. 然后找出您的JSP中的相应错误,并在那里解决问题。

In this case, it appears that your JSP is trying to call a non-existent method on the request object. 在这种情况下,您的JSP似乎试图在request对象上调用不存在的方法。 The compiler is correct. 编译器是正确的。 There is no method called getDispatcherType() in the HttpServletRequest API, so your JSP code that is attempting to call it is incorrect. HttpServletRequest API中没有名为getDispatcherType()方法,因此您试图调用它的JSP代码不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM