1

Tyring a simple example of json but it is showing error. I have included gson-2.2.3.jar in class path.

I am using netbeans 7.1. This program is not deploying.

Apache tomcat log is showing:

 Unable to load configuration. - action - file:/D:/APP/webApp1/build/web/WEB-INF/classes/struts.xml:10:73
Caused by: Error building results for action sayHi in namespace  - action - file:/D:/APP/webApp1/build/web/WEB-INF/classes/struts.xml:10:73
Caused by: There is no result type defined for type 'json' mapped with name 'success'.  Did you mean 'json'? - result - file:/D:/APP/webApp1/build/web/WEB-INF/classes/struts.xml:11:33

I am trying simple example. Please see what is the proble. Action Class

 public class AjaxActions extends ActionSupport {

    private String name;
    private String greeting;

    public String sayHi() {

        greeting = "HI " + name;
        return ActionSupport.SUCCESS;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }
}

Struts.xml

<package extends="struts-default,json-default" name="ajax-package" namespace="/ajax">
    <action class="example.AjaxActions" method="sayHi" name="sayHi">
        <result type="json">
        </result>
    </action>
</package>

in jsp file

<form action="" id="introForm">
            <label for="name">Enter Your Name</label>
            <input name="name">
            <input type="submit">
        </form>
        <script>
            $(function(){

                $("#introForm").submit(function(){

                var formInput=$(this).serialize();
                $.getJSON('ajax/sayHi.action', formInput,function(data) {

                $('.result').html('' + data.greeting + '');
                return false;

                });

            });
        </script>
7
  • Try adding the getters and setters for name and greeting in your action class Commented Jul 6, 2013 at 5:22
  • It was already there. I forget to include it. Not working Commented Jul 6, 2013 at 5:29
  • Try changing your struts package to only extend json-default Commented Jul 6, 2013 at 6:07
  • Now it is deploying but not giving any result Commented Jul 6, 2013 at 7:12
  • Your action class has package 'example' right? any errors on console? try using the network tab on Chrome Dev Tools or similar to analyze the ajax call Commented Jul 6, 2013 at 7:26

1 Answer 1

1

add struts2-json-plugin.jar according to struts2 version in your project libraries. if you are using struts2.3.4 then use struts2-json-plugin2.3.4.jar

And change your in your struts.xml

<package extends="struts-default,json-default" to <package extends="json-default".

And use the following html code.

    <form action="" id="introForm">
                <label for="name">Enter Your Name</label>
                <input type="text" id="name" name="name">
                <input onclick="javascript:getResultData();" type="submit">
           <span id="resultHtml"></span>
 </form>
            <script>
                function getResultData(){
                    var formInput=$("#name").val();
                    vat inputData={"name":formInput}
                    $.getJSON('ajax/sayHi.action', inputData,function(data) {

                    $("#resultHtml").html('' + data.greeting + '');
                    return false;

                    });
                   }

            </script>
Sign up to request clarification or add additional context in comments.

2 Comments

@user2302288 I have updated the answer.. try this one it ll help.
I tried now it is working properly. I am using struts2.3.4 for that we have to include struts2-jsonplugin 2.3.4.jar. Then it will work properly.It was only the problem of jar file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.