如何使用用户定义的对象从struts操作类填充文本框

时间:2016-08-12 09:15:13

标签: jquery ajax struts2

我已经用一个字符串填充了一个文本框,例如struts动作类String name =“Apple”这个苹果我已经使用juery ajax和json填充在文本框中。 现在我改变了一点假设有一个这样的Person Entity类和
以前我在struts.xml

<param name="root">name</param> 

现在我应该在struts.xml

中拥有什么

public class Person {
    private String name;

    public String getName() {
        return name;
    }

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

动作类

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONException;
import org.json.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

public class DemoAction extends ActionSupport implements ServletRequestAware {
    HttpServletRequest request;
    JSONObject obj = null;
    Person person = null;

    public String execute() {
        String requestedNumber = getServletRequest().getParameter("userNumber");
        System.out.println(requestedNumber);
        person = new Person();
        person.setName("pradeep");
        obj = new JSONObject();
        try {
            obj.put("userName", person.getName());
            String json = obj.toString();
            System.out.println(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public JSONObject getObj() {
        return obj;
    }

    public void setObj(JSONObject obj) {
        this.obj = obj;
    }

    public HttpServletRequest getServletRequest() {
        return this.request;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

}

struts.xml中

<struts>
    <package name="default" extends="json-default">
        <action name="populateTextBox" class="com.pradeep.demo.DemoAction">
            <result type="json" >
            <param name="noCache">true</param>
        <param name="excludeNullProperties">true</param>
        <param name="root">person</param>
            </result>
        </action>
    </package>
</struts>

Jquery的

 <script>
        $(document).ready(function () {
            $('#userCheck').change(function (event) {
                if(!$(this).is(":checked"))
                    return;
                var userNumber = $('#userNumber').val();           
                if(userNumber != ""){
                    $.ajax({
                        type : 'GET',
                        url : 'populateTextBox',
                        dataType : 'json',
                        data : {
                            userNumber : $('#userNumber').val()
                            },
                        success : function(data, success) {
                            console.log(data.userName)
                            $('#userName').val(data.userName);                      
                        },
                        error : function(data, success) {
                            console.log(data.person.name)
                            document.getElementById("er").innerHTML = "Problem to upload a name";
                            er.style.color='red';
                            $('#er').show();                        
                        }
                    });
                }else {
                    //FIXME: Handle this properly
                    alert("Enter User Number")
                }
            });
        });
    </script>
    </head>
    <body>
        <s:form action="populateTextBox">
            <s:textfield name="userNumber" id="userNumber"></s:textfield>       
            <s:checkbox id="userCheck" name="userCheck"></s:checkbox>
            <s:textfield name="userName" id="userName"></s:textfield>
            <td><p id="er"></td>
            <s:submit/>
        </s:form>
    </body>

1 个答案:

答案 0 :(得分:0)

您可以像这样定义struts.xml结果:

 <!-- Result fragment -->
   <result type="json">
     <param name="includeProperties">
       ^person.name
     </param>
  </result>

这样,只会在json对象中返回属性person.name。在javascript中,您可以访问data.person.name。