Struts2 + JQuery + Freemarker的ajax调用
2013-06-17 15:21
375 查看
Struts2 当中使用json-lib-jdk5.jar而没有使用struts-json-plugin.jar来实现。JQuery $.post()实现ajax调用。freemarker作为视图模板。
前页模版 invoiceform.ftl:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CH" xml:lang="zh-CH">
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Cache-Control" content="no-store"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<meta http-equiv="Pragma" content="no-cache, must-revalidate, no-store"/>
<title>struts2+jquery_ajax</title>
<script type="text/javascript" src="${request.contextPath}/js/json/json.js"></script>
<script type="text/javascript" src="${request.contextPath}/js/invoice.js"></script>
</head>
<body scroll="auto">
<form name="invoiceForm" action="" method="post" >
<fieldset >
<legend>选择商品:</legend>
<br/>
<#if products? exists>
商品:
<select id="product.id" name="product.id">
<#list products as product>
<option value="${(product.id)? if_exists}">${(product.name)? if_exists} </option>
</#list>
</select>
</#if>
<br/>
<span class="spanProduct">
单价:<input type="text" readonly="readonly" name="product.price">
</span>
<span class="spanProduct">
数量:<input type="text" name="item.quantity" oninput="calTotal(); return false;">
</span>
<span class="spanProduct">
消费:<input type="text" readonly="readonly" name="item.cost">
</span>
</fieldset>
</form>
</body>
</html>
JQuery脚本 invoice.js:
$(function() {
$("select[id='product.id']").change(function(){
//alert($(this).val());
//清空数量和消费
$("input[name='item.quantity']").val("");
$("input[name='item.cost']").val("");
//当商品选择下拉框change时,显示所选商品价格
$.post(
"invoiceAction!listProducts.shtml", //调用Action方法
{"product.id" : $(this).val()}, //传递参数,下拉框选择的商品id
function(data){
//alert(data.price);
$("input[name='product.price']").val(data.price);
},
"json"
);
});
}
action类 invoiceAction:
public class InvoiceAction extends ActionSupport{
private Product product;
/**..Getters & Setters.*/
//Action中的执行方法,若使用json-lib-jdk5.jar则返回为void
public void listProducts() {
try {
JSONObject result = new JSONObject();
product = productService.load(getProduct().getId());
Product _result = new Product();
BeanUtils.copyProperties(_result, product);
//Product类当中存在private Set<Item> items属性,即onetomany的关系,将_result转化为json对象前将items设为null.
_result.setItems(null);
result = JSONObject.fromObject(_result);
//HttpResponse返回json对象
getWebResponse().getWriter().print(result);
} catch (Exception e) {
setErrMessage(e.getMessage());
}
}
}
struts.xml配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="simpledemo" extends="struts-default">
<!--ajax返回json在method当中实现,不用再配置-->
<action name="invoiceAction!*" class="com.scott.action.InvoiceAction" method="{1}">
<result name="input" type="freemarker">/invoiceform.ftl</result>
<result name="detail" type="freemarker">/invoiceItem.ftl</result>
</action>
</package>
前页模版 invoiceform.ftl:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CH" xml:lang="zh-CH">
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Cache-Control" content="no-store"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<meta http-equiv="Pragma" content="no-cache, must-revalidate, no-store"/>
<title>struts2+jquery_ajax</title>
<script type="text/javascript" src="${request.contextPath}/js/json/json.js"></script>
<script type="text/javascript" src="${request.contextPath}/js/invoice.js"></script>
</head>
<body scroll="auto">
<form name="invoiceForm" action="" method="post" >
<fieldset >
<legend>选择商品:</legend>
<br/>
<#if products? exists>
商品:
<select id="product.id" name="product.id">
<#list products as product>
<option value="${(product.id)? if_exists}">${(product.name)? if_exists} </option>
</#list>
</select>
</#if>
<br/>
<span class="spanProduct">
单价:<input type="text" readonly="readonly" name="product.price">
</span>
<span class="spanProduct">
数量:<input type="text" name="item.quantity" oninput="calTotal(); return false;">
</span>
<span class="spanProduct">
消费:<input type="text" readonly="readonly" name="item.cost">
</span>
</fieldset>
</form>
</body>
</html>
JQuery脚本 invoice.js:
$(function() {
$("select[id='product.id']").change(function(){
//alert($(this).val());
//清空数量和消费
$("input[name='item.quantity']").val("");
$("input[name='item.cost']").val("");
//当商品选择下拉框change时,显示所选商品价格
$.post(
"invoiceAction!listProducts.shtml", //调用Action方法
{"product.id" : $(this).val()}, //传递参数,下拉框选择的商品id
function(data){
//alert(data.price);
$("input[name='product.price']").val(data.price);
},
"json"
);
});
}
action类 invoiceAction:
public class InvoiceAction extends ActionSupport{
private Product product;
/**..Getters & Setters.*/
//Action中的执行方法,若使用json-lib-jdk5.jar则返回为void
public void listProducts() {
try {
JSONObject result = new JSONObject();
product = productService.load(getProduct().getId());
Product _result = new Product();
BeanUtils.copyProperties(_result, product);
//Product类当中存在private Set<Item> items属性,即onetomany的关系,将_result转化为json对象前将items设为null.
_result.setItems(null);
result = JSONObject.fromObject(_result);
//HttpResponse返回json对象
getWebResponse().getWriter().print(result);
} catch (Exception e) {
setErrMessage(e.getMessage());
}
}
}
struts.xml配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="simpledemo" extends="struts-default">
<!--ajax返回json在method当中实现,不用再配置-->
<action name="invoiceAction!*" class="com.scott.action.InvoiceAction" method="{1}">
<result name="input" type="freemarker">/invoiceform.ftl</result>
<result name="detail" type="freemarker">/invoiceItem.ftl</result>
</action>
</package>
相关文章推荐
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及非文件上传的部分
- Jquery $.ajaxSubmit()调用Struts2的Action示例
- jQuery纯手写前台,动态分页,调用ajax,与后台struts2动态交互!
- 利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法
- jQuery调用AJAX时Get和post公用的乱码解决方法实例说明
- .NET 下的 jQuery UI 开源控件 - JQueryElement, 简化 js 脚本编写, 提供更方便的 ajax 调用[1]
- 初次体验Jquery(Ajax + Json)+Struts2
- PHP中运用jQuery的Ajax跨域调用实现代码
- jquery的ajax跨域调用json
- jQuery调用Asp.net Ajax页面中的方法
- jquery中通过ajax调用webservice传递数组参数的问题
- Dwz/Jquery--使用Ajax提交表单时调用表单设置的校验
- JQuery调用Web service实现AJAX实现教程
- jquery 封装的ajax调用
- jQuery通过ajax调用ASP.NET函数
- JSP+jquery使用ajax方式调用json的实现方法
- JQUERY浮动窗口、toast提示、ajax调用
- Jquery_Ajax 注册验证(Struts2)<学习随笔>
- JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据