带有 JS的支柱2

Struts2 with JS

本文关键字:JS 带有      更新时间:2023-09-26

我是Json和Struts2的新手。我想根据第一个下拉值制作第二个下拉值,但即使 getjson 中的警报也没有出现,我也没有得到任何回报。我需要州列表数据,以便我可以根据国家/地区填充州列表

    <?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>
            <constant name="struts.devMode" value="true" />
            <package name="basicstruts2" namespace="/" extends="struts-default">    
                <action name="changeStateName" class="ActionController.changeStateName" method="getStateName">
                </action>
            </package>
        </struts>

JSP 页面

        <script type="text/javascript">
          $(document).ready(function(){
            $("#countrylist").change(function changeState(){
                var country=$( "#countrylist option:selected" ).text();
                alert("inside function"+country);
                $.getJSON('changeStateName.action?country='+country,
                        function(data){   
alert("inside getjson"+country);
                     var options = $("#StateList");
                     options.find('option')
                     .remove()
                     .end();
                     options.append($("<option />").val("-1").text("--Select State--"));
                    $.each(divisionList, function() {
                         options.append($("<option />").val(this).text(this));
                     }); 
                });
                 });
        </script>
        </head>
        <body>
        <h1>Welcome file <s:property value="userName"/></h1>
        <table cellpadding="5" cellspacing="5">
         <tbody>
           <tr><td><s:select id="countrylist" list="cuntry" key="CountryName" ></s:select></td></tr>
           <tr><td><s:select id="StateList" list="state" key="StateName" headerKey="0" headerValue="--select--"></s:select></td></tr>
           <tr><td><s:submit></s:submit></td></tr>
         </tbody>
        </table>
        </body>
        </html>

操作类

        public class changeStateName {
        private HashMap<String,String> stateList=new HashMap<String,String>();
            public HashMap<String, String> getStateList() {
                return stateList;
            }
            public void setStateList(HashMap<String, String> stateList) {
                this.stateList = stateList;
            }
        public HashMap<String, String> getStateName() throws IOException{
                System.out.println("inside changeStateName"+country);
                HashMap<String,String> sl=new HashMap<String, String>();
                sl.put("1", "kar");
                sl.put("2", "Del");
                sl.put("3", "Har");
                setStateList(sl);
                return stateList;
            }
        }

据我所知,你在changeStateName类中没有国家变量.你的getStateName()方法应该返回字符串。

  public String getStateName() throws IOException{
            // Buisness logic here
            return "success"; //hoping everything works fine :-)
        }

你的支柱.xml文件非常凌乱,对于 JSON,您必须扩展 JSON 默认值。

<?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>
        <constant name="struts.devMode" value="true" />
        <package name="basicstruts2" namespace="/" extends="json-default">    
            <action name="changeStateName" class="ActionController.changeStateName" method="getStateName">
                 <result name="success" type="json" />
            </action>
        </package>
    </struts>

在此处查看完整的演示。http://www.simplecodestuffs.com/ajax-implementation-in-struts-2-using-jquery-and-json/

我希望这工作正常!!