structs2註解+jsp+ajax實現post非同步載入select

來源:互聯網
上載者:User

標籤:非同步   ajax   jsp   select   json   

流程:

1.點擊載入btn發起非同步請求post

2.幕後處理請求返回資料

3.前端擷取資料成功,對資料進行處理


前端:

html:首先要匯入jq包,不然怎麼用ajax呢。

<script type="text/javascript" src="js/jquery.js"></script>

<select name="warId" id="warId" style="height:100%;width:107px"><option value="">所有倉庫 </option></select>

<script type="text/javascript">$(document).ready(function(){    //這裡實現的是: 點擊class為add的按鈕時載入倉庫列表$(".add").click(function(){$.post("xxxx.do",function(data){      //要非同步載入的action,和成功後的回調var objs = data.warehouses;     //對應action裡的屬性var selObj = $("#wareId");   selObj.empty();for (var i in objs) {var obj = objs[i];addOption(selObj,obj.id,obj.warehouseName); //為Select追加一個Option(下拉項)           }},"json"); //傳回型別 }); });function addOption(selObj,value,text){var optionstring = "<option value=\"" + value + "\" >"+ text + "</option>";selObj.append(optionstring); //為Select追加一個Option(下拉項)           }</script>

後台structs:

使用註解實現,

要注意的是聲明類的時候要加註解:@ParentPackage("json-default")

方法聲明註解: @Action(value = "xxxx", results = @Result(name = "success", type="json"))  

需要跳轉頁面就把type = "json" 換成  location = "/mobile/auction_detailed.jsp" 要跳轉的頁面


@ParentPackage("json-default")public class WarHouseAction extends ActionSupport {@Autowiredprivate WarehouseService warehouseService;private List<Warehouse> warehouses;/** * 後台ajax * @return * 需要跳轉頁面就把type = "json" 換成  location = "/mobile/auction_detailed.jsp" 要跳轉的頁面 */@Action(value = "addGood_before", results = @Result(name = "success", type="json"))  public String addGoodBefore(){warehouses = warehouseService.findAllEnableWarehouses();   //這個是返回到頁面的return "success";}public WarehouseService getWarehouseService() {return warehouseService;}public void setWarehouseService(WarehouseService warehouseService) {this.warehouseService = warehouseService;}public List<Warehouse> getWarehouses() {return warehouses;}public void setWarehouses(List<Warehouse> warehouses) {this.warehouses = warehouses;}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.