日期:2014-05-18 浏览次数:20739 次
<body>
<div id="demo">
wow! jQuery.<br/>
</div>
用户名:<input type="text" id="userName"/><br/>
密码:<input type="password" id="passWord"/><br/>
<input type="button" value="ajax" id="btn"/>
</body>
$(document).ready(function(){
$("#btn").bind("click",function(){
var params={"user.userName":$("#userName").val(),"user.passWord":$("#passWord").val()};
$.ajax({
url:"../action/ajax",
data:params,
type:"post",
dataType:"json",
success:function(data){
//alert(data.toJSONString());
if(data.user!=null){
alert("登录成功!");
}else{
alert("登录失败!");
}
}
})
})
});
<package name="ajax" namespace="/action" extends="json-default">
<action name="ajax" class="com.action.AjaxAction">
<result name="ajax" type="json">
<param name="includeProperties">
user.*
</param>
</result>
</action>
</package>
public class AjaxAction extends ActionSupport {
private User user; //User对象就俩属性:userName和passWord,都是String类型
private String toJSONString; //请注意这个属性,我的问题就是关于它的
public String execute(){
System.out.println(this.toJSONString); //请注意这一句,呆会我会粘出他的显示结果
if(!user.getUserName().equals("admin") || !user.getPassWord().equals("admin")){
user=null;
}
return "ajax";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getToJSONString() {
return toJSONString;
}
public void setToJSONString(String toJSONString) {
this.toJSONString = toJSONString;
}
}