欧美日本网站,亚洲欧美在线一区二区,亚洲日本精品一区http://www.aygfsteel.com/zyw090111/category/46088.html我的未來(lái)不是夢(mèng)!zh-cnFri, 22 Nov 2013 20:29:22 GMTFri, 22 Nov 2013 20:29:22 GMT60博客遷移嘍http://www.aygfsteel.com/zyw090111/archive/2013/10/08/404747.html平常心平常心Tue, 08 Oct 2013 06:02:00 GMThttp://www.aygfsteel.com/zyw090111/archive/2013/10/08/404747.htmlhttp://www.aygfsteel.com/zyw090111/comments/404747.htmlhttp://www.aygfsteel.com/zyw090111/archive/2013/10/08/404747.html#Feedback0http://www.aygfsteel.com/zyw090111/comments/commentRss/404747.htmlhttp://www.aygfsteel.com/zyw090111/services/trackbacks/404747.html感謝大家都我博客的關(guān)注和關(guān)系,現(xiàn)在將博客遷移到www.v5cn.cn上面了,有興趣的童鞋可以到上面尋找自己感興趣的技術(shù)博文,主要包括WorldWind,Lucene等技術(shù)。www.v5cn.cn



平常心 2013-10-08 14:02 發(fā)表評(píng)論
]]>
使用Struts2的iterator標(biāo)簽輕松遍歷復(fù)雜的Map類(lèi)型http://www.aygfsteel.com/zyw090111/archive/2010/08/26/330017.html平常心平常心Thu, 26 Aug 2010 14:18:00 GMThttp://www.aygfsteel.com/zyw090111/archive/2010/08/26/330017.htmlhttp://www.aygfsteel.com/zyw090111/comments/330017.htmlhttp://www.aygfsteel.com/zyw090111/archive/2010/08/26/330017.html#Feedback0http://www.aygfsteel.com/zyw090111/comments/commentRss/330017.htmlhttp://www.aygfsteel.com/zyw090111/services/trackbacks/330017.html更多博客請(qǐng)查看:http://www.v5cn.cn
1.創(chuàng)建一個(gè)Web工程,添加Struts2支持。
2.創(chuàng)建兩個(gè)實(shí)體類(lèi):
a). Mother(母親)的Java類(lèi)。
package struts.map.entity;

import java.io.Serializable;

public class Mother implements Serializable {

private static final long serialVersionUID = 1L;

private int motherId;        //母親ID
    private String motherName;        //母親名字
    public int getMotherId() {
return motherId;
}
public void setMotherId(int motherId) {
this.motherId = motherId;
}
public String getMotherName() {
return motherName;
}
public void setMotherName(String motherName) {
this.motherName = motherName;
}
}

b).Children(孩子)的Java類(lèi)

package struts.map.entity;

import java.io.Serializable;

public class Children implements Serializable {

private static final long serialVersionUID = 1L;

private int childId;        //孩子ID
    private int motherId;        //母親的ID
    private String childName;        //孩子名字
    
public int getChildId() {
return childId;
}
public void setChildId(int childId) {
this.childId = childId;
}
public int getMotherId() {
return motherId;
}
public void setMotherId(int motherId) {
this.motherId = motherId;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
}

 

3. 創(chuàng)建一個(gè)Action,并創(chuàng)建一位母親和她的孩子。

package struts.map.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import struts.map.entity.Children;
import struts.map.entity.Mother;

import com.opensymphony.xwork2.ActionSupport;

public class Struts2_Map extends ActionSupport {

private static final long serialVersionUID = 1L;

private Map<Mother,List<Children>> motherChildn;

public Map<Mother, List<Children>> getMotherChildn() {
return motherChildn;
}

@Override
public String execute() throws Exception {
/*-------------------以對(duì)象做父節(jié)點(diǎn)的鍵,List做子節(jié)點(diǎn)的值,的Map-----------------------*/
Mother mother 
= new Mother();
mother.setMotherId(
10000);
mother.setMotherName(
"花木蘭");

Children children1 
= new Children();
children1.setChildId(
10000);
children1.setMotherId(
10000);
children1.setChildName(
"小花木蘭1");

Children children2 
= new Children();
children2.setChildId(
10001);
children2.setMotherId(
10000);
children2.setChildName(
"小花木蘭2");

Children children3 
= new Children();
children3.setChildId(
10002);
children3.setMotherId(
10000);
children3.setChildName(
"小花木蘭3");

motherChildn 
= new HashMap<Mother,List<Children>>();

List
<Children> childrens = new ArrayList<Children>();

childrens.add(children1);
childrens.add(children2);
childrens.add(children3);

motherChildn.put(mother,childrens);

return SUCCESS;
}
}

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="map" extends="struts-default">
<action name="struts_map" class="struts.map.test.Struts2_Map">
<result>result.jsp</result>
</action>
</package>
</struts>  

4.創(chuàng)建兩個(gè)頁(yè)面:
a).跳轉(zhuǎn)頁(yè)面:
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>Struts_Map</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<href="struts_map.action">查看Map</a>
</body>
</html>

b).最終頁(yè)面,也是作重要的頁(yè)面:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>Struts_Map</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<div>
<h3>-----------------以對(duì)象做父節(jié)點(diǎn)的鍵,List做子節(jié)點(diǎn)的值,的Map--------------------</h3>
<s:iterator var="mc" value="motherChildn">
<div>
母親名稱(chēng):
<s:property value="key.motherName"/>
</div>
<s:iterator var="ch" value="value">
<div>
&nbsp;&nbsp;&nbsp;孩子名稱(chēng):<s:property value="#ch.childName"/>
</div>
</s:iterator>
</s:iterator>
</div>
</body>
</html>

最終運(yùn)行結(jié)果:


平常心 2010-08-26 22:18 發(fā)表評(píng)論
]]>
使用Struts2+Gson+JQuery實(shí)現(xiàn)異步請(qǐng)求JSON對(duì)象http://www.aygfsteel.com/zyw090111/archive/2010/07/29/327501.html平常心平常心Thu, 29 Jul 2010 10:15:00 GMThttp://www.aygfsteel.com/zyw090111/archive/2010/07/29/327501.htmlhttp://www.aygfsteel.com/zyw090111/comments/327501.htmlhttp://www.aygfsteel.com/zyw090111/archive/2010/07/29/327501.html#Feedback0http://www.aygfsteel.com/zyw090111/comments/commentRss/327501.htmlhttp://www.aygfsteel.com/zyw090111/services/trackbacks/327501.html更多博客請(qǐng)查看:http://www.v5cn.cn
GSON是Google公司的Java對(duì)象序列化成JSON的插件
下載地址:http://code.google.com/p/google-gson/downloads/list
下載下來(lái)以后:把gson-1.4.jar這個(gè)jar文件加到工程里。
Action的使用方式是:
package test.gson;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class TestGson extends ActionSupport {
    
private static final long serialVersionUID = 1L;
    
private Users user;
    
public Users getUser() {
        
return user;
    }

    
public void setUser(Users user) {
        
this.user = user;
    }

    @Override
    
public String execute() throws Exception {
        user 
= new Users();
        user.setId(
10000);
        user.setUserName(
"zhangsan");
        user.setPwd(
"000000");
        user.setEmail(
"zhangsan@sina.com");
        
        Gson g  
= new Gson();
        String json 
= g.toJson(user);
        HttpServletResponse response 
= ServletActionContext.getResponse();
        response.setContentType(
"application/json;charset=utf-8");
        response.setHeader(
"Cache-Control","no-cache");
        
        PrintWriter pw 
= response.getWriter();
        pw.print(json);
        pw.flush();
        pw.close();
        
        
        
return null;
    }

}

其中response.setContentType("application/json;charset=utf-8");是最重要的
一定要把ContentType設(shè)置成application/json形式
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="jsons" extends="struts-default" namespace="/">
        
<action name="testGson" class="test.gson.TestGson"></action>
    
</package>
</struts>
Gson其實(shí)可以用在所有的Javaweb工程了,不一定是Struts2

平常心 2010-07-29 18:15 發(fā)表評(píng)論
]]>
Struts2+JSON+jQuery實(shí)現(xiàn)異步交互數(shù)據(jù)時(shí)選擇要序列化的屬性(二使用XML配置方式)http://www.aygfsteel.com/zyw090111/archive/2010/07/29/327456.html平常心平常心Thu, 29 Jul 2010 08:29:00 GMThttp://www.aygfsteel.com/zyw090111/archive/2010/07/29/327456.htmlhttp://www.aygfsteel.com/zyw090111/comments/327456.htmlhttp://www.aygfsteel.com/zyw090111/archive/2010/07/29/327456.html#Feedback1http://www.aygfsteel.com/zyw090111/comments/commentRss/327456.htmlhttp://www.aygfsteel.com/zyw090111/services/trackbacks/327456.html更多博客請(qǐng)查看:http://www.v5cn.cn
只需在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="jsons" extends="json-default" namespace="/">
        
<action name="getJSON" class="test.json.Users">
            
<result name="success" type="json">
                
<!-- excludeProperties表示不包含的屬性(可以使用正則表達(dá)式匹配) -->
                
<param name="excludeProperties">
                    id,userName
                
</param>
                
<!-- includeProperties表示包含序列化的屬性(可以使用正則表達(dá)式匹配) -->
                
<param name="includeProperties">
                    pwd,address
                
</param>
            
</result>
        
</action>
    
</package>
</struts>    
默認(rèn)情況下Struts2插件的序列化是從Action開(kāi)始的如果需要序列化從指定的方式開(kāi)始請(qǐng)使用:
<!-- 這樣序列化工作就從birthday開(kāi)始了 -->
                
<param name="root">
                    birthday
                
</param>
Struts2JSONPlugin使用文檔.pdf

平常心 2010-07-29 16:29 發(fā)表評(píng)論
]]>
Struts2+JSON+jQuery實(shí)現(xiàn)異步交互數(shù)據(jù)時(shí)選擇要序列化的屬性(一注解方式)http://www.aygfsteel.com/zyw090111/archive/2010/07/29/327452.html平常心平常心Thu, 29 Jul 2010 08:15:00 GMThttp://www.aygfsteel.com/zyw090111/archive/2010/07/29/327452.htmlhttp://www.aygfsteel.com/zyw090111/comments/327452.htmlhttp://www.aygfsteel.com/zyw090111/archive/2010/07/29/327452.html#Feedback2http://www.aygfsteel.com/zyw090111/comments/commentRss/327452.htmlhttp://www.aygfsteel.com/zyw090111/services/trackbacks/327452.html更多博客請(qǐng)查看:http://www.v5cn.cn
在使用Struts2的JSON插件,實(shí)現(xiàn)Action中的屬性序列化成JSON對(duì)象時(shí)默認(rèn)JSON插件會(huì)把所有Action中包含getter方法的屬性都序列化到JSON對(duì)象中。但是有時(shí)候我們并不需要太多的屬性,或者只需要一個(gè)屬性。那么怎樣控制屬性序列化到JSON對(duì)象中哪?Struts2的JSON插件為我們提供了兩種方式,第一:使用注解的方式控制,第二:使用Struts2的struts.xml配置文件的方式。

這一講我們主要介紹注解方式。如果大家還不會(huì)Struts2+JSON+JQuery的交互方式請(qǐng)查看 http://zyw090111.javaeye.comStruts2+jQuery+JSON實(shí)現(xiàn)異步交互的文章

我們要使用JSON的注解是@JSON這個(gè)類(lèi)共有是個(gè)屬性分別是:
1. name    String 類(lèi)型     用戶(hù)為屬性起一個(gè)別名(我們序列化到JSON對(duì)象中的鍵默認(rèn)是屬性名稱(chēng),如果使用了name屬性那么鍵是name起的名字);
2. serialize  Boolean類(lèi)型   默認(rèn)為true 也就是可以被序列化,如果設(shè)為false那么該屬性將不包含在JSON對(duì)象中;
3. format  String類(lèi)型  主要是對(duì)日期進(jìn)行格式化
4. deserialize Boolean類(lèi)型 默認(rèn)為true,它是指反序列化,和serialize相反。
請(qǐng)看代碼:
package test.json;

import java.util.Date;

import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings(
"serial")
public class Users extends ActionSupport {
    
private int id;
    
private String userName;
    
private String pwd;
    
private String address;
    
private Date birthday;
    
public int getId() {
        
return id;
    }

    
public void setId(int id) {
        
this.id = id;
    }

    @JSON(serialize
=false)
    
public String getUserName() {
        
return userName;
    }

    
    
public void setUserName(String userName) {
        
this.userName = userName;
    }

    @JSON(name
="mm")
    
public String getPwd() {
        
return pwd;
    }

    
public void setPwd(String pwd) {
        
this.pwd = pwd;
    }

    
public String getAddress() {
        
return address;
    }

    
public void setAddress(String address) {
        
this.address = address;
    }

    @JSON(format
="yy-MM-dd")
    
public Date getBirthday() {
        
return birthday;
    }

    
public void setBirthday(Date birthday) {
        
this.birthday = birthday;
    }

    @Override
    
public String execute() throws Exception {
        
        
this.id = 10000;
        
this.userName = "zhangsan";
        
this.pwd = "00000";
        
this.address = "xian";
        
this.birthday = new Date();
        
        
return SUCCESS;
    }

}


平常心 2010-07-29 16:15 發(fā)表評(píng)論
]]>
主站蜘蛛池模板: 礼泉县| 如东县| 龙山县| 科技| 霸州市| 靖边县| 上饶市| 巴里| 鄯善县| 大埔县| 高要市| 左贡县| 和田市| 永靖县| 米林县| 蒙自县| 新源县| 桂平市| 霸州市| 沁源县| 苍山县| 东明县| 麻城市| 潜山县| 太原市| 湟源县| 保靖县| 呼玛县| 晋宁县| 荥阳市| 白水县| 临邑县| 南投市| 安化县| 酒泉市| 江都市| 宜州市| 清水县| 额济纳旗| 明星| 潍坊市|