ajax 结合struts实现跨域访问

前端之家收集整理的这篇文章主要介绍了ajax 结合struts实现跨域访问前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.首先ajax的访问方法如下:

  1. $.getJSON("requestUrl?callback=?",function(data){
  2. });


2.struts中action 的写法如下:
  1. public String getBirthdayUser() {
  2. List<User> list = userService.listAll(1,100);
  3. Calendar cal = Calendar.getInstance();
  4. int month = cal.get(Calendar.MONTH) + 1;
  5. int day = cal.get(Calendar.DAY_OF_MONTH);
  6. List<User> brithdayUser = new ArrayList<User>();
  7. List<String> birthdayUserName = new ArrayList<String>();
  8. for (User user : list) {
  9. if (user.getIdCard() != null && user.getIdCard().length() == 18) {
  10. if (Integer.valueOf(user.getIdCard().substring(10,12)) == month
  11. && Integer.valueOf(user.getIdCard().substring(12,14)) == day) {
  12. brithdayUser.add(user);
  13. birthdayUserName.add(user.getUserName());
  14. }
  15. }
  16. }
  17. HttpServletResponse response = ServletActionContext.getResponse();
  18. response.setCharacterEncoding("utf-8");
  19. userJson.put("birthdayUser",birthdayUserName);
  20. return "getBirthdayUserSuccess";
  21.  
  22. }

3.struts的配置文件如下:
  1. <result name="getBirthdayUserSuccess" type="json">
  2. <param name="root">userJson</param>
  3. <param name="callbackParameter">callback</param>
  4. </result>


至此 ajax结合struts 实现跨域访问结束

猜你在找的Ajax相关文章