要加的struts包有:
commons-beanutils-1.7.0.jar
commons-chain-1.2.jar
commons-collections-3.2.jar
commons-digester-2.0.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-validator-1.3.1.jar
ezmorph-1.0.3.jar (不这个会去一非常怪错误。不相信你可以试试) 我认为应该无法实现java对象与json对象之间转化.
freemarker-2.3.15.jar
json-lib-2.1.jar
ognl-2.7.3.jar
struts2-json-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
struts2-core-2.1.8.1.jar
其它具体 的代码;我是看了。http://www.iteye.com/topic/576513。他是原码而写的。基本一样了。
web.xml
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
strust.xml
- <package name="ajax" extends="json-default">
- <action name="ajaxLogin" class="org.test.action.AjaxLoginAction">
-
- <result type="json">
-
-
- <param name="root">result</param>
- </result>
-
- </action>
- </package>
<package name="ajax" extends="json-default">
<action name="ajaxLogin" class="org.test.action.AjaxLoginAction">
<!-- 返回类型为json 在json-default中定义 -->
<result type="json">
<!-- root的值对应要返回的值的属性 -->
<!-- 这里的result值即是 对应action中的 result -->
<param name="root">result</param>
</result>
</action>
</package>
action代码:
- private String result;
-
- private String loginName;
- private String password;
-
- public String getResult() {
- return result;
- }
-
-
- public void setResult(String result) {
- this.result = result;
- }
-
-
- public String getLoginName() {
- return loginName;
- }
-
-
- public void setLoginName(String loginName) {
- this.loginName = loginName;
- }
-
-
- public String getPassword() {
- return password;
- }
-
-
- public void setPassword(String password) {
- this.password = password;
- }
-
-
- @Override
- public String execute() {
-
- try{
- System.out.println("------------");
-
- Map<String, String> map = new HashMap<String, String>();
- System.out.println(this.loginName);
-
- map.put("name", this.loginName);
- map.put("password",this.password);
-
-
- JSONObject jo = JSONObject.fromObject(map);
- System.out.println("*********");
-
- this.result = jo.toString();
-
-
- System.out.println(this.result);
-
- }catch(Exception e){
- System.out.println(e);
- }
-
-
- return SUCCESS;
-
- }
private String result;
// struts的属性驱动模式,自动填充页面的属性到这里
private String loginName;
private String password;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() {
try{
System.out.println("------------");
// 用一个Map做例子
Map<String, String> map = new HashMap<String, String>();
System.out.println(this.loginName);
// 为map添加一条数据,记录一下页面传过来loginName
map.put("name", this.loginName);
map.put("password",this.password);
// 将要返回的map对象进行json处理
JSONObject jo = JSONObject.fromObject(map);
System.out.println("*********");
// 调用json对象的toString方法转换为字符串然后赋值给result
this.result = jo.toString();
// 可以测试一下result
System.out.println(this.result);
}catch(Exception e){
System.out.println(e);
}
return SUCCESS;
}
index.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script><script type="text/javascript">
- $(document).ready( function() {
- //使用 Ajax 的方式 判断登录
- $("#btn_login").click( function() {
- var url = 'ajaxLogin.action';
- //获取表单值,并以json的数据形式保存到params中
- var params = {
- loginName:$("#loginName").val(),
- password:$("#password").val()
- //上行结尾决对不加逗号(,)。如果加了在IE6下就不能运行实现了。
- }
- alert(params.loginName);
- //使用$.post方式
- $.post(
- url, //服务器要接受的url
- params, //传递的参数
- function cbf(data){ //服务器返回后执行的函数 参数 data保存的就是服务器发送到客户端的数据
- //alert(data);
- var member = eval("("+data+")"); //包数据解析为json 格式
- $('#result').html("欢迎您: "+member.name+"<br>您的密码是:"+member.password);
- },
- 'json' //数据传递的类型 json
- );
- });
- });
- </script>
- </head>
- <body>
- <center>
- <table>
- <tr>
- <td>
- <span>用户名:</span>
- </td>
- <td>
- <input type="text" id="loginName" name="loginName">
- </td>
- </tr>
- <tr>
- <td>
- <span>密码:</span>
- </td>
- <td>
- <input type="password" name="password" id="password">
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="button" id="btn_login" value="Login" />
- </td>
- </tr>
- </table>
- <p>
- 这里显示ajax信息:
- <br />
- <span id="result"></span>
- </p>
- </center>
-
-
- </body>
- </html>