首页 > 代码库 > struts2返回json数据类型
struts2返回json数据类型
项目需要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=http://www.mamicode.com/"UTF-8" />>
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=http://www.mamicode.com/"scripts/jquery-1.8.js"></script>>
UserAction.javapackage 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。