问题描述:在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">时它只会得到他们的对象,而不会有他们的属性,因为你没有指定这个对象的属性