標籤:非同步 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;}}