我这里写的是ajaxSubmit获取返回值(ajax后台写法一样)
我也不知是否是java ssh框架from提交ajaxSubmit、ajax获取返回值 正确写法。上网查一大堆不知写什么的(反正我试,不成功),最后看到了一个,改了下。能实现效果。
1、jsp代码
<form id="loginform" action="${ctx}/SignupAction!logOK.action" method="post">
登录 <input type="text" id="signUpName" name="signupname" aria-required="true">
密码 <input type="password" id="password" name="password" aria-required="true">
<input type="button" class="login_btn" onClick="login()" value="立即登录">
</form>
。。。。。。。。。。。。。。。。。。。。。。。。。。
<script type="text/javascript">
//登录
function login(){
$("#loginform").ajaxSubmit(function(data){
if((typeof data)=="string"){
data=JSON.parse(data);
}
console.log(data);
console.log("111");
/* layer.msg(data.msg);
if(data.state){
setTimeout('tzaa()', 2000);
}; */
})
}
</script>
2、(SignupAction)服务端
//注入服务器
@Autowired
private ISignupService signupService;
private Signuptable signuptable_D;
private JsonReturn jsonReturn;//公用vo 下面我会写
HttpServletResponse response = ServletActionContext.getResponse();
//登录按钮
public String logOK(){
jsonReturn=new JsonReturn();//判断
jsonReturn.setState(false);
String xdf="0";//判断输出一:
signuptable_D=signupService.findXX(this.getSignupname());//ServiceImpe的查询数据(根据前台的名字查询)
try {
//// 输出一:简单的类型如字符串 整型等
////response.getWriter().write(xdf); //write() 仅支持输出字符类型数据,字符、字符数组、字符串等
//输出二(ok):特殊的数组(表) 好像和jdbc一样啊
response.setContentType("text/html;charset=utf-8");//设置输出格式
JSONObject jsonObject=JSONObject.fromObject(jsonReturn);
PrintWriter out = response.getWriter();
out.println(jsonObject.toString());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
3、struts.xml不用加代码了(没用到它)
公用vo :JsonReturn
package com.gx.vo;
import java.io.Serializable;
public class JsonReturn implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5863100019332084445L;
private Boolean state;
private String msg;
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
新手勿喷,不懂可留言。