struts2中,jsp页面通过ajax访问了action,action如何返回一个json数据给这个jsp页面, 20
在jsp页面中通过ajax访问了action,对带过去的数值进行判断,然后返回一个json格式的数据给这个jsp,action怎么返回这个json给jsp呢index.j...
在jsp页面中通过ajax访问了action,对带过去的数值进行判断,然后返回一个json格式的数据给这个jsp,action怎么返回这个json给jsp呢
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>mypage index jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="javascript/jquery-1.3.2.min.js" type="text/javascript"></script>
</head>
<body>
welcome<br>
<div id="testJquery" style="cursor:pointer;">
click
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
$('#testJquery').click(function(){
alert("test");
checkUser();
});
});
function checkUser(){
$.ajax({
type:'POST',
url:'http://localhost:8099/mypage/demo/page.action',
dataType:'JSON',
data:"cmd=10&json=['admin','admin']",
//data:{"cmd":"10","username":"admsdsin","password":"admin"},
beforeSend:function(XMLHttpRequest){
//alert("beforeSend");
},
success:function(data,textStatus){
alert("SUCCESS data:"+data);
},
error:function(XMLHttpRequest,textStatus,errorThrown){
alert("error,textStatus:"+textStatus);
}
});
}
</script>
</html>
pageAction.java
package action;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
public class pageAction extends ActionSupport{
private String username;
private String password;
private String cmd;
public String execute(){
String result = "";
String message = "";
cmd = ServletActionContext.getRequest().getParameter("cmd");
username = ServletActionContext.getRequest().getParameter("username");
password = ServletActionContext.getRequest().getParameter("password");
//System.out.println("username:"+username+",password:"+password);
if("admin".equals(username) && "admin".equals(password)){
//message = "this is success";
result = "success";
}else{
//message = "this is error";
result = "error";
}
return result;
}
}
省略了get和set方法
请高手帮忙看一下 展开
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>mypage index jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="javascript/jquery-1.3.2.min.js" type="text/javascript"></script>
</head>
<body>
welcome<br>
<div id="testJquery" style="cursor:pointer;">
click
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
$('#testJquery').click(function(){
alert("test");
checkUser();
});
});
function checkUser(){
$.ajax({
type:'POST',
url:'http://localhost:8099/mypage/demo/page.action',
dataType:'JSON',
data:"cmd=10&json=['admin','admin']",
//data:{"cmd":"10","username":"admsdsin","password":"admin"},
beforeSend:function(XMLHttpRequest){
//alert("beforeSend");
},
success:function(data,textStatus){
alert("SUCCESS data:"+data);
},
error:function(XMLHttpRequest,textStatus,errorThrown){
alert("error,textStatus:"+textStatus);
}
});
}
</script>
</html>
pageAction.java
package action;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
public class pageAction extends ActionSupport{
private String username;
private String password;
private String cmd;
public String execute(){
String result = "";
String message = "";
cmd = ServletActionContext.getRequest().getParameter("cmd");
username = ServletActionContext.getRequest().getParameter("username");
password = ServletActionContext.getRequest().getParameter("password");
//System.out.println("username:"+username+",password:"+password);
if("admin".equals(username) && "admin".equals(password)){
//message = "this is success";
result = "success";
}else{
//message = "this is error";
result = "error";
}
return result;
}
}
省略了get和set方法
请高手帮忙看一下 展开
展开全部
后台:
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 = ServletActionContext.getRequest().getParameter("cmd");
username = ServletActionContext.getRequest().getParameter("username");
password = ServletActionContext.getRequest().getParameter("password");
//System.out.println("username:"+username+",password:"+password);
if("admin".equals(username) && "admin".equals(password)){
json.put("content", "true");
}else{
json.put("content", "输入的账号或密码有误!");
}
out.write(json.toString());
return SUCCESS;
}
}
前台:
<script type="text/javascript">
function checkAnswer() {
//获得输入的账号和密码的值
var username= document.getElementById("username").value;
var password= document.getElementById("password").value;
dataStr = {
checkname : username,
checkpass : password
};
jQuery
.ajax( {
type : "POST",
url : "ajax/checkAnswer.action?temp=" + Math.random(),
data : dataStr,
dataType : "json",
success : function(root) {
if ("true" == root.content) {
jQuery.messager.alert("操作提示", "登录成功!", "info");
} else {
jQuery.messager.alert("操作提示", root.content, "info");
}
}});
}
</script>
试试吧,大体是这样的格式,可能还会有一些小错误,注意下就好!
还要注意下在Structs里配置时:
<action name="seekcardAction" class="seekcardAction" method="returnResult">
<result type="json" /> //注意返回类型
</action>
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 = ServletActionContext.getRequest().getParameter("cmd");
username = ServletActionContext.getRequest().getParameter("username");
password = ServletActionContext.getRequest().getParameter("password");
//System.out.println("username:"+username+",password:"+password);
if("admin".equals(username) && "admin".equals(password)){
json.put("content", "true");
}else{
json.put("content", "输入的账号或密码有误!");
}
out.write(json.toString());
return SUCCESS;
}
}
前台:
<script type="text/javascript">
function checkAnswer() {
//获得输入的账号和密码的值
var username= document.getElementById("username").value;
var password= document.getElementById("password").value;
dataStr = {
checkname : username,
checkpass : password
};
jQuery
.ajax( {
type : "POST",
url : "ajax/checkAnswer.action?temp=" + Math.random(),
data : dataStr,
dataType : "json",
success : function(root) {
if ("true" == root.content) {
jQuery.messager.alert("操作提示", "登录成功!", "info");
} else {
jQuery.messager.alert("操作提示", root.content, "info");
}
}});
}
</script>
试试吧,大体是这样的格式,可能还会有一些小错误,注意下就好!
还要注意下在Structs里配置时:
<action name="seekcardAction" class="seekcardAction" method="returnResult">
<result type="json" /> //注意返回类型
</action>
更多追问追答
追问
我这样改了,访问 localhost:8099/mypage,是404 ,tomcat的logs,localhost报出的错误是:
Exception starting filter struts2
Error building results for action page in namespace /demo - action - file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%205.5/webapps/mypage/WEB-INF/classes/struts.xml:5:49
追答
那应该你structs配置文件有错误,这个不好怎么说,因为每个人搭的框架都不一定相同。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你需要在classpath中添加struts2-json-plugin.jar
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
创建result的输入流 返回JSON对象 JS脚本调用。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
后台返回JsonConvert.SerializeObject(object value)这样的json数据应该可以
using Newtonsoft.Json.Converters;json在这个命名空间下
using Newtonsoft.Json.Converters;json在这个命名空间下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询