简体   繁体   English

无法解析为在Tomcat中手动部署的JSP编译中的类型

[英]can not resolve to a type in JSP compilation manually deployment in tomcat

I am new to Web Development in Java. 我是Java Web开发的新手。 I am trying to deploy web application in Apache Tomcat (9.0.17). 我正在尝试在Apache Tomcat(9.0.17)中部署Web应用程序。 I have installed Linux OS in which Java OpenJDK is pre-installed. 我已经安装了预装有Java OpenJDK的Linux OS。 I am not able to compile JSP. 我无法编译JSP。

Output of "java -version" in terminal: 在终端中输出“ java -version”:

openjdk version "1.8.0_202"
OpenJDK Runtime Environment (build 1.8.0_202-b26)
OpenJDK 64-Bit Server VM (build 25.202-b26, mixed mode)

I have put my java package inside TOMCAT_INSTALLATION_DIR/webapps/app/WEB_INF/classes/ .here TOMCAT_INSTALLATION_DIR is a directory where i extracted apache-tomcat-9.0.17.tar.gz file. 我已将Java包放入TOMCAT_INSTALLATION_DIR / webapps / app / WEB_INF / classes /中。这里TOMCAT_INSTALLATION_DIR是我提取apache-tomcat-9.0.17.tar.gz文件的目录。 Inside classes directory, i have java package named abc. 在类目录中,我有一个名为abc的java包。 Inside that, i have put Test.java 在里面,我放了Test.java

Here is the code in Test.java 这是Test.java中的代码

package abc;
public class Test{
    public  String f(){
            return ("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
    }
    public Test(){
        System.out.println("I am created");
    }
}

I successfully compiled it and put in same directory where my Test.java file resided(inside abc directory). 我成功地编译了它,并将其放在我的Test.java文件所在的目录中(在abc目录中)。

I have created web.xml inside WEB-INF folder. 我已经在WEB-INF文件夹中创建了web.xml。 content inside web.xml : web.xml中的内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>app</display-name>
  <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

I have index.jsp file inside app/ directory. 我在app /目录中有index.jsp文件。 Content of JSP : JSP的内容:

<%@ page  import="abc.*"%>
<!DOCTYPE html>
<html>
<head>

<title>Insert title here</title>
</head>
<body>
    <%
    Test cc=new Test();
    cc.f();
    %>
</body>
</html>

I ran apache by ./cataline.sh run command inside bin directory that is inside installation directory. 我通过./cataline.sh运行命令在安装目录中的bin目录中运行了apache。 Then i open localhost:8080/app URI. 然后我打开localhost:8080 / app URI。 I got an error 我有一个错误

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

An error occurred at line: [10] in the jsp file: [/index.jsp]
Test cannot be resolved to a type
7: </head>
8: <body>
9:  <%
10:     Test cc=new Test();
11:     cc.f();
12:     %>
13: </body>


An error occurred at line: [10] in the jsp file: [/index.jsp]
Test cannot be resolved to a type
7: </head>
8: <body>
9:  <%
10:     Test cc=new Test();
11:     cc.f();
12:     %>
13: </body>


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)

Thanks for detailed instructions in the question. 感谢您对问题的详细说明。 I setup the web app per those details and found two issues: 我根据这些详细信息设置了Web应用程序,发现了两个问题:

  1. It's not WEB_INF but WEB-INF . not WEB_INF but WEB-INF Please check if the folder name is indeed WEB-INF. 请检查文件夹名称是否确实为WEB-INF。
  2. Here is the updated index.jsp so that returned value is displayed on the web page. 这是updated index.jsp以便将返回的值显示在网页上。 The code in the JSP file gets executed in tomcat server and any System.out.println line would get logged in server logs (ie TOMCAT_INSTALLATION_DIR/logs/catalina.out ). JSP文件中的代码在tomcat服务器中执行,并且任何System.out.println行都将记录在服务器日志中(即TOMCAT_INSTALLATION_DIR / logs / catalina.out)。 Similarly, the value returned from function call is not going to get displayed unless you use <%= variable %> . 同样,除非使用<%= variable %>否则不会显示从函数调用返回的值。
<%@ page  import="abc.*"%>
<!DOCTYPE html>
<html>
<head>

<title>Insert title here</title>
</head>
<body>
    <%
    Test cc=new Test();%>
    <%= cc.f() %>
</body>
</html>

In case these changes don't work, could you please paste the screenshot of the whole folder structure of all files under the Tomcat installation directory ? 如果这些更改不起作用,可以将所有文件的整个文件夹结构的屏幕截图粘贴到Tomcat安装目录下吗? I am asking this as everything else is correct and it worked for me. 我问这是因为其他所有内容都正确,并且对我有用。

My Local Setup Folder Structure My Local WebApp Index Page 我的本地设置文件夹结构 我的本地WebApp索引页

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

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