最简单的jquery ajax在sturts中的应用
action:
public ActionForward ajaxTest() throws Exception { try { String val = getRequest().getParameter("val"); val = "回应:" + val; getResponse().getOutputStream().print(val); // 放入response } catch (Exception e) { throw e; } return null; // 这里返回null }
jsp:
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <html> <head> <title>ajax测试</title> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <link href="<%=request.getContextPath()%>/css/style.css" rel="stylesheet" type="text/css"> <script src="<%=request.getContextPath()%>/js/jquery-1.3.2.js" type="text/javascript"></script> <script src="<%=request.getContextPath()%>/js/ajaxTest.js" type="text/javascript"></script> <script type="text/javascript"> </script> </head> <body> <form action="demo.do" method="post" id="demoForm"> ajax测试 <input type="text" id="val"/> <input type="text" id="result"/> <input type="button" id="btn" value="ajax"/> </form> </body> </html>
js:
$(document).ready(function() { $('#btn').bind('click', function() { $.ajax({ url: 'demo.do?nextStep=ajaxTest', data: {'val': $('#val').val()}, success : function(result) { $('#result').val(result); } }); }); });
最后: