實現前台與後台即時通訊。
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 這個包了。。。。。