PDA

View Full Version : extjs,struts and ajax



frknml
28 Nov 2008, 9:17 AM
Hi everyone;

i'm familiar with java and struts but i'm very new for extjs so i need your help.Please give me an advice, how can i use ext with struts ?Can you give me an detailed information ?Or please explain me step by step usage of struts with extjs.

faruk namlı

omerio
28 Nov 2008, 11:06 AM
First you need to decide on which form you are going to submit and retrieve data from the server (e.g. json, xml, form parameters, etc...), one approach I use is to retrieve data from the server using json and to submit to the server using request parameters.

If you use Struts 1

To post from the UI using request parameters which will get populated into your form. On the front-end you could use Ext.data.Connection to submit request parameters to the server:

new Ext.data.Connection().request({
url: '/MyApp/myaction.do',
params: {id: 10,
name: 'test'
},
failure: requestFailed,
success: requestSuccessful
});


And your struts form


public class MyForm extends ActionForm {
private int id;
private String name;
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
}


To send json back to the client you can use the JSON taglib (http://json-taglib.sourceforge.net/) and have your action classes forward to a jsp fragment such as:


<%@ page contentType="text/json"%>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="s" uri="/struts-tags"%>

<c:forEach var="person" items="${persons}">
<json:object>
<json:property name="firstName" value="${person.firstName}" />
<json:property name="lastName" value="${person.lastName}" />
<json:object name="address">
<json:property name="houseNumber" value="${person.address.houseNumber}" />
<json:property name="street" value="${person.address.street}" />
<json:property name="city" value="${person.address.city}" />
<json:property name="postcode" value="${person.address.postCode}" />
</json:object>
<json:object name="settings">
<json:property name="preferredName" value="${person.settings.preferredName}" />
<json:property name="backgroundColor" value="${person.settings.backgroundColor}" />
<json:property name="registrationDate">
<fmt:formatDate value="${person.settings.registrationDate}" />
</json:property>
</json:object>
</json:object>
</c:forEach>


If you use Struts 2

Then things are alot easier. The same approach mentioned above to post to the server can be used. This time your action class is the form.

To send JSON back to the client use the Struts 2 json plugin (http://cwiki.apache.org/S2PLUGINS/json-plugin.html) and in your struts config define a result of type json for your action class:


<action name="readStock" method="readStock" class="stockAction">
<result type="json">
<param name="enableGZIP">true</param>
<param name="noCache">true</param>
<param name="excludeNullProperties">true</param>
<param name="root">results</param>
</result>
</action>


This thread regarding using Ext with Java is also quite useful:

http://extjs.com/forum/showthread.php?t=35014