java intellij 不解析 JSP 代码检查或自动完成中的 el 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12694392/
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-10-31 09:51:26  来源:igfitidea点击:

intellij not resolving el variables within JSP code inspection or autocomplete

javajspintellij-ideael

提问by NimChimpsky

To summarize the answer shown here Code assist in (jsp /jstl) view for Spring MVC model objects in Eclipseis not working for me at all, is there a setting that I need to change ?

总结这里显示的答案Eclipse 中 Spring MVC 模型对象的 (jsp / jstl) 视图中的代码辅助根本不适合我,是否有我需要更改的设置?

I have just downloaded the sample spring-mvc-showcaseon github, and it doesn't work out of the box on that project (with either 11.1.3 or EAP 12 version both full enterprise editions), see below (I have no idea where it gets formBean from) :

我刚刚在 github 上下载了示例spring-mvc-showcase,但它在该项目上无法开箱即用(11.1.3 或 EAP 12 版本都是完整的企业版),请参见下文(我不知道它从哪里获取 formBean):

fruit and foo, both work, but not available as autocomplete options

Fruit 和 foo,都可以工作,但不能用作自动完成选项

Here is an example from my own project,the screen shot below (bottom frame) shows my controller adding a string attribute to model and returning correct view name. I would then expect shopString to be offered up as autocomplete option when editing that view, however it is not : JSP shown abover controller, the autocomplete offered up when "s" typed

这是我自己项目中的一个示例,下面的屏幕截图(底部框架)显示了我的控制器向模型添加字符串属性并返回正确的视图名称。然后,我希望在编辑该视图时将 shopString 作为自动完成选项提供,但它不是: JSP 显示在控制器上方,输入“s”时提供自动完成功能

sg is a javascript variable - so great it should be there, but where is "shopString" ?. Is there a setting I need to change or something else I am missing to get this functionality (using 11.1.3 enterprise edition with all the spring plugins).

sg 是一个 javascript 变量 - 它应该在那里很棒,但是“shopString”在哪里?。是否有我需要更改的设置或我缺少的其他内容才能获得此功能(使用 11.1.3 企业版和所有 spring 插件)。

It is also failing on spring specific variables : enter image description here

弹簧特定变量也失败: 在此处输入图片说明

IS their an open source (one of the spring tutorial projects?) where this definitely works ... or is there a setting I need change in my Intellij install (I have tested with a brand new download of the version 12 EAP) ?

他们是一个开源(春季教程项目之一?),这绝对有效……或者我的 Intellij 安装中是否有需要更改的设置(我已经使用全新的 12 EAP 版下载进行了测试)?

One more screenshot below shows all my spring coifg files set up correctly via autodetection, but the code inspections fails ... this is the spring-mvc-showcaseproject : code inspection and spring config

下面的另一个屏幕截图显示了我通过自动检测正确设置的所有 spring coifg 文件,但是代码检查失败了……这是spring-mvc-showcase项目: code inspection and spring config

回答by Pakka Pakka

There's a standard way to do this, which is not IntelliJ-specific.

有一种标准方法可以做到这一点,它不是 IntelliJ 特定的。

<jsp:useBean id="someModel" scope="request" type="foo.bar.SomeModelClass"/>

The typeattribute here does not need to be a concrete class, it can be an interface type as well. Typically you'd put these declarations at the start of your JSP/JSPX files, to provide something like a "declaration of model inputs".

这里的类型属性不需要是具体的类,也可以是接口类型。通常,您会将这些声明放在 JSP/JSPX 文件的开头,以提供类似于“模型输入声明”的内容。

Using JSPs in such a declarative way was recommended in the original book on Spring, in fact (Expert One-on-One J2EE Design and Development.). IntelliJ has been providing full code completion for such pages since at least 7 years.

实际上,关于 Spring 的原著(专家一对一 J2EE 设计与开发)中推荐以这种声明方式使用 JSP 。至少 7 年来,IntelliJ 一直在为此类页面提供完整的代码补全。

Note that there are additional relevant convenience features in IntelliJ: if an EL variable reference is marked as undefined, you can press Alt-Enterto select a QuickFix, which will insert a declaration like above. It will even try to figure out the actual type, based on the properties you're accessing.

请注意,IntelliJ 中还有其他相关的便利功能:如果 EL 变量引用被标记为未定义,您可以按Alt-Enter选择一个 QuickFix,这将插入如上的声明。它甚至会根据您正在访问的属性尝试找出实际类型。

回答by mana

As I understand Spring, there is no declaration for definitions of variables that you may put into your model. The call model.addAttribute()may add an object to the model, either identified by a parameter or automatically generated by the class name of the object.

据我了解,Spring 没有声明可以放入模型中的变量定义。该调用model.addAttribute()可能会向模型添加一个对象,该对象要么由参数标识,要么由对象的类名自动生成。

So imagine the following case where you have more than one method:

因此,请想象以下情况,其中您有多个方法:

@RequestMapping("foo") public String foo(Model model) { 
    model.addAttribute("model", new Foo());
    return new Random().nextBoolean() ? "page" : "someOtherPage";
}
@RequestMapping("bar") public String bar(Model model) { 
    model.addAttribute("model", new Bar());
    model.addAttribute("model", new Foo());
    model.addAttribute("model", new Bar());
    return new Random().nextBoolean() ? "page" : "someOtherPage";
}

and the JSP would be something like

并且 JSP 将类似于

<c:out ${model.value} />

Since there is no proper mapping of which controllers may under some circumstances forward to which views, nor what exactly lies within the model, your IDE has no real chance to provide you with proper information.

由于在某些情况下没有正确映射哪些控制器可以转发哪些视图,也没有模型中的确切内容,因此您的 IDE 没有真正的机会为您提供正确的信息。

But to support the IDE in suggesting you some useful information, you can use type hints. Therefore, you have to copy the whole reference of an object, e. g. fooand add a JSP comment like:

但是为了支持 IDE 向您建议一些有用的信息,您可以使用类型提示。因此,您必须复制对象的整个引用,例如foo并添加如下 JSP 注释:

<%--@elvariable id="foo" type="com.mycompany.SomeObject"--%>

The warning will vanish and the full IDE support is on your side, allowing you to traverse the fieldsof foo. One of the nicest things is that the unused getter warningswill vanish, too. You can directly call the show usagesaction directly from the JSP or the POJO.

该警告将消失,完整IDE支持就在你身边,让你穿越田野foo。最好的事情之一是未使用的 getter 警告也会消失。您可以直接从 JSP 或 POJO直接调用show usages操作。

This also works with JSF and particularly within JSF components. Pretty neat feature to have this kind of code completion, showing warnings and errors.

这也适用于 JSF,尤其是在 JSF 组件中。具有这种代码完成功能的非常简洁的功能,显示警告和错误。

Hope that helps you with your switch to Intellij Idea.

希望能帮助您切换到 Intellij Idea。

Edit:I also reported this finding to a friend wo wrapped the whole thing into a nice blog entry. Maybe you're interested in reading it: open link

编辑:我还向一位朋友报告了这一发现,我将整个事情打包成一个不错的博客条目。也许你有兴趣阅读:打开链接

回答by NimChimpsky

This got fixedin the latest release of intellij 122.694

这在Intellij 122.694的最新版本中得到了修复

回答by Andrew

I faced with similar issue when start writing my own interceptor. Problem was that I start using refference in my view resolver configuration xml issue

开始编写自己的拦截器时,我遇到了类似的问题。问题是我开始在我的视图解析器配置中使用引用 xml issue

don't use contruction like this

不要使用这样的结构

<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" ref="prefix"/>
    <property name="suffix" ref="suffix"/>
</bean>-