+ -
当前位置:首页 → 问答吧 → Json 集成S2SH 问题 json无法调用到 action里面 由spring注入的类的方法

Json 集成S2SH 问题 json无法调用到 action里面 由spring注入的类的方法

时间:2011-01-11

来源:互联网

js代码:
$(document).ready(function(){
  $("#username").blur(function(){
  var param = {
  username: $("#username").val()
  }  
  $.post("registerJsonAction.action", param, function checkName(data){
  alert(data);
  if (data == 'exist') {
  $("#username_error").html("<font color ='red'>该用户名已存在!</font>");
  }
  else {
  $("#username_error").html("<font color ='green'>恭喜该用户名可以注册!</font>");
  }
  }, "json");
  });
});

struts2 action 代码:

@Component("register")
@Scope("prototype")
public class RegisterJsonAction extends ActionSupport {
private static final long serialVersionUID = 7044325217725864312L;
private String username;
private String tip;
private UserService userService;

/*@JSON(serialize = false)
public UserService getUserService() {
return userService;
}*/

@Resource(name = "userService")
public void setUserService(UserService userService) {
this.userService = userService;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getTip() {
return tip;
}

public void setTip(String tip) {
this.tip = tip;
}

public String execute() throws Exception {
if (userService.checkByName(getUsername())) {
tip = "exist";
} else {
tip = "not_exist";
}
return SUCCESS;
}
}

我已经 试过 用Junit4 测试过方法 是可以的 json+struts2+hibernate 也是可以 但是集成了spring2.5 就不行了 

希望高手帮帮忙为什么 提示 是 500错误 说我的“userService.checkByName(getUsername())” 方法null 返回空指针异常 ! 谢谢大家

作者: Jokerfly0209   发布时间: 2011-01-11

CSS code

用接口
DriveModel
实现里面的方法 
看看能不能获取到UserService 类


作者: leehuat   发布时间: 2011-01-11

你的struts2的对象要交给spring来创建完成,你这两个框架的整合有做了吗?

作者: licip   发布时间: 2011-01-11

一看就是没整合

作者: fangtao8798   发布时间: 2011-01-11

引用 2 楼 licip 的回复:

你的struts2的对象要交给spring来创建完成,你这两个框架的整合有做了吗?

我整合了啊 struts 不用交给spring吧 我在web 里面 设置了 listener 和 spring的 xml路径了

作者: Jokerfly0209   发布时间: 2011-01-11

我单独在 一个测试类里面 试过 ssh 有没集成 测试返回的结果是我所要的结果 应该是没问题的 就是 在struts
的 action里面 那个userService 值是 null (调试的结果)
web.xml

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>



<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

作者: Jokerfly0209   发布时间: 2011-01-11

热门下载

更多