Struts2 - 使用jquery ajax从数据库加载信息
用于开发我的webApp我使用 struts 2 hibernate 3
,并且当我更改中的值时我想运行一个查询< s:select />
标记并将此查询的结果传递给隐藏的输入
,并且当选择标记的元素已更改时,我希望保留在同一页面中
for developing my webApp I m using struts 2 hibernate 3
and I want to run a query when I change the values in <s:select/>
tag and pass the result of this query in a hidden input
and i want to stay in the same page when changed element of select tag
我的页面jsp loot就像这样:
my page jsp loot like this example :
<form name="evalform" action="saveOrUpdateSousEval" method="post" >
<s:iterator begin="1" end="4" status="status">
<s:hidden name="SousEval_Note"
value="99"
placeholder="entrer Note"
/>
<s:select
headerValue="---------------- Select ---------------"
headerKey="-1"
list="SousItemsListGrille"
listKey="SousItem_ID"
listValue="SousItem_Libelle"
name="sousEvalItem.SousItem_ID"
cssClass="selectedId"
/>
</s:iterator>
</form>
这里我已经开始获取所选元素的ID:
here I have start by getting id of element selected :
<script type="text/javascript">
$("#idselectdiv .selectedId").change(function () {
var idd = $(this).val();
//each element selected in each select tag has an id and i want excute query of this id
alert(" id selected "+idd);
$.ajax({
//somthing here !!
});
});
</script>
在struts中。 xmli定义此动作:
in struts. xmli define this action :
<action name="ponderation" method="getItembyPonderation" class="action.classAction">
<result name="success" >/oki.jsp</result>
</action>
在我的课程中我有这个方法:
in my class Action i have this methode :
public Double getItembyPonderation(){
System.out.print("enter getItembyPonderation ok");
Double b = null;
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
List<Double> a=selectponder.selectponderation(Long.parseLong(request.getParameter("SousItem_ID")));
while (a != null) {
return b=a.get(0);
}
return b;
}
在我的课程中:
public List<Double> selectponderation(Long idsousitem){
List<Double> valponderation = null;
try {
valponderation = session.createQuery("SELECT a.ponderation FROM items a, sousitems b WHERE a.Item_ID = b.Item_ID and b.SousItem_ID="+idsousitem).list(); ;
} catch (Exception e) {e.printStackTrace();}
return valponderation;
}
这里我需要这样做
here i needan idea to do that
<script type="text/javascript">
$("#idselectdiv .selectedId").change(function () {
var idd = $(this).val();
//each element selected in each select tag has an id and i want excute query of this id
alert(" id selected "+idd);
$.ajax({
url : 'your_action name_where_you_execute_query',
data:{
idd:idd
},
type : 'GET',
dataType : 'as_you_want_from_your_action'
});
});
</script>
以上是如何调用动作并将参数传递给该动作的示例。
你知道吗?
Above is example of how you can calll an action and pass your parameter to that action. Do you get it?