0

I was trying to get bean object and other instance field of Action class in JSON. I am implementing ModelDriven to set data coming from jquery ajax into a bean class.I have another field in action class with getter and setter.But it is not setting in Json object.
Here is my
index.jsp

<script type="text/javascript">
$(document).ready(function() {
    $('#submit').click(function() {
        var rollNo=$('#rollNo').val();
        var name=$('#name').val();
        var totalMarks=$('#totalMarks').val();

        $.ajax({
            url:'submitForm',
            type:'POST',
            data:{rollNo:rollNo,name:name,totalMarks:totalMarks},
            success:function(data){
                alert(data.rollNo);
            }
        });
    });
});
</script>
</head>
<body>
<table>
<tr>
<td>Roll No:</td>
<td><input id="rollNo" type="text" name="rollNO"></td>
</tr>
<tr>
<td>Name:</td>
<td><input id="name" type="text" name="name"></td>
</tr>
<tr>
<td>Total Marks:</td>
<td><input id="totalMarks" type="text" name="totalMarks"></td>
</tr>
<tr>

<td colspan="2"><input id="submit" type="submit" value="Submit"></td>
</tr>
</table>


And struts.xml is

<package name="default" namespace="/" extends="struts-default,json-default">    
        <action name="submitForm" class="org.mz.example.action.Action">
         <result type="json">

         </result>
        </action>
    </package>


And Acton.java is

public class Action extends ActionSupport implements ModelDriven<Student>{

    private Student student=new Student();
    private String result;

    public String execute(){
        System.out.println(student.getName());
        setResult("Hello");
        return SUCCESS;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public Student getModel() {
        // TODO Auto-generated method stub
        return student;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    <br>

I want to get instance field result value of action class in Json. But in jquery, What i am getting is JSON object is acting as Student Object.
Here is bean class Student.java

public class Student {

    private String rollNo;
    private String name;
    private String totalMarks;
    public String getRollNo() {
        return rollNo;
    }
    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTotalMarks() {
        return totalMarks;
    }
    public void setTotalMarks(String totalMarks) {
        this.totalMarks = totalMarks;
    }
}


I want to get value of result field and bean object Student in JSON on index.jsp.
I dont want <param name="root">rsult</param>I want both result and bean class at one time.
How can I do it.
I want also one suggesstion. If i send data in JSON to action class.How will it automatically set it to bean class and other data to instance of Action class.

Arrow
  • 165
  • 2
  • 12
  • This is no duplicate. I want data from action to jsp. Not reverse. @RomanC – Arrow Jun 06 '16 at 16:54
  • @ Roman C please read again. I want data from action to jsp. What i am getting is json object actiing as Student object. When data is coming from action to jsp, it is not setting String result in json object only bean class object – Arrow Jun 06 '16 at 17:01
  • Don't use modeldriven then. – Aleksandr M Jun 06 '16 at 17:58
  • @AleksandrM then will i have to manually set the data in bean class.Any other solution – Arrow Jun 07 '16 at 04:04
  • What do you mean by manually? In the action? If you have setter/getter for property then it should be ok. – Aleksandr M Jun 07 '16 at 17:11
  • What is the purpose of ModelDriven? It will populate bean class of Student.If there will be no ModelDriven,then i am asking how will I populate Student bean class. I have to manullay set in it ? – Arrow Jun 08 '16 at 03:10
  • No. You need a field with getter/setter in your action. See some tutorials - http://struts.apache.org/docs/tutorials.html. – Aleksandr M Jun 08 '16 at 18:00
  • @AleksandrM then u are saying i have to change name of input field like . Is this you are telling me? – Arrow Jun 09 '16 at 06:42
  • Exactly. And drop `=new Student()`, S2 will initialize object for you, upon form submission. – Aleksandr M Jun 10 '16 at 15:35
  • 1
    @AleksandrM Thanks... – Arrow Jun 11 '16 at 16:25

0 Answers0