get JSON data from struts

前端 未结 1 1545
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 06:09

I can get part of data from the Action function, by using the JSONArray.fromObject method, using AJAX to receive the json object. But it is very strange that t

相关标签:
1条回答
  • 2020-12-07 06:53

    Try this, will help you in Struts 2.0.14 with jsonplugin-0.32.jar.

    struts.xml:

    <struts>
         <package name="example" extends="json-default">
            <action name="HelloWorld" class="example.HelloWorld"  >
                <result type="json" />
            </action>
                  <action name="HelloWorld1" class="example.HelloWorld"  >
                <result name="success" >example/HelloWorld.jsp</result>
            </action>
        </package>
    </struts>
    

    action class Helloworld.java:

    package prabhakar;
    
    import glb.DB;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Prabhakar
     */
    public class HelloWorld  {
    
    
        private List<StateMaster> stateList= new ArrayList<StateMaster>();
        private List<RegnMaster> regnList= new ArrayList<StateMaster>();
    
        private Integer stateId;
        public Integer getStateId()
        {
        return this.stateId;
        }
        public void setStateId(Integer stateId)
        {
        this.stateId=stateId;
        }
        public List<StateMaster> getStateList() {
            return stateList;
        }
    
        public void setStateList(List<StateMaster> stateList) {
            this.stateList = stateList;
        }
         public void setRegnList(List<RegnMaster> regnList) {
            this.regnList = regnList;
        }
        public List<RegnMaster> getRegnList() {
            return regnList;
        }
    
        public String execute() throws Exception {
    
            stateList=DB.getStateData()//
            if(stateId !=null)
              {
             regnList=DB.getRegnByStateId(stateId);
              }
    
            //setMessage(getText(MESSAGE));
            return "success";
        }
    
        /**
         * Provide default valuie for Message property.
         */
    
    }
    

    You can directly call HelloWorld.action to view the JSON data or else you can bind the JSON data to a form element below.

    JSP page HelloWorld.jsp:

      /*
         Prabhakar
      */
    
    <%@ page contentType="text/html; charset=UTF-8" %>
    
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <script>
    <%@include file="../js/jquery-1.7.1.min.js"%>
    </script>
        <html>
    
    <!-- JavaScript Plugins -->
      <script>
           function getLoad(){
    
    
           var stateId = $('#state').val();
    
    $.getJSON('HelloWorld.action', {'stateId': stateId},
        function(data) {
    
               var divisionList = (data.regnList);
    
                    var options = $("#regn");
                    options.find('option')
        .remove()
        .end();
         options.append($("<option />").val("-1").text("--Select--"));
    $.each(divisionList, function() {
    
        options.append($("<option />").val(this.regnId).text(this.regnName));
    });
        }
    );}
       </script>
    
    <!-- jQuery-UI Dependent Scripts -->
    
        <body>
            State List <s:select name="stateId" list="stateList" id="state" listKey="stateId" onchange="getLoad()" listValue="stateName" headerKey="0" headerValue="--select--" />
            Regn List <s:select name="regnId"  list="regnList" listKey="regnId" id="regn" listValue="regnName" headerKey="0" headerValue="--select--" />
        </body>
    </html>
    

    Happy coding :)

    0 讨论(0)
提交回复
热议问题