Populating dropdown from database via struts2 action class (using jtable)

一个人想着一个人 提交于 2021-01-28 12:40:33

问题


I am using jtable http://jtable.org/ in my project. jTable is a jQuery plugin that is used to create AJAX based CRUD tables without coding HTML or Javascript.

http://i62.tinypic.com/3461eo3.jpg

jtable code for above form is

HTML code

<div id="StudentTableContainer"></div> 

Javascript code

<script type="text/javascript">  



    $(document).ready(function () {  



        $('#StudentTableContainer').jtable({  

            title: 'Student List',  

            paging: true,  

            pageSize: 10,  

            sorting: true,  

            defaultSorting: 'Name ASC',  

            actions: {  

                listAction: '/Demo/StudentList',  

                deleteAction: '/Demo/DeleteStudent',  

                updateAction: '/Demo/UpdateStudent',  

                createAction: '/Demo/CreateStudent'  

            },  

            fields: {  

                StudentId: {  

                    key: true,  

                    create: false,  

                    edit: false,  

                    list: false  

                },  

                Name: {  

                    title: 'Name',  

                    width: '30%'  

                },  

                EmailAddress: {  

                    title: 'Email address',  

                    list: false  

                },  

                Password: {  

                    title: 'User Password',  

                    type: 'password',  

                    list: false  

                },  

                Gender: {  

                    title: 'Gender',  

                    options: { 'M': 'Male', 'F': 'Female' },  

                    list: false  

                },  

                ContinentalId: {  

                    title: 'Continental',  

                    options: '/Demo/GetContinentalOptions',  

                    list: false  

                },  

                CountryId: {  

                    title: 'Country',  

                    dependsOn: 'ContinentalId', //Countries depends on continentals. Thus, jTable builds cascade dropdowns!  

                    options: function (data) {  

                        if (data.source == 'list') {  

                            //Return url of all countries for optimization.   

                            //This method is called for each row on the table and jTable caches options based on this url.  

                            return '/Demo/GetCountryOptions?continentalId=0';  

                        }  



                        //This code runs when user opens edit/create form or changes continental combobox on an edit/create form.  

                        //data.source == 'edit' || data.source == 'create'  

                        return '/Demo/GetCountryOptions?continentalId=' + data.dependedValues.ContinentalId;  

                    },  

                    list: false  

                },  

                CityId: {  

                    title: 'City',  

                    width: '30%',  

                    dependsOn: 'CountryId', //Cities depends on countries. Thus, jTable builds cascade dropdowns!  

                    options: function (data) {  

                        if (data.source == 'list') {  

                            //Return url of all cities for optimization.   

                            //This method is called for each row on the table and jTable caches options based on this url.  

                            return '/Demo/GetCityOptions?countryId=0';  

                        }  



                        //This code runs when user opens edit/create form or changes country combobox on an edit/create form.  

                        //data.source == 'edit' || data.source == 'create'  

                        return '/Demo/GetCityOptions?countryId=' + data.dependedValues.CountryId;  

                    }  

                },  

                BirthDate: {  

                    title: 'Birth date',  

                    type: 'date',  

                    displayFormat: 'yy-mm-dd',  

                    list: false  

                },  

                Education: {  

                    title: 'Education',  

                    list: false,  

                    type: 'radiobutton',  

                    options: [  

                        { Value: '1', DisplayText: 'Primary school' },  

                        { Value: '2', DisplayText: 'High school' },  

                        { Value: '3', DisplayText: 'University' }  

                    ]  

                },  

                About: {  

                    title: 'About this person',  

                    type: 'textarea',  

                    list: false  

                },  

                IsActive: {  

                    title: 'Status',  

                    width: '15%',  

                    type: 'checkbox',  

                    values: { 'false': 'Passive', 'true': 'Active' },  

                    defaultValue: 'true'  

                },  

                RecordDate: {  

                    title: 'Record date',  

                    width: '25%',  

                    type: 'date',  

                    displayFormat: 'yy-mm-dd',  

                    create: false,  

                    edit: false,  

                    sorting: false //This column is not sortable!  

                }  

            }  

        });  



        //Load student list from server  

        $('#StudentTableContainer').jtable('load');  

    });  



</script>  

I want to have a Dropdown in my project which should take values from database through struts2 action class . Like in the above demo code, list of continents can be accessed from database via struts2 action class (using URL pattern /Demo/GetContinentalOptions

As jtable only understands json so please guide me what should I write in Struts2 Action class and Struts.xml

Note: In your sample code you can even hardcode dropdown values


回答1:


You can populate your json field with the following action. You also need a convention plugin to use annotations. To use json result you need a json plugin.

@Action(value="GetContinentalOptions", results=@Result(type="json", params = {"root", "map"}))
public class ContinentalOptionsAction extends ActionSupport {    
  Map<String, String> map=new HashMap<>();

  public Map<String, String> getMap() {
    return map;
  }

   @Override
   public String execute() throws Exception {
      map.put("1", "Asia");
      map.put("2", "America");
      map.put("3", "Europe");
      map.put("4", "Africa");
      return SUCCESS;
   }
}

In the options function

var options = []; 
$.ajax({ //get from server
    url: '<s:url action="GetContinentalOptions"/>',
    dataType: 'json',
    success: function (data) {
        options = data;
    }
});
return options;

EDIT:

Without convention plugin you should write action configuration in struts.xml

<action name="GetContinentalOptions" class="com.action.ContinentalOptionsAction">
  <result type="json">
    <param name="root" value="map"/> 
  </result>
</action>


来源:https://stackoverflow.com/questions/24506509/populating-dropdown-from-database-via-struts2-action-class-using-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!