通过jquery实现ajax调用

要素:struts1

一、

1、test.jsp

<head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
 <script type="text/javascript" src="<%=request.getContextPath()%>/jquery/DLC.js"></script>
 </head>

 

 

<html:form>

   <td width="150" align="left">

      <html:select property="makeName" styleId="makeName"  οnchange="getYear('yearName','${pageContext.request.contextPath}/website/index.do?method=getYear&makeName='+this[this.selectedIndex].value)">
            <html:option value="Select..."/>
            <html:options property="makeName" labelProperty="makeName" collection="makeList"/>
           </html:select>

   </td>

 

 

<td width="150" align="left">
            <html:select property="yearName" οnchange="getModel()" styleId="yearName">
             <html:option value="Select..."/>
             <html:options labelProperty="yearName" property="yearName" collection="yearList"/>            
            </html:select>
           </td>

</html:form>

 

 

2、DLC.js

function getYear(makeName,url){
 $.ajax({
  type:'post',
  url:url,
  success: function(html){$('#'+makeName).html(html);}
  });
}

 

3、Action

 

public ActionForward getYears(ActionMapping mapping,ActionForm form,HttpServletRequest request,
   HttpServletResponse response){
 
  String makeName = request.getParameter("makeName");
    
  List<String> years = idlcs.getYears(makeName,lanId);
  request.setAttribute("years", years);
  return mapping.findForward("getYears");
 }

 

4、getYears.jsp(以页面的方式返回结果)

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
 <%
 List<String> list = (List)request.getAttribute("years");
 for(String tm :list){
 %>
 <option value="<%=tm%>"><%=tm%></option>
 <%
 }
 %>

 

 改写3、4步,直接返回结果,不写jsp页面了

 

 public ActionForward getYears(ActionMapping mapping,ActionForm form,HttpServletRequest request,
   HttpServletResponse response){
 
  String makeName = request.getParameter("makeName");
  List<String> years = idlcs.getYears(makeName,lanId);
    
  response.setContentType("text/html");
  response.setHeader("Cache-Control", "no-cache");
  response.setCharacterEncoding("UTF-8");
  
  PrintWriter out = null;
  try {
   out = response.getWriter();
  
  for(String year:years){
   out.println("<option>"+year+"</option>");
  }
  out.close();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if(out!=null){
    out.close();
   }
  }
  return null;

 

 

 

二、<html:select>标签属性onchange里不支持<%=request.getContextPath()%>,支持EL表达式

struts-html.tld:

<tag>
<name>select</name>

<attribute>

...
<name>onchange</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

...

</tag>

 

 

<#

Element : rtexprvalue
Defines if the nesting attribute can have scriptlet expressions as a value, i.e the value
 of the attribute may be dynamically calculated at request time, as opposed to a static
 value determined at translation time. #PCDATA ::= true | false | yes | no If not present
 then the default is "false", i.e the attribute has a static value

Data Type : #PCDATA

#>

 

 

**************************************************************************

实例:

1、在javascript中

var url1 = "<%=request.getContextPath()%>/admin/manageChangeRecord.do?method=checkSameVenderBySerialno";
   var oldCode = changeRecordInfoForm.oldCode.value;
   var newCode = changeRecordInfoForm.newCode.value;

$.ajax({
    url:url1,
     cache: false,
          dataType:"text",
          data:{code1:oldCode,code2:newCode},
          success: function(ret){//ret为服务器返回值
                     if(ret=="false"){
                      alert("换机的产品属于不同的分公司,不能换机");
                     }else{
                     document.forms[0].submit();
      document.getElementById("subid").disabled="disabled";
                     }
                 }
   });

 

服务器端:

public ActionForward checkSameVenderBySerialno(ActionMapping actionMapping,
   ActionForm actionForm, HttpServletRequest request,
   HttpServletResponse response) throws X431WebException {

 long oldVenderId = ...

 long newVenderId = ...
  ......

 

  //flag 为true时,表示换机的是同一个分公司的产品
  boolean flag = oldVenderId==newVenderId?true:false;
  try {
   PrintWriter pw = response.getWriter();
   pw.print(flag);
   pw.flush();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值