struts2 中使用 json需要的jar包以及配置

commons-lang-2.4.jar;

json-lib-2.3-jdk13.jar;

jsonplugin-0[1].32.jar;

ezmorph-1.0.2.jar;

commons-beanutils-1.7.0.jar;

commons-collections-3.2.1.jar(这个包要注意版本,我用2.1不行

还需要把jquery-1[1].2.6.pack.js;

struts.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ajax" extends="json-default">
<action name="testAction" class="model.testAction">
<result type="json">/index.jsp</result>
</action>
</package>
</struts>

testAction.java

package model;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

public class testAction extends ActionSupport {
User user;
String result;
@Override
public String execute() throws Exception {
JSONObject js=JSONObject.fromObject(user) ;
System.out.println(js);
result=js.toString();
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}

package model;

public class User {
String username;
String password;

public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>My JSP 'index.jsp' starting page</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 type="text/javascript" src="<%=path %>/js/jquery-1[1].2.6.pack.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
var params=$("input").serialize();
$.ajax({
url:"testAction.action",
type:"post",
dataType:"json",
data:params,
success:update_page


});
});
});
function update_page(result){

var json=eval(result);
alert(json);
var str="姓名:"+json.user.username+"<br>";
str+="密码:"+json.user.password+"<br>";
$("#result").html(str);
}
</script>

</head>

<body>
<div id="result">

</div>

<s:form action="testAction" method="post">

<s:textfield label="用户名" name="user.username" />

<s:textfield label="密码" name="user.password" />

<button>

提交

</button>

</s:form>

</body>
</html>

from:http://blog.csdn.net/agxy08xx1mxx/article/details/6962303

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值