实现前台与后台即时通信。
1. 最好是把$.ajax 中的url中的action由参数传过去。
前台 onload=" load('$(cardAction)')";
<s:url action="cardAction" namespace="/examjson" includeParams="none" id="cardAction"/>
JScript
function load(_url){
$.ajax({
type: "POST",
url: _url,
data: "name="+name,
error:function(){alert("error")},
success: function(msg){
eval("res = "+msg);
var cards = res.cards;
for(var i = 0;i<cards.length;i++){
$("#cardSelect").append("<option value="+cards[i]+">"+cards[i]+"</option>")
}
}
});
}
2. 配制struts.xml
<package name="jsonpack" extends="json-default" namespace="/examjson">
<!-- 得到card -->
<action name="cardAction" class="cardAction" method="Cards">
<result type="json">
<param name="excludeProperties">employeeService</param>
</result>
</action>
</package>
注意:employeeService为 cardAction中需要注入的对象。
3,applicationContext.xml
<bean id="addEmployeeAction" class="com.ambow.ExamOnlineSystem.entity.exam.action.AddEmployeeAction">
<property >
<ref bean="employee"/>
</property>
</bean>
java
package com.ambow.ExamOnlineSystem.entity.exam.action;
import com.ambow.ExamOnlineSystem.entity.exam.interfaces.IEmployee;
import com.opensymphony.xwork2.ActionSupport;
public class CardAction extends ActionSupport {
private static final long serialVersionUID = 163236660018615361L;
private String name;
private IEmployee employeeService;
String[] cards;
public String[] getCards() {
return cards;
}
public void setCards(String[] cards) {
this.cards = cards;
}
public void setEmployeeService(IEmployee employeeService) {
this.employeeService = employeeService;
}
public void setName(String name) {
this.name = name;
}
public String Cards(){
String[] str=employeeService.getCards(name);
cards = new String[str.length];
cards = str;
return ActionSupport.SUCCESS;
}
}
看上面cards为可以得到 的数据.得到方法在js文件的load() 方法中.
葡萄你个哈蜜瓜,差点忘了加jsonplugin-0.30.jar 这个包了。。。。。