struts2中,jsp页面通过ajax访问了action,action如何返回一个json数据给这个jsp页面,

struts2中,jsp页面通过ajax访问了action,action如何返回一个json数据给这个jsp页面,,第1张

后台:
public class pageAction extends ActionSupport{
private String username;
private String password;
private String cmd;
public String execute(){
String result = "";
String message = "";
//创建流
PrintWriter out = null;
//创建json对象
JSONObject json = new JSONObject();
cmd = ServletActionContextgetRequest()getParameter("cmd");
username = ServletActionContextgetRequest()getParameter("username");
password = ServletActionContextgetRequest()getParameter("password");
//Systemoutprintln("username:"+username+",password:"+password);
if("admin"equals(username) && "admin"equals(password)){
jsonput("content", "true");
}else{
jsonput("content", "输入的账号或密码有误!");
}
outwrite(jsontoString());
return SUCCESS;
}
}
前台:
<script type="text/javascript">
function checkAnswer() {
//获得输入的账号和密码的值
var username= documentgetElementById("username")value;
var password= documentgetElementById("password")value;
dataStr = {
checkname : username,
checkpass : password
};
jQuery
ajax( {
type : "POST",
url : "ajax/checkAnsweractiontemp=" + Mathrandom(),
data : dataStr,
dataType : "json",
success : function(root) {
if ("true" == rootcontent) {
jQuerymessageralert(" *** 作提示", "登录成功!", "info");
} else {
jQuerymessageralert(" *** 作提示", rootcontent, "info");
}
}});
}
</script>
试试吧,大体是这样的格式,可能还会有一些小错误,注意下就好!
还要注意下在Structs里配置时:

<action name="seekcardAction" class="seekcardAction" method="returnResult">
<result type="json" /> //注意返回类型
</action>

jsp中用ajax提交到另外一个表单数据:
jsp/ajax提交成功后采用以下方式跳转
1、本页面跳转:"windowlocationhref"、"locationhref"
2、上一层页面跳转:"parentlocationhref"
3、最外层的页面跳转:"toplocationhref"
举例说明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js这样写
"windowlocationhref"、"locationhref":D页面跳转
"parentlocationhref":C页面跳转
"toplocationhref":A页面跳转
如果D页面中有form的话,
<form>: form提交后D页面跳转
<form target="_blank">: form提交后d出新页面
<form target="_parent">: form提交后C页面跳转
<form target="_top"> : form提交后A页面跳转

通过参数传递来接收这个值就可以了。
比如:
onClick="javascript:location='/bvalue=<%value%>'" //这里假设Servlet的url-parttern是"/b"
然后bjava里getParameter("value");就得到传递的value了。
对value进行加工后直接放到session范围,页面上就可以直接获取该值了。

jsp页面
<input type="text" name="num" id="nums"/>
js
$ajax({
type:"post",
url:"randonu",
data:"randonu",
async:false,//是否异步
success:function(data){
alert(data);
}
});
action
private String num;
private boolean check;
if(numequals("123")){
check=true;
}else{
check=false;
}
请问如何才能够获取到check的在jsp页面上进行判断是true还是false

在javascript中,带中括号的表示数组,带大括号的表示对象,对象里的属性及其值的表示方法为“属性名”:“属性值”,多属性间用逗号分隔。
所以根据上面的分析可以得知,你的xml>

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13402769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-29
下一篇 2023-07-29

发表评论

登录后才能评论

评论列表(0条)

保存