XML

1、引入dom4j-2.0.0.jar

2、引入jquery-1.8.2.js

3、新建common.js

getInfo = function(){
$.post("getXmlAction.action",{name:$('#name').val()},function(ret,status){
if("success" == status)
var id = $(ret).find("id").text();
var age = $(ret).find("age").text();
var address = $(ret).find("address").text();
var name = $(ret).find("name").text(); var html = "<table>" +
"<tr>" +
"<th>" +id+age+address+name
"</th>"+
"</tr>" +
"</table>"
$('body table:eq(0)').remove();
$('#xmlbody').append(html);
});
} getJson = function(){
    $.post("getJsonAction.action",{name:$('#name').val()},function(ret,status){
        if("success" == status){
        
        }
    });
}

4、新建xmlTest.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="scripts/common.js"></script>
</head> <body id="xmlbody">
<select id="name">
<option value="james" selected="selected">james</option>
<option value="currey">currey</option>
</select>
<input type="button" value="getInfo" onclick="getInfo();"/>
</body>
</html>

5、修改struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd"> <struts>
<package name="testSSH" extends="struts-default"> <action name="getXmlAction" class="com.test.action.xml.GetXmlAction"> </action>
</package>
</struts>

6、新建GetXmlAction.java

package com.test.action.xml;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.dom4j.*;
import org.dom4j.io.*;
import com.opensymphony.xwork2.ActionSupport; public class GetXmlAction extends ActionSupport { private static final long serialVersionUID = 1L; private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String execute() throws Exception {
person person1 = new person();
person1.setAge(11);
person1.setName("james");
person1.setId(1);
person1.setAddress("beijing"); person person2 = new person();
person2.setId(2);
person2.setAddress("shanghai");
person2.setAge(12);
person2.setName("currey"); //创建 xml 对象
Document document = DocumentHelper.createDocument();
//创建根元素
Element rootElement = document.addElement("users");
Element userElement = rootElement.addElement("user");
Element idElement = userElement.addElement("Id");
Element ageElement = userElement.addElement("age");
Element addressElement = userElement.addElement("address");
Element nameElement = userElement.addElement("name");
if("james".equals(this.name)){
idElement.setText(person1.getId()+"");
ageElement.setText(person1.getAge()+"");
addressElement.setText(person1.getAddress());
nameElement.setText(person1.getName());
}else{
idElement.setText(person2.getId()+"");
ageElement.setText(person2.getAge()+"");
addressElement.setText(person2.getAddress());
nameElement.setText(person2.getName());
}
HttpServletResponse response = ServletActionContext.getResponse();
//设置返回类型
response.setContentType("text/xml; charset=UTF-8 ");
//清除浏览器缓存
response.setHeader("Cache-Control", "no-cache");
//定义输出流
PrintWriter out = response.getWriter();
//定义格式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
//定义XMLWriter 对象,将document 对象,输出到客户端
XMLWriter writer = new XMLWriter(out,format);
writer.write(document);
out.flush();
out.close();
return null;
}
}

JSON

1、引入struts2-json-plugin.jar、json-lib.jar包

2、配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd"> <struts>
  // 继承自json-default 而json-default继承自 struts-default
<package name="testSSH" extends="json-default">
//异步请求不定义返回值
<action name="getXmlAction" class="com.test.action.xml.GetXmlAction"></action>
     //异步请求返回值为SUCCESS,定义类型为json 即可,客户端到服务端序列化的过程交由core 处理
<action name="getJsonAction" class="com.test.action.json.GetJsonAction">
<result name="success" type="json">
         //排除 属性 name MyAge【指向别名】
<param name="excludeProperties">name,MyAge</param>
         // 只解析root
<param name="root">person</param>
</result>
</action> </package>
</struts>

3、定义GetJsonAction.java

package com.test.action.json;

import org.apache.struts2.json.annotations.JSON;
import java.util.*; import com.opensymphony.xwork2.ActionSupport;
import com.test.action.json.person; public class GetJsonAction extends ActionSupport { private static final long serialVersionUID = 1L; //客户端传入的名称
private String name; private int age; private String address; private int id; private Date date; private person person = new person(); public person getPerson() {
return person;
} public void setPerson(person person) {
this.person = person;
} @JSON(format = "yyyy-MM-dd")
public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @JSON(name = "MyAddress")
public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @JSON(serialize=false)
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} //将Action 的成员变量序列化成json对象
@Override
public String execute() throws Exception {
person person1 = new person();
person1.setAge(11);
person1.setName("james");
person1.setId(1);
person1.setAddress("beijing");
person1.setDate(new Date()); person person2 = new person();
person2.setId(2);
person2.setAddress("shanghai");
person2.setAge(12);
person2.setName("currey");
person2.setDate(new Date()); if("james".equals(this.name)){
this.id = person1.getId();
this.age = person1.getAge();
this.address = person1.getAddress();
this.date = person1.getDate();
}else{
this.id = person2.getId();
this.age = person2.getAge();
this.address = person2.getAddress();
this.date = person2.getDate();
} return SUCCESS;
}
}

4、定义person属性类

package com.test.action.json;
import java.util.*; public class person { private int id;
private String name;
private int age;
private String address;
private Date date; public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
} }

5、引入jsonText.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="scripts/common.js"></script>
</head> <body id="xmlbody">
<select id="name">
<option value="james" selected="selected">james</option>
<option value="currey">currey</option>
</select>
<input type="button" value="getInfo" onclick="getJson();"/>
</body>
</html>

java学习笔记(12) —— Struts2 通过 xml /json 实现简单的业务处理的更多相关文章

  1. java学习笔记(13) —— google GSON 实现json转化方法

    1.配置struts.xml <action name="getGsonAction" class="com.test.action.json.GetGsonAct ...

  2. java学习笔记(11) —— Struts2与Spring的整合

    1.右键 项目名称 —— MyEclipse —— Add Spring Capabilities 2.选取 Copy checked Library contents to project fold ...

  3. java学习笔记 (9) —— Struts2 国际化

    1.Test.java package com.i18n; import java.util.Locale; public class Test1 { public static void main( ...

  4. java学习笔记 (8) —— Struts2 实现上传

    1.新建upload.jsp <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  5. java学习笔记 (2) —— Struts2类型转换、数据验证重要知识点

    1.*Action.conversion-properties 如(point=com.test.Converter.PointListConverter) 具体操作类的配置文件 2.*Action. ...

  6. Java学习笔记(十)——xml

    [前面的话] 学习过程中还是效率不够高,老是容易注意力不集中,着急啊.不能在这样了,要好好学习,好好努力. 学习过程中的小知识点总结,基础知识,选择阅读. [xml定义] 定义:可扩展标记语言(英语: ...

  7. UI学习笔记---第十六天XML JSON解析

    一.解析的基本概念 从事先规定好的格式中提取数据 解析的前提:提前约定好格式.数据提供方按照格式提供数据,数据方按照格式获取数据 常见解析方式XML解析JSON解析 二.XML:可扩展标记语言 XML ...

  8. Java学习笔记12

    循环 打印一个字符串(例如: "Welcome to Java!") 100次,就需要吧下面的输出语句重复写100遍,这是相当繁琐的: System.out.println(&qu ...

  9. java学习笔记 (5) —— Struts2 监听器配置

    1.创建MyListener.java 实现 PreResultLisener 接口 import com.opensymphony.xwork2.ActionInvocation; import c ...

随机推荐

  1. 第三代搜索推出网民评价系统,seo末日还会远吗?

    昨天的360搜索可谓风光无限,两大搜索新品同日上线,至今360导航页面依旧飘荡着两者的身影,但是不少站长从此却是忧心忡忡,seo末日是否真的要到来了?笔者想起数日前写的一篇博文:seo末日言论频频来袭 ...

  2. 【转】JAVA字符串格式化-String.format()的使用

    原文网址:http://blog.csdn.net/lonely_fireworks/article/details/7962171 常规类型的格式化 String类的format()方法用于创建格式 ...

  3. Linux环境下使用图形化界面的SVN客户端软件-RabbitVCS

    如果想在Linux环境下使用图形化界面的SVN客户端软件,那么RabbitVCS绝对是首选,可以媲美Windows环境下用的TortoiseSVN,甚至连操作都基本一样,所以强烈推荐给各位童鞋. Ra ...

  4. socket编程里的read和recv函数【转载】

    本文转载自:http://blog.163.com/like12@126/blog/static/63023403201291983117551/ 1.read 与 recv 区别 read 原则: ...

  5. MARCHdesign_王濤's 星云个人网站 | www.xingyun.cn/MARCHdesign

    MARCHdesign_王濤's 星云个人网站 | www.xingyun.cn/MARCHdesign MARCHdesign_王濤

  6. Linux操作系统Centos7.2版本搭建Apache+PHP+Mysql环境

    对于在校大学生来说腾讯云1元主机很划算,所以就申请了一台,打算在上面学习下linux,使用版本为centos7.2版本.在服务器上比较推荐centos,此版本生命周期较长,而且网上有关centos的教 ...

  7. 【原创】车载实时路况信息接收终端移植于Smart210开发板 --- 综合教程

    [原创]车载实时路况信息接收终端移植于Smart210开发板 --- 综合教程 所用工具: windows电脑 Ubuntu12.04 Smart210开发板 4g以上SD卡 U盘 步骤: 1.    ...

  8. [Flexbox] Using flex-direction to layout content horizontally and vertically

    The Flexbox css spec allows for more adjustable layouts. The flex-directionproperty allows you to ea ...

  9. 【解决】hive动态添加partitions不能超过100的问题

    Author: kwu [解决]hive动态添加partitions不能超过100的问题,全量动态生成partitions超过100会出现例如以下异常: The maximum number of d ...

  10. 奇妙的go语言(网页下载)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 眼下,网上关于网页爬行的代码非常多.可是,自从看了go语言的web下载代码之后,我才发现原来它 ...