I have to deal with legacy JSP code and refactoring is a big part of my work. Very often I have to extract repeated parts of JSP code like this:
index.jsp
-----------
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Simple jsp page</title>
</head>
<body>
<%
String title = "Name";
String name = "Pavel Feldman";
String color = "blue";
%>
<fieldset style="width:100px;color:<%=color%>">
<legend><%=title.toUpperCase()%></legend>
<%=name%>
</fieldset>
</body>
</html>
Into something more reusable, like this:
/WEB-INF/frame.tag
---------------------------
<%@ tag isELIgnored="false" %>
<%@ attribute name="title" required="true" type="java.lang.String" %>
<%@ attribute name="color" required="true" type="java.lang.String" %>
<fieldset style="width:100px;color:$">
<legend><%=title.toUpperCase()%></legend>
<jsp:doBody/>
</fieldset>
index2.jsp
-------------
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="ui" tagdir="/WEB-INF/tags" %>
<html>
<head>
<title>Simple jsp page</title>
</head>
<body>
<%
String title = "Name";
String name = "Pavel Feldman";
String color = "blue";
pageContext.setAttribute("title", title);
pageContext.setAttribute("name", name);
pageContext.setAttribute("color", color);
%>
<ui:frame title="${title}" color="$">
${name}
</ui:frame>
</body>
</html>
I think it could be automated as "Refactor .tag file" refactoring which includes
1) Extracting selected piece of JSP code into new .tag file
2) Adding reference to taglib to original page if it was not there
<%@ taglib prefix="ui" tagdir="/WEB-INF/tags" %>
3) Enabling EL in original JSP file if it was not enabled
isELIgnored="false"
4) Putting necessary scriptlet local variables into pageContext to make them available to EL
pageContext.setAttribute("title", title);
5) Asking user which variables are passed as attributes and which one goes as body. Asking which attributes are required.
6) Replacing scriptlet variables usages in .tag file with EL when possible (optional)
<%=color%> to $
but leaving more complex expressions as they are
<%=title.toUpperCase()%>
7) Searching across the page or other pages for duplicated piece of JSP to replace duplicate with tag.
For me it looks like JSP version of Extract Method which saves me a lot of time.
Ability to replace scriptlet variables usage like <%=color%> with EL $ and adding them to pageContext (pageContext.setAttribute("color", color); ) could be separate refactoring.
IDEABKL-3141and IDEA-7711