Java 你如何在JSP中导入类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/239147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:53:10  来源:igfitidea点击:

How do you import classes in JSP?

javajsp

提问by jjnguy

I am a complete JSP beginner. I am trying to use a java.util.Listin a JSP page. What do I need to do to use classes other than ones in java.lang?

我是一个完整的 JSP 初学者。我正在尝试java.util.List在 JSP 页面中使用 a 。我需要做什么才能使用 中的类以外的类java.lang

采纳答案by Sandman

Use the following import statement to import java.util.List:

使用以下导入语句导入java.util.List

<%@ page import="java.util.List" %>

BTW, to import more than one class, use the following format:

顺便说一句,要导入多个类,请使用以下格式:

<%@ page import="package1.myClass1,package2.myClass2,....,packageN.myClassN" %>

回答by Axeman

In the page tag:

在页面标签中:

<%@ page import="java.util.List" %>

回答by Kevin Day

FYI - if you are importing a List into a JSP, chances are pretty good that you are violating MVC principles. Take a few hours nowto read up on the MVC approachto web app development (including use of taglibs) - do some more googling on the subject, it's fascinating and will definitely help you write better apps.

仅供参考 - 如果您将 List 导入 JSP,则很有可能您违反了 MVC 原则。现在花几个小时阅读Web 应用程序开发的MVC 方法(包括使用 taglibs) - 在这个主题上做更多的谷歌搜索,它很有趣,肯定会帮助你编写更好的应用程序。

If you are doing anything more complicated than a single JSP displaying some database results, please consider using a framework like Spring, Grails, etc... It will absolutely take you a bit more effort to get going, but it will save you so much time and effort down the road that I really recommend it. Besides, it's cool stuff :-)

如果您正在做比显示一些数据库结果的单个 JSP 更复杂的事情,请考虑使用SpringGrails等框架......它绝对会花费您更多的精力来开始,但它会为您节省很多我真正推荐它的时间和精力。此外,这是很酷的东西:-)

回答by Birhan Nega

This is the syntax to import class

这是导入类的语法

  <%@ page import="package.class" %>

回答by Gaurav Varshney

Use Page Directive to import a Class in JSP page. Page Directive Uses 11 Different types of Attributes , One of them is "import". Page Directive with import Attribute Allows you to Mention more than one package at the same place separated by Commas(,). Alternatively you can have multiple instances of page element each one with Different package .

使用页面指令在 JSP 页面中导入类。页面指​​令使用 11 种不同类型的属性,其中之一是“导入”。带有导入属性的页面指令允许您在以逗号(,)分隔的同一位置提及多个包。或者,您可以拥有多个页面元素实例,每个实例都带有不同的包。

For Example:

例如:

 <%@ page import = "java.io.*" %>
 <%@ page import = "java.io.*", "java.util.*"%>

Note : the import attribute should be placed before the element that calls the importd class .

注意:import 属性应该放在调用导入类的元素之前。

回答by George Siggouroglou

In case you use JSTL and you wish to import a class in a tag page instead of a jsp page, the syntax is a little bit different. Replace the word 'page' with the word 'tag'.

如果您使用 JSTL 并且希望在标记页面而不是 jsp 页面中导入类,则语法会有所不同。将“页面”一词替换为“标签”。

Instead of Sandman's correct answer

而不是桑德曼的正确答案

<%@page import="path.to.your.class"%>

use

<%@tag import="path.to.your.class"%>