记录:使用jquery的ajax方法请求action类的方法,方法return void,
将需要返回的数据用response.getWriter().write("")返回。
js代码:
<script type="text/javascript">
var tmp = 60;
var interval;//声明定时器
$(document).ready(function() {
$("#get_yzm").click(function() {
alert("!!");
$.ajax({
type : "post",
url : "getYZM.action",
success : function(data) {
if (data == null) {
alert("生成验证码失败...");
} else {
$("#yzm").val(data);
$("#get_yzm").attr("disabled", "disabled");
$("#get_yzm").html(tmp);
interval = setInterval(chat, "1000");//定时器,一秒触发一次
}
}
});
});
});
function chat() {
tmp--;
$("#get_yzm").html(tmp);
if (tmp == 0) {
clearTimeout(interval);
$("#get_yzm").attr("disabled", false);
$("#get_yzm").html("获取验证码");
tmp = 60;
}
}
</script>
jsp代码:
<button id="get_yzm" class="i-button-yzm" type="button" >获取验证码</button>
<s:textfield cssClass="itxt" name="yzm" id="yzm" ></s:textfield>
struct.xml中的代码:不需要result
<!-- 验证码 -->
<action name="getYZM" class="com.skyarm.gsgl.action.ViolationsQueryAction" method="getYzm">
</action>
action类中的代码:其中AbsAction继续了ActionSupport
public class ViolationsQueryAction extends AbsAction {
public static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(ViolationsQueryActi<span style="white-space: pre;"> </span>on.class);
private static final long serialVersionUID = 1L;
@Autowired
private TourService tourSErvice;
/**
* 获取验证码
*/
public void getYzm(){
HttpServletResponse response=null;
try {
HttpServletRequest request = getRequest();
HttpSession session = request.getSession();
moblie = (String) session.getAttribute("mobile");
String yzm=tourSErvice.generateYzm(moblie);
response=this.getResponse();
response.getWriter().write(yzm);
} catch (Exception e) {
logger.error(e);
e.printStackTrace();
}
}
}