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

struts2 ajax---jQuery json的需要的jar包以及配置
2009-09-04 10:56

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不行struts2 中使用 json需要的jar包以及配置_第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>

你可能感兴趣的:(json,struts,String,user,jar,stylesheet)