在JSP文件中导入类
我写了一些代码作为Java Servlet,现在我试图将它转换为JSP。我在一个单独的文件中写了一个类,我正在使用,我不知道如何获得JSP文件来识别类。我想它与进口有关。我给这个类一个包( package mypackagename;
)名称,我尝试使用<%@ page import =mypackagename%& code>但我得到一个错误:
I wrote some code as a Java Servlet and now I am trying to convert it to a JSP. I wrote a class in a separate file which I was using, and I can't figure out how to get the JSP file to recognize the class. I guess it has something to do with importing. I gave the class a package (package mypackagename;
) name and I tried using <%@ page import="mypackagename"%>
but I get an error:
导入mypackagename无法解决
The import "mypackagename" cannot be resolved
只需以与在真正的Java类中一样的方式导入它。也就是说 import mypackagename.MyClassName
或 import mypackagename。
,因此不
Just import it the same way as you do in a real Java class. I.e. import mypackagename.MyClassName
or import mypackagename.*
and thus not import mypackagename
with only the package name.
<%@ page import="mypackagename.MyClassName" %>
也就是说,在JSP文件中。
That said, you should not write raw Java code in a JSP file. Scriptlets are considered poor practice. That code belongs in a real Java class. It was located perfectly fine in the Servlet class. What is it, the problem for which you think that it is the "right" solution to move it all into the view side and clutter the template text with raw Java code? Elaborate about it in a new question, then we may be able to suggest the right solutions. Maybe you weren't aware of existence and powers of taglibs like JSTL?