Based on my question posted a few days ago, I realized that SimpleFormController
is inappropriate for handling Ajax requests. Therefore, I'm migrating my application towards annotated controllers.
I'm trying to return a java.util.List
from Oracle database using Spring MVC 3.0.2 with Hibernate via Ajax using Jackson 1.9.8 (its download page) but I haven't yet worked with JSON in any technology. I have read up some tutorials/articles but I couldn't get the idea of how to return such complex data structures and parse them using JSON in Spring. I'm trying to learn JSON-like concepts first.
What basically I'm trying is when a country is selected from a country select box, the states corresponding to that country should be populated from the database via Ajax. I don't have precise idea about how to return a java.util.List
over an Ajax response, how to parse it and again use it in Java code. I'm only upto the following level.
JS code.
function getStates(countryId)
{
$.ajax({
datatype:"json",
type: "POST",
url: "/wagafashion/ajax/TempAjax.htm",
data: "countryId=" + countryId,
success: function(response)
{
$('#msg').html(response);
$('#stateList').val('');
},
error: function(e)
{
alert('Error: ' + e);
}
});
}
The method in the Spring controller class which is invoked when the Ajax request is made on the onchange
event of the country select box.
@RequestMapping(method=RequestMethod.POST, value="ajax/TempAjax")
public @ResponseBody List<StateTable> getStateList(@ModelAttribute("tempBean") TempBean tempBean, BindingResult error, Map model, HttpServletRequest request, HttpServletResponse response)
{
Session session=NewHibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<StateTable>list=session.createQuery("from StateTable where country.countryId=:countryId order by stateId").setParameter("countryId", new BigDecimal(request.getParameter("countryId"))).list();
session.flush();
session.getTransaction().commit();
return list;
}
The state select box I need to populate with the list of states returned by the Ajax response using a <c:forEach></c:forEach>
loop of EL.
<form:select path="cmbState" class="validate[required] text-input tooltip" title="Mandatory select field.">
<form:option value="">Select</form:option>
<c:forEach items="${stateList}" var="row">
<c:choose>
<c:when test="${row.stateId==param['stateId'] and deselectCombo!=1}">
<form:option value="${row.stateId}" selected="selected">${row.stateName}</form:option>
</c:when>
<c:otherwise>
<form:option value="${row.stateId}">${row.stateName}</form:option>
</c:otherwise>
</c:choose>
</c:forEach>
</form:select>
<font style="color: red"><form:errors path="stateId"/></font><br/>
I could only make Ajax request and response successfully. Nothing more I could understand from those tutorials found over the internet. More precisely, how can I use the Ajax response in the items
attribute of the preceding <c:forEach><c:forEach>
loop such as items="${stateList}"
?
Could you give me some hint/idea how can I return a list of data and use it in the preceding loop to populate the state select box? Could you please lead me some steps ahead from here?
I'm using NetBeans 6.9.1 (not Eclipse). In some tutorials about Marvan projects in Eclipse, it was mentioned that the pom.xml
file is required to configure to include <dependencies></dependencies>
(Jackson dependency). There is no such thing like pom.xml
in my project in NetBeans. Is it required to configure somewhere in some xml file in NetBeans such as the one mentioned here?