简体   繁体   English

IntelliJ IDEA中的JSP调试

[英]JSP debugging in IntelliJ IDEA

Does anyone know how to debug JSP in IntelliJ IDEA? 有谁知道如何在IntelliJ IDEA中调试JSP

When I set breakpoint in my JSP files, those breakpoints never seem to take effect. 当我在JSP文件中设置断点时,这些断点似乎永远不会生效。 The debugger never hits them. 调试器从不击中它们。 IDEA seems to think that the breakpoints are valid. IDEA似乎认为断点是有效的。 I do see a red dot placed to the left of the line where I place my breakpoint. 我确实在放置断点的行的左侧看到了一个红点。

I read in IntelliJ forum in this post that JSP files need to be under web-inf for debugging to work. 在这篇文章的 IntelliJ论坛读到, JSP文件需要位于web-inf下才能调试工作。

But then I also read that JSP files placed under web-inf won't be directly accessible by the user. 但是后来我又读到,放置在web-inf下的JSP文件将不能被用户直接访问。

I am not sure who's really right. 我不确定谁是正确的。

For JSP debugging in Intellij there are some configurations that must be in order. 对于Intellij中的JSP调试,必须按顺序进行一些配置。 The fact that Intellij always allows you to add a breakpoint on a JSP line does not necessarily imply that you've configured JSP debugging. Intellij始终允许您在JSP行上添加断点的事实并不一定意味着您已经配置了JSP调试。 In the following I refer to Intellij 8 configuration, wrt previous versions you will need to do similar operations as the concepts are the same. 在下面的内容中,我指的是Intellij 8的配置,由于概念相同,因此您需要执行类似的操作。

In order to enable JSP debugging you must do two steps: set a web application configuration in your project and add a web application server configuration. 为了启用JSP调试,您必须执行两个步骤:在项目中设置Web应用程序配置并添加Web应用程序服务器配置。

Web application Configuration : in order to have JSP debugging, you must have a “web” facet in your project structure, pointing to the correct web.xml file. Web应用程序配置 :为了进行JSP调试,您的项目结构中必须有一个“ web”构面,指向正确的web.xml文件。 Depending on the kind of web application structure you are using, the facet may be detected automatically by Intellij (go anyway to check what it has done) or you may have to add it manually. 根据所使用的Web应用程序结构的类型,Intellij可能会自动检测该构面(无论如何都要检查它是否完成了操作),或者您可能必须手动添加它。 Remember in the “Java EE build settings” tab to set as anable “Create web facet exploded directory”; 记住在“ Java EE构建设置”选项卡中将其设置为“创建Web Facet分解目录”。 if you don't want duplications, a trick is just to enable it and point to your already existing directory. 如果您不想重复,一个诀窍就是启用它并指向您已经存在的目录。

(Web) Application server : Go to “edit configurations”, there you have to add to configurations an application server, not launch the web server as an application like any other. (Web)应用程序服务器 :转到“编辑配置”,您必须在其中添加应用程序服务器的配置,而不是像其他任何应用程序一样启动Web服务器。 In this way Intellij will be able to intercept JSP calls. 这样,Intellij将能够拦截JSP调用。 In the list of application servers, you should have the default one, Tomcat. 在应用程序服务器列表中,您应该具有默认服务器Tomcat。 Be sure to have a local Tomcat installation before you do this, and point to that when adding the web application server. 在执行此操作之前,请确保具有本地Tomcat安装,并在添加Web应用程序服务器时指向该位置。 The last trick is going to the “Deployment” tab and selecting as “Deployment source” the same facet that you configured in the previous step. 最后一个技巧是转到“部署”选项卡,并选择与上一步中配置相同的方面作为“部署源”。

The same configuration works if you want to use another web application server, I tested it with the latest Caucho Resin releases and debugging works fine (it didn't with the previous Intellij and Resin combinations). 如果您要使用其他Web应用程序服务器,则相同的配置也可以使用,我在最新的Caucho Resin版本中对其进行了测试,并且调试工作正常(不适用于以前的Intellij和Resin组合)。

If you don't see Tomcat in the list of available application servers to add, check the plugins in the general Intellij settings pane: in the latest releases, more and more functionality has become “pluggable”, and even very basic functions may be disabled; 如果在要添加的可用应用程序服务器列表中没有看到Tomcat,请检查Intellij常规设置窗格中的插件:在最新版本中,越来越多的功能已变为“可插拔”,甚至可能禁用了非常基本的功能。 ; this plugin is called “Tomcat integration”. 该插件称为“ Tomcat集成”。

Finally, it is surely not true that JSP files need to be under WEB-INF to be under debugging. 最后,肯定不正确的是,JSP文件需要在WEB-INF下才能进行调试。

For remote JSP debugging (which also applies to localhost ) you'll need to install the JSR45support plugin . 对于远程JSP调试(也适用于localhost ),您需要安装JSR45support插件 Please note this feature is only supported in the Ultimate edition of IntelliJ, not the community edition. 请注意,此功能仅在IntelliJ的Ultimate版中受支持,而在社区版中不受支持。

  1. Go to Preferences > Plugins , search for the JSR45 plugin, and enable it. 转到“首选项” >“ 插件” ,搜索JSR45插件,然后启用它。
  2. Create a run configuration: Run > Run Configuration > click the + button, and pick JSR45 Compatible Server , and then in the dialog that opens, select Remote , and set server host and port. 创建运行配置: 运行 > 运行配置 >单击+按钮,然后选择JSR45 Compatible Server ,然后在打开的对话框中选择Remote ,然后设置服务器主机和端口。 Setting Application Server: Generic should work fine. 设置应用服务器: 通用应该可以正常工作。
  3. Make sure you set the correct port in Startup/Configuration > Debug . 确保在“ 启动/配置” >“ 调试”中设置正确的端口。

  4. Open the module settings ( F3 on the project folder), and add a Web Facet under Facets , and under Web Resource Directories specify your JSP root folder. 打开模块设置(在项目文件夹上为F3 ),然后在Facets下添加一个Web Facet ,并在Web Resource Directories下指定您的JSP根文件夹。

  5. Click the Configuration... button, and select the folders with the beans, classes and libraries that your JSPs depend on. 单击Configuration ...按钮,然后选择包含JSP所依赖的bean,类和库的文件夹。

Now JSP breakpoints should work, provided that you started your server with the proper debug arguments . 现在,只要您使用适当的调试参数启动服务器,JSP断点就应该可以使用。

If you have a maven project with auto-import enabled then you might want to disable auto-import because every time the auto-import is triggered your library settings will be reset. 如果您有一个启用了自动导入的Maven项目,则可能要禁用自动导入,因为每次触发自动导入时,库设置都会被重置。

无论如何,您需要在IDEA中启动Tomcat,而不是从远程Tomcat中启动。

Please make sure, that in you tomcat's conf/web.xml suppressSmap is not enabled as support of JSR45 is required by IntelliJ's debugger. 请确保,在你的tomcat的conf / web.xml中 suppressSmap 没有作为支持JSR45的是的IntelliJ调试器需要启用。

It should look like this: 它看起来应该像这样:

<init-param>
  <param-name>suppressSmap</param-name>
  <param-value>false</param-value>
</init-param>

From https://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html 来自https://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html

suppressSmap - Should the generation of SMAP info for JSR45 debugging be suppressed? preventSmap-是否应禁止为JSR45调试生成SMAP信息? true or false, default false. 是或否,默认为false。

For the second part of your question ("jsp files placed under web-inf won't be directly accessible by user") that is correct. 对于问题的第二部分(“放置在web-inf下的jsp文件将不能被用户直接访问”),这是正确的。 To allow users to access JSP files in the WEB-INF folder servlet and servlet-mapping entries need to be made in the web.xml file for each JSP page. 为了允许用户访问WEB-INF文件夹中的JSP文件,需要在每个JSP页面的web.xml文件中创建servlet和servlet映射条目。

If you are using the Intellij debugger you can get the value of an individual attribute by putting a breakpoint inside JSP and evaluating the expression this.jspContext.request.getAttribute("attributeName") . 如果使用的是Intellij调试器,则可以通过在JSP内放置一个断点并评估表达式this.jspContext.request.getAttribute("attributeName")来获取单个属性的值。

Note that this may return a Java Object type, and you may have to cast it to the correct type. 请注意,这可能会返回Java Object类型,并且您可能必须将其强制转换为正确的类型。 Also if you launch a remote Tomcat the IDEA won't hit any breakpoints, so you need to launch the Tomcat in debug mode from inside the IDEA. 另外,如果您启动远程Tomcat,则IDEA不会遇到任何断点,因此您需要从IDEA内部以调试模式启动Tomcat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM