Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带JS的Struts2_Javascript_Struts2 - Fatal编程技术网

Javascript 带JS的Struts2

Javascript 带JS的Struts2,javascript,struts2,Javascript,Struts2,我不熟悉Json和Struts2。我想根据1st生成第二个下拉值,但是我没有得到任何回复,即使getjson中的警报也不会出现。我需要州列表数据,以便根据国家填充州列表 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "ht

我不熟悉Json和Struts2。我想根据1st生成第二个下拉值,但是我没有得到任何回复,即使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类中没有country变量。getStateName方法应该返回字符串

  public String getStateName() throws IOException{

            // Buisness logic here

            return "success"; //hoping everything works fine :-)
        }
您的Struts.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>
检查完整的演示在这里。


我希望这一切顺利

我建议你学习S2的基础知识。看一看。Govi,谢谢你的回复。实际上,这是一个jar问题,所以检查了jar上您在该url上给出的内容。我现在添加了同一个罐子,它很好用。如果您觉得我的答案很有用,请不要费心选择它作为答案:-