Pessoal estou com algumas dúvidas a respeito da integridade do struts 2 com json, vamos as dúvidas…
Como faço para enviar os dados que foram digitados no form para action? sem submit, debugando o sistema o atributo nome por exemplo esta recebendo null.
minha pagina de teste esta assim
JSP
<%--
Document : index
Created on : 08 27, 08, 8:39:18 PM
Author : stikiflem
--%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Struts 2 JSON sample</title>
<script type="text/javascript" src="prototype-1.6.0.2.js"></script>
<script type="text/javascript">
function fetchJSONData(){
var url = 'FetchJSON.action';
var myAjax = new Ajax.Request(
url,
{
method: 'post',
onComplete: fetchSuccess
});
}
function fetchSuccess(originalRequest){
//this is the json return data
var result = originalRequest.responseText.evalJSON();
for (var i=0;i<result.sampleList.length;i++){
//alert(result.sampleList[i]);
document.getElementById('div').innerHTML = result.sampleList;
}
}
</script>
<s:head theme="ajax"/>
</head>
<body>
<s:textfield name="nome" id="nome"/>
<input type="button" value="Get JSON Data (using Struts 2)" onclick="fetchJSONData();"/>
<div id="div">
</div>
</body>
</html>
Minha Action
public class Json extends ActionSupport{
private List sampleList;
private String nome;
@Override
public String execute() {
sampleList = new ArrayList();
sampleList.add("stikiflem sample 1");
sampleList.add("stikiflem sample 2");
sampleList.add("stikiflem sample 3");
sampleList.add("stikiflem sample 4");
sampleList.add("heheh, diogo no mundo ajax");
System.out.println("----------------------------------------------");
System.out.println("----------------------------------------------");
System.out.println("-sampleList:" +sampleList.toString());
System.out.println("----------------------------------------------");
System.out.println("----------------------------------------------");
return Action.SUCCESS;
}
public String getJSON(){
return execute();
}
public List getSampleList() {
return sampleList;
}
public void setSampleList(List sampleList) {
this.sampleList = sampleList;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
Meu struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<include file="com/stikiflem/stikiflem.xml"/>
</struts>
Meu stikiflem.xml
<struts>
<package name="stikiflem" namespace="/" extends="struts-default,json-default">
<action name="FetchJSON" class="com.stikiflem.Json" method="getJSON">
<result type="json"/>
</action>
</package>
</struts>
E ai como eu Faço?