日期:2014-05-16  浏览次数:20785 次

通过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 "fals