問題描述:在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">時它只會得到他們的對象,而不會有他們的屬性,因爲你沒有指定這個對象的屬性