Ajax 与 Struts 1
阅读原文时间:2023年07月08日阅读:2

Xml配置

tiles

result.jsp

<%@page contentType="text/html;charset=utf-8"%>${result}

ajax:

function validateCurrencyDecimalSupport(amountType, amountPrice)

{ var retval = "supported"; $.ajax({ type: "POST", url: "validateCurrencyDecimalSupport.do", timeout: 2000, async: false, data: { amountType: amountType, amountPrice: amountPrice }, success: function(data) { //show content retval = data; }, error: function(jqXHR, textStatus, err) { //show error message alert('text status '+textStatus+', err '+err) } }); return retval; }

AdvancedAction Action:

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
String result = SUCCESS;
String param = mapping.getParameter();
Map args = new HashMap();
DynaActionForm dyForm = (DynaActionForm) form;

if ("validateCurrencyDecimalSupport".equalsIgnoreCase(param))
{
validateCurrencyDecimalSupport(request);
}

return redirect(mapping, result, args);

}

private void validateCurrencyDecimalSupport(HttpServletRequest request)
{
String amount = new BigDecimal(request.getParameter("amountPrice")).stripTrailingZeros().toString();
Integer decimalSupport = BaseServiceFactory.getCurrencyService().getCurrencyType(request.getParameter("amountType").trim()).getDecimalSupport();
if (amount.indexOf(".") > 0 && (amount.split("\\.")[1].length() > decimalSupport.intValue()))
request.setAttribute("result", "The decimal support of currency is : " + decimalSupport.intValue() + ", please input a valid number");
else
request.setAttribute("result", "supported");
}