eclipse 跨多个包含块的 Java 变量 - 无法解析变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16593952/
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
Java variable across multiple include blocks - variable cannot be resolved
提问by bobetko
I have jsp page that looks something like this:
我有一个看起来像这样的jsp页面:
MyPage.jsp
我的页面.jsp
<%@ include file = "/Title.jsp" %>
<%@ include file = "/Header.jsp" %>
<html>...</html>
In Title.jsp there is a instance of "user", let's say:
在 Title.jsp 中有一个“user”的实例,让我们说:
User user = new User();
in Header.jsp user is referenced as
在 Header.jsp 用户被引用为
user.setName("John");
Eclipse is showing error that "user cannot be resolved to variable" in header.jsp file. I perfectly understand why. Is there a way to tell Eclipse to ignore this particular error for this particular variable?Is there anything that can be done here?.
Eclipse 在 header.jsp 文件中显示“用户无法解析为变量”的错误。我完全明白为什么。有没有办法告诉 Eclipse 忽略这个特定变量的这个特定错误?这里有什么可以做的吗?
At work, I am supporting some old (and big and ugly) Java application that is actually a web application. I am trying to convert this Java Project to Java Dynamic Web Project. Before I converted it, somehow Eclipse didn't care about these hanging variables that are all over the place. Now, in Dynamic Web Project, Eclipse is complaining.
在工作中,我支持一些旧的(又大又丑)的 Java 应用程序,它实际上是一个 Web 应用程序。我正在尝试将此 Java 项目转换为 Java 动态 Web 项目。在我转换它之前,不知何故 Eclipse 并不关心这些无处不在的悬挂变量。现在,在动态 Web 项目中,Eclipse 正在抱怨。
Rewriting the code is out of question. Each jsp file is consisted of multiple includes with tons of global variables. I don't won't to touch it more than I need to.
重写代码是不可能的。每个jsp文件由多个包含大量全局变量的包含组成。我不会碰它超过我需要的。
回答by Ravi Thapliyal
Best Practices
最佳实践
Ideally, one shouldn't have any <% // scriptlet %>
blocks in JSPs any more.
理想情况下,<% // scriptlet %>
JSP 中不应再有任何块。
JSPs have evolved so much from simply hiding Java code behind Standard Actions and Custom Tags to using Expression Language, JSTL and now OGNL expressions to fetch the results (for the request just processed) from pre-populated JavaBeans available in any one of the application scopes (like session, request, page etc.) or sophisticated data stores (like ValueStack in Struts 2).
JSP 已经从简单地将 Java 代码隐藏在标准操作和自定义标签后面发展到使用表达式语言、JSTL 和现在的 OGNL 表达式来从任何一个应用程序范围中可用的预填充 JavaBeans 中获取结果(对于刚刚处理的请求) (如会话、请求、页面等)或复杂的数据存储(如 Struts 2 中的 ValueStack)。
So, a proper solution would look something like (can't use EL because we need a "user" reference)
所以,一个合适的解决方案看起来像(不能使用 EL,因为我们需要一个“用户”引用)
<jsp:useBean id="user" class="foo.User" /> <!-- scope="page" by default -->
This line of code when added to Title.jsp
would create a new User
bean and add it to the page
scope and this same line of code in Header.jsp
would then retrieve the User
bean from the page
scope and give it a reference named user
that can then be used throughout the rest of the file.
添加到这行代码Title.jsp
将创建一个新的User
bean 并将其添加到page
作用域中,然后同一行代码将从作用域中Header.jsp
检索User
beanpage
并为其提供一个名为的引用user
,然后可以在整个文件的其余部分中使用该引用.
But, since the rest of the Java code isn't written with tags this won't make much sense. So, you could also simply pass the bean between the two JSPs as a request attribute using one of your <% // scriptlet %> blocks.
但是,因为其余的 Java 代码不是用标签编写的,所以这没有多大意义。因此,您还可以使用 <% // scriptlet %> 块之一简单地将 bean 作为请求属性在两个 JSP 之间传递。
<% // in Title.jsp
request.setAttribute ("user", new User());
%>
<% // in Header.jsp
User user = request.getAttribute ("user");
user.setName ("John Doe");
%>
Answer
回答
If the code base is too huge to salvage and using any of the best practices is just plain impractical; you could configure your Eclipse IDE to ignore JSP syntax validation errors or better only turn them off for JSP fragments or specific files and folders.
如果代码库太大而无法挽救并且使用任何最佳实践都是不切实际的;您可以将 Eclipse IDE 配置为忽略 JSP 语法验证错误,或者最好仅针对 JSP 片段或特定文件和文件夹关闭它们。
With your web project selected in the workspace go to Project > Properties > Validation > JSP Syntax
. Then Enable project specific settings
and turn off Validate JSP fragments
as shown below.
在工作区中选择您的 Web 项目后,转到Project > Properties > Validation > JSP Syntax
. 然后Enable project specific settings
关闭Validate JSP fragments
,如下图所示。
A JSP fragment is a .jspf
file that contains a JSP/HTML segment without opening and closing header tags (unless it's a header or a footer fragment of course). So, although you would have to rename your files to .jspf
for Eclipse to recognize them as fragments (and not validate); the main advantages are:
JSP 片段是一个.jspf
包含 JSP/HTML片段的文件,没有打开和关闭头标记(当然,除非它是页眉或页脚片段)。因此,尽管您必须将文件重命名.jspf
为 Eclipse 才能将它们识别为片段(而不是验证);主要优点是:
- files can be anywhere in your folder structure
- the extension clearly indicates it's an include
- new fragments will get identified automatically
- 文件可以位于文件夹结构中的任何位置
- 扩展名清楚地表明它是一个包含
- 新片段将被自动识别
If the number of include files is huge or they can't be renamed for some reason your next best option would be to move them into a separate folder (like includes
) and then exclude the folder itself from JSP syntax validation.
如果包含文件的数量很大或者由于某种原因无法重命名,那么您的下一个最佳选择是将它们移动到一个单独的文件夹(如includes
),然后从 JSP 语法验证中排除该文件夹本身。
Again, go to Project > Properties > Validation and with project specific settings enabled click on the browse type [...]
button for accessing the JSP Syntax Validator
settings.
再次转到项目 > 属性 > 验证,并在启用项目特定设置的情况下单击浏览类型[...]
按钮以访问JSP Syntax Validator
设置。
In here, first create an Exclude Group
and then Add Rule
to exclude a folder(like WebContent/includesin the image) from JSP syntax validation.
在这里,首先创建一个Exclude Group
,然后Add Rule
到排除的文件夹(如的WebContent /包括从JSP语法验证在图像中)。
Now Eclipse would stop reporting errors for any JSP file dropped in this includesfolder. You could use the same approach for individual filesas well but how practical it is would again depend on how many such fragments you have.
现在,Eclipse 将停止报告包含在此包含文件夹中的任何 JSP 文件的错误。您也可以对单个文件使用相同的方法,但它的实用性将再次取决于您拥有多少这样的片段。
References:
How to avoid Java Code in JSP-Files?
回答by JoeG
To your question: Is there a way to tell Eclipse to ignore this particular error for this particular variable? As noted above, the error is coming from the validation framework in Eclipse. The best you can do is to tell eclipse to ignore that file - not that error or that variable. To do so, right click on the project and pull up it's properties dialog. Once there, select "Validation" and click the checkbox for "Enable project specific settings". From there you can turn on or off specific validations. Personally, I don't find the Eclipse validators all that useful and they take quite a bit of processing time, so I turn almost all of them off! However, you have some fine grain control via the "Settings" column. You hit the elipsis next to JSP Content Validator (for example) and you can specifically exclude only Header.jsp from there.
对于您的问题:有没有办法告诉 Eclipse 忽略此特定变量的此特定错误?如上所述,错误来自 Eclipse 中的验证框架。你能做的最好的事情就是告诉 eclipse 忽略那个文件——而不是那个错误或那个变量。为此,右键单击该项目并拉出它的属性对话框。在那里,选择“验证”并单击“启用项目特定设置”复选框。从那里您可以打开或关闭特定的验证。就我个人而言,我认为 Eclipse 验证器并不是那么有用,而且它们需要相当长的处理时间,所以我几乎将它们全部关闭了!但是,您可以通过“设置”列进行一些细粒度控制。
Hope this helps, it only provides the details to the earlier comments though.
希望这会有所帮助,但它只提供了早期评论的详细信息。