Ajax 与 Struts 1
Xml配置
<action path="/product/product/validateCurrencyDecimalSupport"
type="com.neulion.iptv.ui.struts.AdvancedAction"
parameter="validateCurrencyDecimalSupport" name="AdvancedPriceForm" validate="false" scope="request">
<forward name="success" path=".result"></forward>
</action>
tiles
<definition name=".result" path="/WEB-INF/jsp/result.jsp"/>
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");
}