`
newleague
  • 浏览: 1475264 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

通过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"  onchange="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" onchange="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;
 }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics