吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:struts2-json实现Action逻辑

<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path id="classpath">
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${dist}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dist}"/>
        <mkdir dir="${dist}"/>
        <copy todir="${dist}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="classes" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
        </javac>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">


    <!-- 定义Struts 2的FilterDispathcer的Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>使用JSON插件</title>
    <script src="${pageContext.request.contextPath}/jquery-1.11.1.js" 
    type="text/javascript">
    </script>
    <script type="text/javascript">
        function gotClick()
        {
            $("#show").hide();
            // 指定向JSONExample发送请求,将id为form1的表单所包含的表单控件转换为请求参数
            $.post("JSONExample" , $("#form1").serializeArray() , 
                // 指定回调函数
                function(data , statusText)
                {
                    $("#show").height(80)
                        .width(240)
                        .css("border" , "1px solid black")
                        .css("border-radius" , "15px")
                        .css("background-color" , "#efef99")
                        .css("color" , "#ff0000")
                        .css("padding" , "20px")
                        .empty();
                    // 遍历JavaScript对象的各属性
                    for(var propName in data)
                    {
                        $("#show").append(propName + "-->"
                            + data[propName] + "<br />");
                    }
                    $("#show").show(600);
                },
                // 指定服务器响应为JSON数据
                "json");
        }
    </script>
</head>
<body>
<s:form id="form1">
    <s:textfield name="field1" label="Field 1"/>
    <s:textfield name="field2" label="Field 2"/>
    <s:textfield name="field3" label="Field 3"/>
    <tr><td colspan="2">
    <input type="button" value="提交" onclick="gotClick();"/>
    </td></tr>
</s:form>
<div id="show">
</div>
</body>
</html>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <package name="example" extends="json-default">
        <action name="JSONExample" class="org.crazyit.app.action.JSONExample">
            <!-- 配置类型的json的Result -->
            <result type="json">
                <!-- 为该Result指定参数 -->
                <param name="noCache">true</param>
                <param name="contentType">text/html</param>
                <!-- 设置只序列Action的map属性 -->
                <!--  param name="root">map</param -->
            </result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>
package org.crazyit.app.action;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import org.apache.struts2.json.annotations.JSON;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class JSONExample
{
    // 模拟处理结果的成员变量
    private int[] ints = {10, 20};
    private Map<String , String> map
        = new HashMap<String , String>();
    private String customName = "顾客";
    // 封装请求参数的三个成员变量
    private String field1;
    // 'transient'修饰的成员变量不会被序列化
    private transient String field2;
    // 没有setter和getter方法的成员变量不会被序列化
    private String field3;

    public String execute()
    {
        map.put("name", "疯狂Java讲义");
        return Action.SUCCESS;
    }

    // 使用注解来改变该成员变量序列化后的名字
    @JSON(name="newName")
    public Map getMap()
    {
        return this.map;
    }

    // customName的setter和getter方法
    public void setCustomName(String customName)
    {
        this.customName = customName;
    }
    public String getCustomName()
    {
        return this.customName;
    }

    // field1的setter和getter方法
    public void setField1(String field1)
    {
        this.field1 = field1;
    }
    public String getField1()
    {
        return this.field1;
    }

    // field2的setter和getter方法
    public void setField2(String field2)
    {
        this.field2 = field2;
    }
    public String getField2()
    {
        return this.field2;
    }

    // field3的setter和getter方法
    public void setField3(String field3)
    {
        this.field3 = field3;
    }
    public String getField3()
    {
        return this.field3;
    }
}

 

上一篇:java-规约-集合


下一篇:【oracle】select into from 和 insert into select 的用法和区别