struts2整合json时不能输出对象的解决方案

问题描述:在android客户端需要登录时,验证登录时在服务器端,如果登录成功,那么通过json返回一个user对象

LoginAction 代码:

package com.rent.struts.action;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.rent.domain.User;
import com.rent.service.UserService;
/**
 * RegAction
 * @Scope("prototype")表示每次都new一个action,默认是单例
 */
@Controller
@Scope("prototype")
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 1L;
//注入UserService
@Resource
private UserService us ;
private String msg;
private User realUser;
private String email;
private String password;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public User getRealUser() {
return realUser;
}


public void setRealUser(User realUser) {
this.realUser = realUser;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

/**
* 进行登录
*/
public String doLogin(){
this.realUser=us.doLogin(this.email, this.password);
System.out.println(this.getRealUser());
if(this.realUser!=null){
this.msg="登录成功";
return "success";
}else{
return "fail" ;
}

}
}


struts.xml 配置:

<!-- 登录LoginAction -->
<action name="LoginAction_*" class="loginAction" method="{1}">
<result name="success" type="json">
<param name="includeProperties">msg,realUser</param>
</result>
<result name="success" type="json">
<param name="includeProperties">msg,realUser</param>
</result>
</action>

其它一切正常,发现msg能得到预期的值,但是realUser却是空,但是通过打印该realUser是有值的

解决方案:struts.xml配置问题

改为:

<!-- 登录LoginAction -->
<action name="LoginAction_*" class="loginAction" method="{1}">
<result name="success" type="json">
<param name="excludeProperties">email,password</param>
</result>
<result name="success" type="json">
<param name="excludeProperties">email,password</param>
</result>
</action>

为什么是这样:因为当时用<param name="includeProperties">时它只会得到他们的对象,而不会有他们的属性,因为你没有指定这个对象的属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值