通过struts2操作类(使用jtable)从数据库填充下拉列表

Populating dropdown from database via Struts 2 action class (using jtable)

本文关键字:数据库 填充 下拉列表 jtable 使用 struts2 操作 通过      更新时间:2023-09-26

我在我的项目中使用jtable http://jtable.org/。jTable是一个jQuery插件,用于创建基于AJAX的CRUD表,而无需编码HTML或Javascript。

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

以上格式的jtable代码为

HTML代码:

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

JavaScript代码:

<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>  

我想在我的项目中有一个下拉菜单,应该通过struts2动作类从数据库中获取值。与上面的演示代码一样,可以通过struts2操作类(使用URL模式/Demo/GetContinentalOptions

)从数据库访问大洲列表。

由于jtable只理解json,所以请指导我在Struts2 Action类和Struts.xml中应该写什么

注意:在示例代码中,您甚至可以硬编码下拉值

您可以使用以下操作填充json字段。您还需要一个约定插件来使用注释。要使用json result,你需要一个json插件。

@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;
   }
}

options函数中

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

如果没有约定插件,您应该在struts.xml中编写操作配置

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