1

Here I am trying into integrating Struts 2 with JSON ,jQuery and ajax It is calling to the action class but i am not getting json response. I have added all json plugin and json-defult in struts-xml

Ajax function

function callajax() {
    alert(1);
    $.getJSON('edituser.action', function(data) {
        alert(data);
    });
}

Action class.

package com.hyva.accounts.master.useraccountsetup;

import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author ranjeeth.g
 */
public class MasterUserSetUpActionEdit extends ActionSupport {

    private String username = "ranjith";
    private String password = "1234";
    private String fullname = "Ranjith";
    private String emailaddress = "Ranjith";
    private String telephone = "8762232246";

    @Override
    public String execute() {
//        System.out.println("session = " + session.isOpen());
        return "success";
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getEmailaddress() {
        return emailaddress;
    }

    public void setEmailaddress(String emailaddress) {
        this.emailaddress = emailaddress;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
}

Struts-xml

<action name="edituser" class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpActionEdit">
            <result type="json"></result>
   </action>

Updated code;

<?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>
    <package name="accounts" namespace="/" extends="hibernate-default,json-default">    
        <action name="login" class="com.hyva.accounts.userlogin.LoginAction" >
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">/index.jsp</result>
            <result name="error">/login.jsp</result> 
            <result name="input">/login.jsp</result>          
        </action> 

        <action name="edituser" class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpActionEdit">
            <result type="json"></result>
        </action>

        <action name="userlist"  class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpActionList">
            <result name="success">/setup_useracount_list.jsp</result>
        </action>

        <action name="adduser"  class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpAction">
            <interceptor-ref name="jsonValidationWorkflowStack"/>
            <result name="input">/setup_useracount_list.jsp</result> 
            <result name="success">/setup_useracount_list.jsp</result>
        </action>
        <action name="countryList" method="list" class="com.hyva.accounts.master.country.CountryAction">
            <result name="success">/setup_country_list.jsp</result>
        </action>
        <action name="addCountry" method="saveOrUpdate" class="com.hyva.accounts.country.CountryAction">
            <result name="success" type="redirect">countryList</result>
        </action>         
    </package>
</struts>

If call to this url i am getting

java.lang.NoClassDefFoundError: org/apache/commons/lang/xwork/StringUtils
    org.apache.struts2.json.SerializationParams.<init>(SerializationParams.java:57)
    org.apache.struts2.json.JSONResult.writeToResponse(JSONResult.java:214)
    org.apache.struts2.json.JSONResult.execute(JSONResult.java:204)
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:361)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:265)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
Roman C
  • 49,761
  • 33
  • 66
  • 176

2 Answers2

1

1) Include json according to your struts2 version from here

2) Your package should extends json-default as below

<package name="example1" extends="struts-default, json-default">
<action name="edituser" class="com.hyva.accounts.master.useraccountsetup.MasterUserSetUpActionEdit">
            <result type="json"></result>
   </action>
</package>

3)Check your action for json result like your action name in address bas http://localhost:8080/edituser

It will show json response to browser.

xrcwrn
  • 5,339
  • 17
  • 68
  • 129
  • thanks for your replay this is what i am using struts2-core-2.1.6.jar and struts2-json-plugin-2.1.8.1.jar and i have updated my code please look at above. – user4135983 Oct 13 '14 at 06:05
0

Include Apache's commons-lang-2.x.jar.

Then, since major changes occurred in 2.1.8, be aware that mixing 2.1.6 with 2.1.8 stuff is really, really bad.

Use the same version for all your struts libraries. Struts-core, Struts-json, others if any (struts-spring-plugin, struts-config-browser-plugin, struts-junit-plugin, etc). All except struts-jquery-plugin, frremarker and ognl (that btw must be picked with respect to the right version as specified in Struts dependecies page).

Then it is fundamental trying to upgrade to the latest version (2.3.16.3), due to security issues (and other reasons). If you'll go that way, then include both Apache's commons-lang-2.x.jar and Apache's commons-lang3-3.x.jar (if only the latter isn't enough, because of legacy code). Since they're in different package, they can cohexist.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243