Aniket
Aniket

Reputation: 13

Issue while upgrading to struts version 2.5.17

I am trying to upgrade struts version from 2.3.35 to 2.5.17 but I encountered an issue as below:

java.lang.NullPointerException
    at com.opensymphony.xwork2.util.fs.StrutsJarURLConnection.getInputStream(StrutsJarURLConnection.java:170)
    at com.opensymphony.xwork2.util.fs.JarEntryRevision.needsReloading(JarEntryRevision.java:84)
    at com.opensymphony.xwork2.util.fs.DefaultFileManager.fileNeedsReloading(DefaultFileManager.java:65)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.needsReload(XmlConfigurationProvider.java:428)
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.needsReload(StrutsXmlConfigurationProvider.java:163)

I have been using this guide to migrate to struts version 2.5.17: https://cwiki.apache.org/confluence/display/WW/Struts+2.3+to+2.5+migration

I suspect it to be issue with tiles.

I have upgraded all struts related jar to version 2.5.17 including struts2-tiles-plugin. I have also upgraded all tiles related jars to 3.0.7.

Also I have removed the Xwork-core jar as from 2.5 xwork being merged to struts2-core jar.

Am I doing anything wrong.

Please note: I have not done any code changes as of now. The code works perfectly with struts version 2.3.35. But as soon as I have upgraded the struts version along with tiles version I started getting this issue.

Can some one please suggest If I am doing anything wrong?

Upvotes: 0

Views: 5558

Answers (2)

Aniket
Aniket

Reputation: 13

I got this resolved by upgrading struts version to 2.5.18. It also worked fine when I downgraded the struts version to 2.5.13.

But it is not recommended to use struts version between 2.5.16 and 2.3.36 (inclusive), so I have upgraded it to 2.5.18

Upvotes: 0

Anmol Bhardwaj
Anmol Bhardwaj

Reputation: 664

Yes, there are supposed to be errors without code changes.

I don't think you are doing something wrong.

After adding the new .jars and removing the old ones it will only work when the code is compliant with the new framework.


Code changes would be:

.xml

  1. Add the following <code> to web.xml.
<filter>
<filter-name>struts2</filter-name>
<filter-class> org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
  1. Change struts-config.xml to struts.xml and make following changes:

    You can remove struts-config.xml completely and use annotations instead of .xml file.( since struts 2.5.17 )

<?xml version="1.0" encoding="ISO-8859-1" ?>        <!-- change to UTF-8 -->
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd "> <!-- change to struts-2.5.dtd -->
<struts-config>                     <!-- change to <struts>  -->
<!-- add <include file="struts-default.xml"/> -->
<form-beans>
<form-bean name="MyClassForm" type="forms.MyClassForm">
</form-bean>
</form-beans>

<action-mappings>       <!-- change to <package name="hello-default" extends="struts-default"> -->
<action path="/MyClass" name="MyClassForm" type="actions.MyClassAction"
validate="false">
<action name = “MyClass” class = “actions.MyClass”>
<forward name="success" path="/Index.jsp"/>
<result> /Index.jsp </result>
</action>
</action>
</action-mappings>          <!-- change to  </package> -->
<message-resources parameter="resources"/>
</struts-config>                <!-- change to  </struts> -->

.java

  1. Remove ActionForm.java files.

    Properties are included in ActionSupport class which our Action class is supposed to extend.

  2. Change Action.java

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class MyClassAction extends Action // change to ActionSupport {  
//fields are now a property of the ActionSupport class  

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

// change to public String execute() throws Exception {

MyClassForm input = (MyClassForm) form; // don't need this
input.setField1(“Hello”);    // change to setMessage(“Hello”);
return mapping.findForward(“success”);    // change to return Action.SUCCESS;

.jsp

  1. Actions to be performed in this JSP are:

    • Replace <%@ taglib %> directive
    • Use new set of tags defined by the struts-tags.tld
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
    <title>Hello!</title>
    </head>
    <s:form action="submit.action" method="post">
    <body>
    <s:textfield label="Name" name=" field1" />
    <s:property value="field1"/>
    <s:submit" />
    </body>
    </s:form>
    </html>

Cheers.

Upvotes: 0

Related Questions