需要通过使用struts动作通过ajex调用来选择select2下拉列表的国家名称列表,但遇到问题。请帮助我

我需要帮助,使用struts2动作类将国家/地区名称从json文件获取到select2下拉列表。

我为前端编写了一个jsp文件,struts.xml,web.xml和struts操作类。通过select2调用动作类时出现错误。

当我单击select2下拉列表时,ajax调用了action方法以在列表中显示国家名称。但是什么都没显示

index.jsp

 <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="x-ua-compatible" content="IE=edge" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<body>

<s:form action="fetchCountries">
    <select class="myselect" name="countriesL.code" style="width: 500px;">
    <option value="0">Select</option>
    </select>
</s:form>

<script type="text/javascript">     
    $(document).ready(function () {
        $('.myselect').select2({
            placeholder: 'Select an item',ajax: {
            url: 'fetchCountries',dataType: 'json',data: function (params) 
            {
                var query = {
                search: params.term,//this is Sending Input key by search
                userInput: params.term  //this is Sending Input key by userInput
            }
            return query;
            },processResults: function (data) 
        { 
            //Return Response
            console.log(data);
            var results = $.map(data.countriesL,function (obj)
        {

        return {
        id: obj.id,//text: obj.text
            };
        });
        console.log(results);   //Printing Response
        return {
        results: results,};
        },"more": true,cache: true
        }
        });
        });
    </script type>
</body>
</html>


struts.xml

<!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="default"  namespace="" extends="struts-default" >    

        <action name="Getountries"
                class="com.getjsondata.Getountries" >
         <result>/index.jsp</result>        
        </action>   

        <action name="fetchCountries" class="com.getjsondata.Getountries" method="findcountry">
        <result name="success"></result>
        </action>               
    </package>

</struts>

struts动作类

package com.getjsondata;

import com.opensymphony.xwork2.actionSupport;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator; 
import java.util.Map; 

import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.*;

public class Getountries extends actionSupport
{

    String userInput;
    ArrayList<String> countriesL;



    public String getUserInput() {
        return userInput;
    }



    public void setUserInput(String userInput) {
        this.userInput = userInput;
    }



    public ArrayList<String> getcountriesL() {
        return countriesL;
    }



    public void setCountriesL(ArrayList<String> countriesL) {
        this.countriesL = countriesL;
    }


        public String findcountry() throws Exception {

        countries cl = new countries();
        //countriesL=cl.getIdList(userInput);
        countriesL=cl.getcountries(userInput);

        return "success";
  }


}

countries.java

package com.getjsondata;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class countries 
{
    public String countryName;
    ArrayList<String> idList = new ArrayList<String>();

    public ArrayList<String> getIdList(String userInput) {
        return idList;
    }

    public void setIdList(ArrayList<String> idList) {
        this.idList = idList;
    }

    public String getcountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }



    public ArrayList<String> getcountries(String userInput) throws Exception
    {
    try{
        System.out.println("executing execut() method");
        String path = "C:/Workspace2/Select2Json/resources/countries.json";
        JSONParser parser = new JSONParser();
        JSONArray a = (JSONArray) parser.parse(new FileReader(path));

        for (Object o : a)
          {
            JSONObject person = (JSONObject) o;

            String id = (String) person.get("text");
            idList.add(id);
            System.out.println(id);  

          }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return idList;
    }   
}

示例json文件

[
  {
    "id": "AX","text": "Ă…land Islands"
  },{
    "id": "PK","text": "Pakistan"
  },{
    "id": "SE","text": "Sweden"
  },{
    "id": "BV","text": "Bouvet Island"
  }
]
runnerfly 回答:需要通过使用struts动作通过ajex调用来选择select2下拉列表的国家名称列表,但遇到问题。请帮助我

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3139012.html

大家都在问