我有一个与ajax一起使用的Action类,它返回一个JSON对象。起初我使用了一个常数值。但我决定使用会话对象:
public class ViewFlightHistoryAction extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 7325894105057499195L;
private String viewOption;
private List<UserBooking> bookingsList;
// private Map<String, Object> sessionMap;
@Override
public String execute() throws Exception {
bookingsList = getBookingsList(viewOption);
return SUCCESS;
}
private List<UserBooking> getBookingsList(String viewOption) throws Exception {
// Map<String, Object> session = ActionContext.getContext().getSession();
Customer customer = (Customer) sessionMap.get("customer");
BookingManager bookingManager = new BookingManager();
List<UserBooking> userBookingsList = new LinkedList<UserBooking>();
String previousFlights = "Previous Flights";
String currentFlights = "Current Flights";
if (previousFlights.equalsIgnoreCase(viewOption)) {
// Replace parameter with session user's ID
userBookingsList = bookingManager.getPreviousBookingsList(customer.getCustomerID());
} else if (currentFlights.equalsIgnoreCase(viewOption)) {
userBookingsList = bookingManager.getCurrentBookingsList(customer.getCustomerID());
}
return userBookingsList;
}
public String getViewOption() {
return viewOption;
}
public void setViewOption(String viewOption) {
this.viewOption = viewOption;
}
public List<UserBooking> getBookingsList() {
return bookingsList;
}
public void setBookingsList(List<UserBooking> bookingsList) {
this.bookingsList = bookingsList;
}
@Override
public void setSession(Map<String, Object> sessionMap) {
this.sessionMap = sessionMap;
}
}
这是我对ajax的jQuery:
function viewFlightsWithAjax($viewOption) {
$.ajax({
type:"POST",
url:"viewFlightHistory",
async:true,
data: {viewOption: $viewOption},
dataType:"json",
success: function(jsonObject) {
var bookingsList = jsonObject.bookingsList;
for (var i = 0; i < bookingsList.length; i++) {
var booking = bookingsList[i];
var bookingID = booking.bookingID;
var bookingFlightDate = booking.flightDate;
bookingFlightDate = toLocaleDate(bookingFlightDate);
var bookingOrigin = booking.origin;
var bookingDestination = booking.destination;
var bookingRow = "<tr>"
+ "<td>" + bookingID + "</td>"
+ "<td>" + bookingFlightDate + "</td>"
+ "<td>" + bookingOrigin + "</td>"
+ "<td>" + bookingDestination + "</td>"
+ "<td>Receipt#</td>"
+ "<td>Itenerary Details</td>"
+ "</tr>";
$("#bookingTable tbody").append(bookingRow);
}
}
});
}
无论如何,我的问题是,当我开始实现SessionAware并使用会话对象时,它就不再起作用了。我调试它,bookingsList属性确实检索它不返回它的值。在我的struts.xml中,我排除了sessionMap和viewOption,但它似乎仍然没有返回JSON值。如何检索JSON对象?
编辑:我使用了这个拦截器:package com.bootcamp.skyjuander.system.interceptors;
import java.util.Map;
import com.bootcamp.skyjuander.service.user.beans.Customer;
import com.bootcamp.skyjuander.system.actions.ViewFlightHistoryAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class ViewFlightHistoryInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = -9003400922950846927L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ViewFlightHistoryAction action = (ViewFlightHistoryAction) invocation.getAction();
Map<String, Object> sessionMap = ActionContext.getContext().getSession();
// Customer customer
Customer sessionCustomer = (Customer) sessionMap.get("customer");
action.setCustomer(sessionCustomer);
String result = invocation.invoke();
return result;
}
}