首页 新闻 赞助 找找看

大家好,我遇到一个关于Struts2整合使用JSON和jQuery的问题,求帮助呀

0
悬赏园豆:80 [已解决问题] 解决于 2013-02-08 09:50

我搞了3天,也没搞定,不过,貌似找到了错误根源
但问了Google老师,他没告诉我想要的答案
所以就想来这里求助了,谢谢了

好了,这是问题描述
我希望得到的效果是:进入这个登录页面,输入用户编号,密码,提交,
后台使用Ajax进行数据验证,和传输,在登录页面输出从StudentAction中取出的数据

这是StudentAction.java

 1 package org.action;
 2 
 3 import java.util.Map;
 4 
 5 import javax.annotation.Resource;
 6 
 7 import org.apache.struts2.interceptor.SessionAware;
 8 import org.model.Student;
 9 import org.service.StudentService;
10 import org.springframework.context.annotation.Scope;
11 import org.springframework.stereotype.Component;
12 import org.util.Util;
13 
14 import com.opensymphony.xwork2.ActionSupport;
15 
16 @Component("studentAction")
17 @Scope("prototype")
18 public class StudentAction extends ActionSupport implements SessionAware
19 {
20         //静态常量JSON
21         private static final String JSON = "json";
22         //保存Session的引用
23         private Map<String, Object> session;
24         //StudentService的引用
25         private StudentService studentService;
26         //用于封装请求参数的Student对象
27         private Student student;
28         private String message;
29 
30         /**
31          * 登录方法
32          * @return
33          * @throws Exception
34          */
35         public String login() throws Exception
36         {        
37                 Util.show("%d --> %s", this.student.getId(), this.student.getPassword());
38                
39                 this.message = "你会显示吗?";
40                
41                 return JSON;
42         }
43 
44         @Override
45         public void setSession(Map<String, Object> session)
46         {
47                 // TODO Auto-generated method stub
48                 this.session = session;
49         }
50 
51         public StudentService getStudentService()
52         {
53                 return studentService;
54         }
55 
56     //错在这里
57     //注释掉@Resource就可以通过前台获得这个Action的数据
58     //不注释的话,就没有效果了,最让人痛苦是,也不报错
      //StudentService是业务逻辑层接口
59 @Resource 60 public void setStudentService(StudentService studentService) 61 { 62 this.studentService = studentService; 63 } 64 65 public Student getStudent() 66 { 67 return student; 68 } 69 70 public void setStudent(Student student) 71 { 72 this.student = student; 73 } 74 75 public Map<String, Object> getSession() 76 { 77 return session; 78 } 79 80 public String getMessage() 81 { 82 return message; 83 } 84 85 public void setMessage(String message) 86 { 87 this.message = message; 88 } 89 90 }

问题描述都写在代码里了,谢谢了

还有就是要说声抱歉,因为论坛帖子不支持文件上传

所以我把源代码分享在了360云盘上,麻烦大家帮忙下载看看

问题出哪里了,谢谢了

这是源码地址:http://l11.yunpan.cn/lk/Q2KpWNjwRVprI

Super9527的主页 Super9527 | 初学一级 | 园豆:130
提问于:2013-02-06 10:18
< >
分享
最佳答案
0

对java不懂

不过道理是相通的,我看到你用的是$.getjson(),你可以换成为$.ajax()试试看

然后借助IE9或chrom浏览器监控一下提交了些什么样的数据,再打开调试功能,看看后台接收到什么样的数据

一步一步排除

收获园豆:80
Rich.T | 老鸟四级 |园豆:3440 | 2013-02-06 10:28

后台的方法执行了,数据也可以获得,可以正常输出前台输入的数据,但是无法利用JSON获得Action的数据,我搞了好久才发现是那个业务逻辑层接口的依赖注入@Resource出的错,仅仅是注释掉@Resource,前台就可以获得从后台传过来的数据,说来也怪,我写测试代码时,依赖注入普通类就没错,到我写项目时,要依赖注入Service接口时,就没有效果了,还不报错,真心没有比这更痛苦的了,谢谢你的回答

Super9527 | 园豆:130 (初学一级) | 2013-02-06 10:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册