Does the Eclipse Web Tools Project handle refactor

2020-07-11 08:04发布

问题:

I've installed the Web Tools Project for Eclipse version 3.4. I've been trying to get refactoring working the way I think it should and have had no success.

I have one Java project that contains the Java classes for the jar that is put into the /WEB-INF/lib for a web site. Another project (a Dynamic Web project) has the JSP files for the same site. The Dynamic Web project is set up with the Java project as a required project in the build path. I have also set the Project References option for the Dynamic Web project to refer to the Java Project.

If I use refactoring to change a method name in one of the Java classes, I would expect that references to that method in the JSPs would be renamed. But it does not work. References in the Java project that contains the class and other Java projects that use that class have the references changed are refactored, but not references to the method inside the JSPs.

I've been fiddling with it for days, trying different versions of Eclipse and WTP. Deleting all my projects and files and setting them up again. Nothing makes any difference.

Does this work for anyone else? Is there something special I need to set to get this working?

回答1:

Sorry, this is not directly answering your question but nevertheless addressing your problem... maybe it helps, otherwise please ignore:

As matt b suggested, you should not place java code in a jsp. Instead 'simply' use Tags. If you're using the standard tag library, there's almost everything there that you need. For things that you must handle yourself, it's very simple to write your own tags once you know what tags are about.

With this trick you'll get a better application (view) architecture, opportunity for tests (hope you like them), adhere to the DRY (dont repeat yourself) principle and place all Java code in .java files, so that they will easily be picked up by any tool.

There's even an option for web.xml to enforce disabling scripting, leaving tags working, but if you already have a huge investment in scripted jsps, this might not be what you need...

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <scripting-invalid>true</scripting-invalid>
  </jsp-property-group>
</jsp-config>

Another answer to a question that you didn't ask is that IntelliJ Idea handles the scenario that you talk about very well (though I'm using eclipse myself day to day, this is one of the differences between both IDEs)



回答2:

My experiences with how WTP (or any flavor of Eclipses) treats Java code in scriptlet blocks in a JSP file has always been horrible: it never works well or the way you expect. I've seen Eclipse add <jsp:import> statements in the middle of the JSP file, and do all other sorts of weird formatting.

My only advice is to stay away from editing a code in a JSP inside Eclipse.

(My other piece of advice would be to not put Java code directly in a JSP file, and move to a MVC architecture, but that's a subject for another day...)



回答3:

Please vote for this to be possible here. Or create a new more specific feature request. In that link you can see that there are many issues regarding refactoring and JSP, but only a few people is aware that if they vote for them, they are a lot more likely to be implemented in the next eclipse release.

Please copy and fix the link before using it, because it gets corrupted. The "+" becomes "%2B". Here is the correct link: https://bugs.eclipse.org/bugs/buglist.cgi?quicksearch=jsp+refactor



回答4:

Maybe I am not understanding you correctly, but it sounds like that you are putting the classes in a Jar or a War before you are trying to refactor. Refactoring is for the current project, not for anything in your build path.

If I misunderstood your question, I apologize.