Monday, December 23, 2013

How to Integrate Struts2.x in web application

Perform following steps to integrate Struts2.x.


1) Download struts2.3 from struts.www.apache.org/download.cgi‎ website. You will be getting couple of jars, however you no need to take all jars in your application.


2) Copy following jars in WEB-INF\lib folder


   1. struts2-core-2.3.15.3.jar
   2. xwork-core-2.3.15.3.jar
   3. ognl-3.0.6.jar
   4. javassist-3.11.0.GA.jar
   5. commons-lang3-3.1.jar
   6. commons-io-2.0.1.jar
   7. core-0.6.2.jar
   8. Xecers.jar
   9. xalan.jar

 3) Configure FilterDispatcher in the web.xml file to intercept each and every http request and response


<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>

4) Place Struts.xml file in WEB-INF\classes file and configure action in xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <constant name="struts.devMode" value="true" />
   <package name="Test" extends="struts-default">
    
      <action name="testAction" class="com.test.TestAction" method="execute">
     <result name="success">/test/Test.jsp</result>
     </action>

   </package>
   <-- more packages can be listed here -->

</struts>

5) Write TestAction class file which will invoke execute method.

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext

public class TestAction extends ActionSupport {
    private String name;
    public String execute() throws Exception {
            System.out.println("TestAction" + name);
                        return SUCCESS;
        }
    public String getName(){
       return name;
    }
    public void setName(){
      this.name = name;
    }
}

 6) Write Test.jsp file and use value stack to take values from response
<%@page import="com.opensymphony.xwork2.util.ValueStack"%>
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@page import="com.opensymphony.xwork2.ognl.OgnlValueStack"%>
<%
    ValueStack stack = ActionContext.getContext().getValueStack();
    String name = (String) stack.findValue("name");
    System.out.println("Name :"+name);
%>
<html>
    <body>
        <form method="post" action ="TestAction.action">
            <--- Write any HTML code which suites you
            use ognl for using tag or use struts inbuild tags.
            or use free form html tags. Its up to developer-->
        </form>
    </body>
</html>

7) After Prepare war file and deploy in the server.


Issues which you can see -

1) Sometimes Jar files conflict happens and that is too because of either classpath or server used.
2) IF you see xml parser issue then check your lib folder its quite possible that you are using xml jar file which is conflicting with server parsing jar file.in that case write servletcontextlistener and load you jar file during server startup.

No comments:

Post a Comment