Only a type can be imported. l1.l2.MyClass resolves to a package

==========这里是解决方案===============

把生成的MyClass.class复制到网站根目录下的WEB-INF下的classes目录下,如果没有classes,就建一个,然后在classes目录下,按照包的层级逐层建立目录,例如l1.l2.MyClass,就建一个l1目录,在l1目录下建l2目录,把MyClass.class复制到l2目录下,即可。

==========以下是过程===============

昨天和今天,一直在尝试在JSP中调用JavaBean,看似很简单的事情,却遇到这个问题,遍搜全网,包括英文,下面这句给了我一些提示:

If you spell the class name wrong or the class isn't on the classpath, the JSP processor will say it "resolves to a package" rather than that it doesn't exist. This was driving me crazy today as I kept not seeing a typo I'd made.

就是说,你写错类名了,它就找不到,也就是说,随便写个类名,java找不到,就会出这个提示,说你写的这个类被解析为包,它不说找不到这个类。

整个问题过程如下:

1.用Eclipse Java Neon在项目中建立jsp页面a.jsp,位于D:\eclipseWorkspace\MentalArithmetic\WebContent,并在tomcat的conf目录下的server.xml中配置host节,设置为

<Context path="" docBase="D:\eclipseWorkspace\MentalArithmetic\WebContent" debug="0" privileged="true" reloadable="true">
</Context>

2.项目中右键new一个java的class,类名叫MyClass,包名叫l1.l2,它会在java Resources节点下的src目录下创建java文件,对应于MentalArithmetic目录下的src目录,类似这样D:\eclipseWorkspace\MentalArithmetic\src\l1\l2,l1和l2是包的名字,layer1和layer2的意思。

package l1.l2;

public class MyClass {

    public MyClass() {

    }
public String address; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}

3.项目中右键new一个jsp文件,我叫它a.jsp,在MentalArithmetic\WebContent,也就是网站根目录下。并在此文件中引入类,代码如下:

<%@ page language="java" import="l1.l2.MyClass"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form> <%
MyClass b = new MyClass();
b.setAddress("kkkkkkkkkkkkkkkkk"); out.print(b.getAddress());
%>
</form>
</body>
</html>

理论上,如果这是一个在eclipse中关联紧密的项目,像visual studio一样,这个时候应该是可以运行了。背后的很多工作,应当由IDE来完成。

然而,问题来了。无论我怎样运行,都会提示Only a type can be imported. l1.l2.MyClass resolves to a package,做了很多尝试,确认我没有写错。把eclipse在build中生成的MyClass.class到处粘贴,每一个可能的地方,均无效。网上说tomcat会在网站根目录下的WEB-INF下的classes中找类的信息,这个其实是对的,但是在不清楚全过程的情况下,这么一句是无法解决问题的,我把MyClass.class复制到了classes目录下,然并卵,仍然无效。不细说了。

找了很多网站,找了很多书,依然然并卵。

最后,绝望极了。

然后就思考一个问题,如果JSP页面中,随便写个导入类的语句,如<%@ page import="a.b.c"%>,就会报下面这个错

Only a type can be imported. a.b.c resolves to a package

无论a.b.c是否真的存在,这说明系统没有找到a.b.c这个类,那么问题就变成了“如何让系统找到自定义的类?”

如果tomcat会在网站根目录下寻找类的字节码,那么它是不是像eclipse中定义的一样,一层一层地去寻找呢?

打开网站根目录下的WEB-INF,这个目录是eclipse生成的,里面毛也没有。

新建了一个classes目录,在其下又新建了一个l1子目录,在l1下又新建了一个l2子目录,把MyClass.class复制过来,再运行http://localhost/a.jsp,成功。

我一点都不兴奋。

这是不是意味着,每次写完javaBean,都要手动把class复制到WEB-INFO目录去?可能有更好的方法,目前不知道。

作为eclipse来讲,如此基础的工作都没有完成,这个IDE真的好用么?

是不是开源的工具都是这样,易用性较差,学习曲线陡峭,如果你会用,那它还挺好使的,如果你是不会用的初学者,要花很多的时间和精力去学习。

从这个角度来看,微软真的挺伟大的,致力于让开发者把精力集中在业务上,开发工具简单得一塌糊涂,包括java的开发者在内,都认为VS太好用了,节省大量时间。但是它的正版工具价格与破解成本比起来,实在让人无法产生购买欲。如果VS和.net能继续开源,继续发展,超过Java也并非不可能。

Only a type can be imported. classname resolves to a package的解决的更多相关文章

  1. 解决Only a type can be imported. com.mysql.jdbc.Connection resolves to a package的报错问题

    写jsp加载数据驱动以后老是提示Only a type can be imported. com.mysql.jdbc.Connection resolves to a package的错误,然而改成 ...

  2. 解决Only a type can be imported. * resolves to a package

    HTTP Status 500 - Unable to compile class for JSP: type Exception report message Unable to compile c ...

  3. JSP error: Only a type can be imported

    错误: [14] in the generated java file: [E:\apache-tomcat-7.0.63-windows-x64\apache-tomcat-7.0.63\work\ ...

  4. PWC6199:Generated servlet error:Only a type can be imported. org.apache.jasper.tagplugins.jstl.core.ForEach resolves to a package

    <%@ import="org.apache.jasper.tagplugins.jstl.core.ForEach"%> 去掉这条语句,就不报错了.所以问题就出在这里 ...

  5. Type.GetType()在跨程序集反射时返回null的解决方法

    在开发中,经常会遇到这种情况,在程序集A.dll中需要反射程序集B.dll中的类型.如果使用稍有不慎,就会产生运行时错误.例如使用Type.GetType("BNameSpace.Class ...

  6. Dynamics CRM2015 The plug-in type does not exist in the specified assembly问题的解决方法

    在用插件工具PluginProfiler调试时,报"The plug-in type  xxxx  does not exist in the specified assembly" ...

  7. Eclipse之NDK编译-- Type 'jint' could not be resolved, and JNIEnv, jclass错误解决办法

    最近在研究面部识别美白相关的功能.使用的是opencv,就去研究了.今天正好有空就把安装了ndk,安装完成之后就试图去编译demo程序,hellow-jni c代码,一开始编辑就报错了3个错误信息: ...

  8. The type org.springframework.jms.JmsException cannot be resolved报错解决

    在调用JmsTemplate的send方法时,一直报编译时异常.如下: 异常提示是无法解析org.SpringFrawork.jms.JmsException类型.如下: The type org.s ...

  9. 在 UIViewController 中手动增加 TableView 出现 Type 'SomeViewController' does not confirm to protocol 'UITableViewDataSource' 问题的解决办法

    许多时候我们都有在普通的继承自 UIViewController 的控制器中使用 TableView 的需求,这时候就需要当前控制器类继承 UITableViewDelegate 和 UITableV ...

随机推荐

  1. e674. 创建并绘制加速图像

    Images in accelerated memory are much faster to draw on the screen. This example demonstrates how to ...

  2. ScreenPointToRay - 近视口到屏幕的射线

    正如题目所说,ScreenPointToRay可以计算从Camera的近视口nearClip向前发射一条射线到屏幕上的点的坐标. 函数原型为: public Ray ScreenPointToRay( ...

  3. CPU性能判断指标---上下文切换,运行队列和使用率

    http://blog.chinaunix.net/uid-15007890-id-3064254.html uptime11:35:08 up 21:57,  6 users,  load aver ...

  4. JS的事件冒泡和事件捕获

    先上结论:他们是描述事件触发时序问题的术语.事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件冒泡是自下而上的去触发事件.绑定事件方法的第三个参数,就是控制事件 ...

  5. js中数组作为参数传递的定义

    下面的函数实现了一个我们想要的最基本的图片预加载效果 function preloadimages(arr){    var newimages=[]    var arr=(typeof arr!= ...

  6. MathType如何编辑手写体l

    MathType在编辑公式不仅方便而且规范,并且能够根据自己的需要选择不同的字体进行使用,可以是正体也可以是斜体,可以是新罗马体,也可以是花体,这些用word公式编辑器MathType都是可以的.还有 ...

  7. cesium图形上加载图片

    <!DOCTYPE html> <html> <head> <!-- Use correct character set. --> <meta c ...

  8. 超全面的JavaWeb笔记day10<Response&Request&路径&编码>

    1.Response 2.Request 3.路径 4.编码 请求响应流程图 response 1.response概述 response是Servlet.service方法的一个参数,类型为java ...

  9. SQL集合运算:差集、交集、并集

    1.差集( except ) select a from t_a except select a from t_b -- 也可写作: select a from t_a where a not in ...

  10. python2.0_s12_day19_前端结合后端展示客户咨询纪录

    接下来就是将后台视图与前端页面结合起来了完成后台系统了.实现前端展示用户列表1.先在base.html代码中把模版中Dashboard下面的内容清空,如下: 具体删除哪些html代码,自己找吧.2.我 ...