跨越多个包含块的Java变量 - 变量无法解析 [英] Java variable across multiple include blocks - variable cannot be resolved

查看:370
本文介绍了跨越多个包含块的Java变量 - 变量无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有jsp页面看起来像这样:



MyPage.jsp



 <%@ include file =/Title.jsp%> 
<%@ include file =/Header.jsp%>
< html> ...< / html>

在Title.jsp中有一个用户的实例,让我们说:

  User user = new User(); Header.jsp用户被引用为



  user.setName( 约翰); 

Eclipse在header.jsp文件中显示用户无法解析为变量的错误。我完全明白为什么有没有办法告诉Eclipse忽略这个特定变量的这个特定错误?有什么可以在这里完成吗?



工作,我支持一些实际上是一个Web应用程序的旧(和大而丑)的Java应用程序。我正在尝试将这个Java项目转换为Java动态Web项目。在我转换之前,不知何故,Eclipse并不关心这些挂起的变量。现在,在动态Web项目中,Eclipse正在抱怨。



重写代码是没有问题的。每个jsp文件由多个包含大量全局变量的包含。

解决方案

最佳做法



理想情况下,JSP中不应该有任何<%// scriptlet%> 块。



从简单的隐藏标准操作和自定义标签的Java代码到使用表达式语言,JSTL和现在的OGNL表达式,从预填充的文件中获取结果(刚刚处理的请求),JSP已经演变得如此之多JavaBeans可以在任何一个应用范围(如会话,请求,页面等)或复杂的数据存储(如Struts 2中的ValueStack)中。



所以,一个适当的解决方案看起来像(不能使用EL,因为我们需要一个用户参考)

 < jsp:useBean id =userclass =foo.User/> <! -  scope =page - > 

添加到 Title.jsp 将创建一个新的用户 bean,并将其添加到页面范围,同样的代码 Header.jsp 然后将从页面范围内检索用户并给它一个名为 user 的引用,然后可以在文件的其余部分使用。



但是,由于Java代码的其余部分不是用标签写的,这并不太有意义。因此,您也可以简单地将两个JSP之间的bean作为请求属性使用您的<%// scriptlet%>块之一传递。

 code><%// in Title.jsp 
request.setAttribute(user,new User());
%>

<%Header.jsp
用户user = request.getAttribute(user);
user.setName(John Doe);
%>



答案



太大的挽救和使用任何最佳做法是不切实际的;您可以配置Eclipse IDE以忽略JSP语法验证错误,或者更好地将其关闭用于JSP片段或特定文件和文件夹。



在工作区中选择您的Web项目转到项目>属性>验证> JSP语法。然后启用项目特定设置并关闭验证JSP片段,如下所示。





JSP片段是一个 .jspf 文件,其中包含没有打开和关闭标题标签的JSP / HTML段(除非是标题或页脚片段)。所以,尽管您必须将您的文件重命名为 .jspf ,以便Eclipse将其识别为片段(而不是验证);主要优点是:




  • 文件可以在您的文件夹结构中的任何位置

  • 扩展名清楚地表示这是一个包含

  • 新的片段将自动识别



如果包含文件的数量是因为某些原因,您的下一个最佳选择是将其移动到单独的文件夹(例如包含),然后从JSP语法中排除文件夹本身,则无法重命名验证



再次,转到项目>属性>验证并启用项目特定设置点击浏览类型 [...] code>按钮,用于访问 JSP语法验证器设置。





在这里,首先创建一个排除组然后添加规则 排除文件夹 (如 WebContent / includes 在图像)从JSP语法验证。





现在Eclipse将停止报告此包含文件夹中丢失的任何JSP文件的错误。您可以 对个别文件使用相同的方法 ,但实际情况又取决于您有多少这样的片段。



参考文献:

如何避免JSP文件中的Java代码?


I have jsp page that looks something like this:

MyPage.jsp

<%@ include file = "/Title.jsp" %>
<%@ include file = "/Header.jsp" %>
<html>...</html>

In Title.jsp there is a instance of "user", let's say:

User user = new User();

in Header.jsp user is referenced as

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?.

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.

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.

解决方案

Best Practices

Ideally, one shouldn't have any <% // scriptlet %> blocks in JSPs any more.

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).

So, a proper solution would look something like (can't use EL because we need a "user" reference)

 <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.

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.

<% // 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.

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.

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:

  • 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.

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.

In here, first create an Exclude Group and then Add Rule to exclude a folder (like WebContent/includes in the image) from JSP syntax validation.

Now Eclipse would stop reporting errors for any JSP file dropped in this includes folder. You could use the same approach for individual files as well but how practical it is would again depend on how many such fragments you have.

References:
How to avoid Java Code in JSP-Files?

这篇关于跨越多个包含块的Java变量 - 变量无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆