i have a problem to retrieve the Collection(or ArrayList) from a Controller to jsp using ajax in Spring MVC
.
The Controller code is:
ProjectController.java
@Controller
public class ProjectController {
protected final Logger log = Logger.getLogger(ProjectController.class);
@Autowired
private AziendaService service;
@RequestMapping(value ="/companyList", method = RequestMethod.GET,headers = "application/javascript")
public @ResponseBody Object getCompanyList_AJAX() {
log.info("initiale getCompanyList_AJAX method.....");
Collection<Company> collection = service.getAllCompany();
return collection;
}
}
where Company(Azienda) is Class POJO and Entity in my DBMS.
and the dispatcher-servlet.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven />
<context:property-placeholder location="classpath:properties/*.properties"/>
<context:component-scan base-package="spring.hibernate"/>
<tx:annotation-driven />
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${connection.driver_class}" />
<property name="url" value="${connection.url}" />
<property name="username" value="${connection.username}" />
<property name="password" value="${connection.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>spring.hibernate.model.Account</value>
<value>spring.hibernate.model.Assegnazione</value>
<value>spring.hibernate.model.Attivita</value>
<value>spring.hibernate.model.Azienda</value>
<value>spring.hibernate.model.Comune</value>
<value>spring.hibernate.model.Dipendente</value>
<value>spring.hibernate.model.Persona</value>
<value>spring.hibernate.model.Progetto</value>
<value>spring.hibernate.model.Provincia</value>
<value>spring.hibernate.model.Regione</value>
<value>spring.hibernate.model.Timesheet</value>
<value>spring.hibernate.model.Workpackage</value>
</list>
</property>
</bean>
<bean id="accountDAO" class="spring.hibernate.dao.AccountDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="aziendaDAO" class="spring.hibernate.dao.AziendaDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
and The ajax in JSP code is: addProject.jsp
<script type="text/javascript">
$(window).load(function () {
$.ajax({
type: 'GET',
url: 'companyList.htm',
datatype:'text',
success: function(response, status, xhr) {
if(xhr.readyState == 4 &&(xhr.status == 200 || xhr.status == 0)) {
var values = [];
values = response;
$.each(values, function( index, value ) {
alert( index + ": " + value );
});
document.getElementById("loaderCompany").style.display = "none";
}
else if(xhr.readyState < 4) {
document.getElementById("loaderCompany").style.display = "inline";
}
}
</script>
I don't know why i can't retrieve the list of company in ajax. I debugged it in Chrome console and the error is GET http://localhost:8060/Project/companyList.htm 404 (Not Found)
.
Someone Can help me to resolve the problem??.
Thanks.