项目需要jar包
项目结构
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 定义Struts2的FilterDispathcer的Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <!-- FilterDispatcher用来初始化Struts2并且处理所有的WEB请求。 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
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> <!-- 配置了系列常量 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="wangzs" extends="json-default"> <action name="userAction" class="com.wzs.action.UserAction"> <result name="success" type="json"> <param name="root">result</param> </result> </action> <!-- 让用户直接访问该应用时列出所有视图页面 --> <action name=""> <result>.</result> </action> </package> </struts>
testJson.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test json</title> <script type="text/javascript" src="scripts/jquery-1.8.js"></script> <script type="text/javascript"> $(function() { $("#testJson").click(function() { $.ajax({ type : "post", url : "userAction", data : { name : $("#name").val(), age : $("#age").val(), address : $("#address").val() }, dataType : "json", success : function(data) { var d = eval("(" + data + ")");//将数据转换成json类型 $("#s_name").text("" + d.name + ""); $("#s_age").text("" + d.age + ""); $("#s_address").text("" + d.address + ""); }, error : function() { alert("系统异常,请稍后重试!"); } }); }); }) </script> </head> <body> This is wangzs page. <br> <table border="1"> <tr> <td>用户名:</td> <td><input type="text" name="name" id="name" /></td> </tr> <tr> <td>年龄:</td> <td><input type="text" name="age" id="age" /></td> </tr> <tr> <td>地址:</td> <td><input type="text" name="address" id="address" /></td> </tr> <tr> <td colspan="2"><input type="button" value="测试json" id="testJson" /></td> </tr> </table> <br /> <ul> <li>姓名:<span id="s_name">赞无数据</span></li> <li>年龄:<span id="s_age">暂无数据</span></li> <li>职务:<span id="s_address">暂无数据</span></li> </ul> </body> </html>
UserAction.java
package com.wzs.action; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private String name; private String age; private String address; private String result; @Override public String execute() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("name", getName()); map.put("age", getAge()); map.put("address", getAddress()); JSONObject jsonObject = JSONObject.fromObject(map);// 将map对象转换成json类型数据 result = jsonObject.toString();// 给result赋值,传递给页面 return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } }
项目源码下载:http://download.csdn.net/detail/adam_zs/7452839
struts2返回json数据类型
时间: 2024-10-28 15:31:08