2015-05-08 66 views
0

我試圖從Apache Struts 2中的JSON請求中獲取數據。
我已經使用此URL進行了測試:http://date.jsontest.com/
StrutsJsonClientAction.java文件,行如何在struts2中從JSON請求中獲取數據

System.out.println(result); 

打印此控制檯:

{ "time": "12:04:12 PM", "milliseconds_since_epoch": 1431086652240, "date": "05-08-2015"} 

我想知道如何表達這個結果result.jsp

今天,我發現有resttemplate可以在spring中使用,我想知道是否有像struts2中的restemplate這樣的工具可以使用?

我目前的解決方案:StrutsJsonClientAction.java

package com.example.action; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import javax.servlet.http.HttpServletRequest; 
import org.apache.struts2.ServletActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
import javax.servlet.http.HttpServletRequest; 
import org.apache.struts2.ServletActionContext; 

public class StrutsJsonClientAction extends ActionSupport{ 

    private static final long serialVersionUID = 1L; 

    private final String url="http://date.jsontest.com/"; 

    public String execute() throws Exception { 

     HttpServletRequest request= ServletActionContext.getRequest(); 

     request.setCharacterEncoding("utf-8"); 

     URL myUrl = new URL(url); 
     //System.out.println(myUrl.toString()); 
     HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection(); 
     connection.connect(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8")); 
     String result=""; 
     String temp=null; 

     while ((temp=reader.readLine())!= null) { 
      result=result+temp; 
     } 

     System.out.println(result); 

     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="struts2" extends="struts-default"> 
     <action name="GoToGetApi" class="com.example.action.StrutsJsonClientAction"> 
      <result>/resault.jsp</result> 
     </action> 
    </package> 
</struts>  
+0

像任何其他財產。看到一些S2教程:http://struts.apache.org/docs/tutorials.html。 –

+1

相關並值得一讀:http://stackoverflow.com/a/17149414/1654265 –

+0

JSON請求它不是你的想法,獲取數據是不同的。你可以用其他插件做類似的事情。 –

回答

1

你需要做兩件事情:

1.-一旦你從URL獲取字符串(您的結果變量),你需要轉換成一個對象,例如,你可以創建一個Java Bean稱爲針對這種情況,對象將是這樣的:

public class Response{ 
    private String time; 
    private Long milisenconds; 
    private Date date; 
    //setters and getters 
} 

2,現在你可以投你的字符串轉換成使用傑克遜映射庫Response對象,看起來就像這樣:

private Response response; 

//don't forget to add your response and the getter to be able to show this value in your jsp, otherwise you can't do it 
public Response getResponse(){ 
    return response; 
} 
public String execute(){ 
    //your code to get your String result value. 
    ObjectMapper mapper=new ObjectMapper(); 
    response=mapper.readValue(result,Response.class);//will parse your String "result" into Response object using this library 
    return SUCCESS; 
} 

3.-你也需要添加一個新庫到你的項目中以支持JSON響應,你可以找到這個庫:struts2-json-plugin

而在你的包/動作配置中,而不是返回一個頁面,你將返回一個json類型:

<?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="struts2" extends="struts-default"> 
     <action name="GoToGetApi" class="com.example.action.StrutsJsonClientAction"> 
      <result type="json"> 
       <param name="root">response</param> 
      </result> 
     </action> 
    </package> 
</struts>  

4.-所以現在你可以把這個網址:http://localhost:8080/yourProyect/GoToGetApi.action,你會看到JSON格式的效應初探。

但是,如果你只有一個「打印」在你的JSP JSON字符串,你可以這樣做只是增加這個在動作類:

private String apiResponse; 
//get of apiResponse 

public String execute(){ 
     //your current code here 
     System.out.println(result); 
     apiResponse=result; 
     return SUCCESS; 
    } 

而且在你的JSP,你只需要添加:

<body> 
    <!--your current code here--> 
    <label>This is my response:</label> ${apiResponse} 
    <br/> 
    <label>This is my response:</label><s:property value="apiResponse" /> 
</body> 
</html> 
+1

前兩個代碼片段毫無用處且完全混亂......但答案的其餘部分是正確的,+1 –

0

兩件事情:

1)創建該對象的一個​​JavaBean。 JavaBean中的字段名稱必須與JSON的字段匹配(儘管如果需要的話可以繞過這個)。

public class JsonTest{ 
    private String time; 
    private Long milliseconds_since_epoch; 
    private Date date; 
    //setters and getters 
} 

2)使用Gson將JSON Stirng編組到一個對象中。

public static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); 

然後使用它...

JsonTest object = gson.fromJson(jsonString, JsonTest.class); 

從任何對象得到JSON字符串....

String json = gson.toJson(someObject);