如何在struts2中从JSON请求中获取数据
我试图从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>
你可以使用
1)Java script to parse the JSON字符串并显示该值
或
2)像脚本库AngularJS和JQuery这样的Java脚本库已经建立在支持它们的基础上,可以轻松高效地使用最少的代码。
你需要做两件事情:
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)创建该对象的一个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);
像任何其他财产。看到一些S2教程:http://struts.apache.org/docs/tutorials.html。 –
相关并值得一读:http://*.com/a/17149414/1654265 –
JSON请求它不是你的想法,获取数据是不同的。你可以用其他插件做类似的事情。 –