Migration from Struts 1 to Struts 2

Struts 1 to Struts 2 migration strategy

You have to rewrite both action classes and JSP, however you can reuse your business services if they are exist on the service layer. The same is for persistence layer.

Note, if you can leave JSP implementation, then you should replace them with something that will work on the view layer. It could be html, javascript, another templating framework like freemarker or velocity. It could be SPA, or MPA application that communicates with the server via HTTP or web sockets. It's up to you how do you architect your application. Struts can parse usual HTTP requests (GET/POST) and REST with support of the plugin. A lot of plugins exist to help you to better use the framework.

Struts1 is too old and can't work with the latest Struts2 because they have mismatch of dependencies of different versions. You should just leave Struts1 and migrate it to Struts2. It's straightforward process that takes less time rather that rewriting to another framework like Spring MVC.

If you need more information about the migration process you can read this answer. However, a lot of information and code is already outdated comparing to the latest version of Struts and might not work the ideas remain the same.

Migrating from Struts1 to Struts2

In Struts 1 you should return ActionForward from the execute method. Struts 2 returns a result code of type String. So the code where ActionForward is expected should be replaced with result code. The action result should be configured to the action the same way like you configure forwards in Struts 1.

Create two result configs: one is redirectAction result type, and another dispatcher result type. Like this

<result name "redirect" type="redirectAction">${forwardPage}</result>
<result>${forwardPage}</result>

The code should be replace with

private String forwardPage; 

public String getForwardPage() { return forwardPage; }

public void setForwardPage(String forwardPage) {
this.forwardPage = forwardPage;
}

protected String getActionForward(FilterContext ctx, String key, boolean redirect) {
HashMap filterForwards = ctx.getFilterForwards();
String forwardPage = (String)filterForwards.get(key);
if(forwardPage == null)
return NONE;
if (redirect) {
setForwardPage(forwardPage);
return "redirect";
} else {
setForwardPage(forwardPage)
return SUCCESS;
}
}

The errors are provided by the ActionSupport class that your action should inherit. Then you can use the code

protected void setError(String msg) {
addActionError(getText("exception", new Object[]{msg}));
}

In the JSP you can display errors with

<s:actionerror/>

While migrating Struts 1 to Struts 2, the FormTag class in Struts 2 is missing a getOnsubmit method

The <s:form> tag belongs to Struts 2 core tag library. So, if you need to use your own tag that extends this one then you should extend org.apache.struts2.views.jsp.ui.FormTag class and provide your own implementation by overriding public methods, and adding additional methods. The onsubmit field has a protected modifyer.

But before doing any changes to the Struts 2 framework ask an advice from qualifyed experts: why do you need to do it? For carrying the old code to the new one? The code that is written for S1 is incompatible with S2.

onsubmit is a HTML attribute of the HTML <form> tag, and it could be set in the any other ways on the server, i.e. using OGNL, or on the client using JavaScript.

Also when migrating from Struts1 to Struts 2 you should read Struts 1 to Struts 2 migration strategy.

Migration Struts 1 to Struts 2 + Tiles 3 getting UI issues

Answering my own question.

Issue was resolved after adding below dtd in jsp,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Somehow I missed this to add in jsp.



Related Topics



Leave a reply



Submit