I'm not getting response as JSON type data from server.
I'm using JSON plugin.
jQuery( "#dialog-form" ).dialog({
autoOpen: false,
height: 500,
width: 750,
modal: true,
buttons :{
"Search" : function(){
jQuery.ajax({type : 'POST',
dataType : 'json',
url : '<s:url action="part" method="finder" />',
success : handledata})
}
}
});
var handledata = function(data)
{
alert(data);
}
If dataType = 'json'
I am not getting any response, but if I don't mention any dataType
, I'm getting the HTML format of the page.
public String list(){
JSONObject jo = new JSONObject();
try {
Iterator it = findList.iterator();
while(it.hasNext()){
SearchResult part = (SearchResult) it.next();
jo.put("col1",part.getcol1());
jo.put("col2",part.getcol2());
}
log.debug("--------->:"+jo.toString());
} catch (Exception e) {
log.error(e);
}
return jo.toString();
}
struts.xml:
<package name="default" namespace="/ajax" extends="json-default">
<action name="finder"
class="action.Part" method="finder" name="finder">
<result type="json" />
</action>
</package>
JSP page:
<div id="dialog-form" >
<form action="" id="channelfinder">
<textarea id="products" name="prodnbr"<s:property value='prodNbr'/>
</form>
</div>
Console error:
org.apache.struts2.dispatcher.Dispatcher - Could not find action or result No result defined for action action.Part and result {"col1":"col1","col2":"col2"}
web.xml
:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>/parts</display-name>
<description>Parts List Web App</description>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/errorPage.jsp</location>
</error-page>
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
I'm not getting data to jQuery success. Please correct me, whats wrong here?
Struts2 JSON Plugin works in a particular way:
The JSON plugin provides a "json" result type that serializes actions into JSON.
It serializes the entire Action into JSON, except
If you don't want the whole Action to be serialized, but only one object of your choice, you can specify a Root Object:
Use the "root" attribute(OGNL expression) to specify the root object to be serialized.
it can be done in struts.xml
like this:
<result type="json">
<param name="root">
objectToBeSerialized
</param>
</result>
while the Action should have:
private CustomObject objectToBeSerialized;
public CustomObject getObjectToBeSerialized(){
return this.objectToBeSerialized;
}
Where CustomObject can be a Primitive, a String, an Array, etc...
Using it this way (the way it is built for), you can return SUCCESS
and ERROR
like in any other AJAX Struts2 Action, without breaking the framework conventions, and access the serialized JSON object from the callback function of the AJAX jQuery call like any other field (if using rootObject, the "data" of var handledata = function(data)
would be your object, otherwise it would be your Action).
In your case, assuming your object structure looks like this
row1 [col1, col2],
row2 [col1, col2],
rowN [col1, col2]
you could create a List of an object with two columns:
Value Object
public class MyRow implements Serializable {
private static final long serialVersionUID = 1L;
private String col1;
private String col2;
// Getters
public String getCol1(){
return this.col1;
}
public String getCol2(){
return this.col2;
}
}
Action class
public class PartAction implements Serializable {
private static final long serialVersionUID = 1L;
private List<MyRow> rows;
// Getter
public List<MyRow> getRows() {
return this.rows;
}
public String finder() {
String result = Action.SUCCESS;
rows = new ArrayList<MyRow>();
try {
Iterator it = findList.iterator();
while(it.hasNext()) {
SearchResult part = (SearchResult) it.next();
MyRow row = new MyRow();
row.setCol1(part.getcol1());
row.setCol2(part.getcol2());
rows.add(row);
}
} catch (Exception e) {
result = Action.ERROR;
log.error(e);
}
return result;
}
}
Struts.xml
<package name="default" namespace="/ajax" extends="json-default">
<action name="finder" class="action.Part" method="finder" name="finder">
<result type="json" >
<param name="root">
rows
</param>
</result>
</action>
</package>
To test it in the AJAX Callback Function, simply use $.each
:
var handledata = function(data) {
$.each(data, function(index) {
alert(data[index].col1);
alert(data[index].col2);
});
}
Of course you can use a List<List<String>>
instead of a Custom object, or any other object structure you like more than this: it was only to get you the idea.