Spring mvc + ajax

4 respostas
V

Bom dia, galera!

Estou fazendo os exercicios da apostila FJ-21 da caelum e travei no 11.21(exercicios: ajax).

Bom, a minha dificuldade é adicionar no arquivo spring-context.xml essa linha:

Em todos os lugares que eu coloquei, deu um erro no console. E eu acho que a minha função não funcionou por causa disso.

lista.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>

<head>
	<script type="text/javascript" src="/resources/js/jquery.js"></script>
	
	<script type="text/javascript">
	function finalizaAgora(id) {
		$.post("finalizaTarefa", {'id' : id}, function() {
		$("#tarefa_"+id).html("Finalizado");
		});
	}
	</script>
</head>	

<body>

<a href="novaTarefa">Criar nova tarefa</a>
<br /> 
<br />
<table>
<tr>
<th>Id</th>
<th>Descrição</th>
<th>Finalizado?</th>
<th>Data de finalização</th>
</tr>
<c:forEach items="${tarefas}" var="tarefa">
<tr>
<td>${tarefa.id}</td>
<td>${tarefa.descricao}</td>
<c:if test="${tarefa.finalizado eq false}">
	<td id="tarefa_${tarefa.id}"><a href="#" onClick="finalizaAgora(${tarefa.id})">Finaliza agora!</a></td>
</c:if>
<td><fmt:formatDate value="${tarefa.dataFinalizacao.time}" pattern="dd/MM/yyyy"/></td>
<td><a href="mostraTarefa?id=${tarefa.id}">Alterar</a></td>
<td><a href="removeTarefa?id=${tarefa.id}">Remover</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
tarefasControllers.java
@RequestMapping("finalizaTarefa")
	public void finaliza(Long id, HttpServletResponse response) {		
		TarefaDAO dao = new TarefaDAO();
		dao.finaliza(id);
		response.setStatus(200);
	}
TarefaDAO
public void finaliza(Long id){
		
		Tarefa tarefa = new TarefaDAO().buscaPorId(id);
		
		String sql = "update tarefas set finalizado=?, dataFinalizacao=? where id=?";
		
		try{
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			
			stmt.setBoolean(1, true);
			
			stmt.setDate(2, new Date(Calendar.getInstance().getTimeInMillis()));
			
			stmt.setLong(3, tarefa.getId());
				
			stmt.execute();
			stmt.close();
			
			System.out.println("DADOS ALTERADOS COM SUCESSO!");
		}catch(SQLException e){
			throw new RuntimeException(e);
		}				
	}
spring-contex.xml(onde acrescento o ?)
<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<context:component-scan base-package="br.com.caelum.tarefas" />
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="/WEB-INF/mensagens" />
	</bean>	 
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>fj21-tarefas</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

Obrigado desde já,
abrassss!

4 Respostas

leonardoteles

olá, boa tarde!!

vc conseguiu resolver esse problema?..estou parado no mesmo exercício que vc parou!!

poderia me ajudar, caso tenha conseguido?!?

wfuertes

Verifica:

Não seria:
Obs: sem a barra( / )?

wagnerfrancisco

Postem aqui qual o erro que está dando (o que está sendo impresso no console).

leonardoteles

E aê galera, boa tarde!!

Depois de tanto catar informação na net, consegui resolver, em partes, esse problema…o que tá rolando comigo agora é que quando clico pra finalizar uma tarefa…o campo “id” muda pra finalizado, e não a coluna certa…agora não tenho como postar o código, mas postarei aqui mais tarde!!!

Criado 22 de agosto de 2012
Ultima resposta 21 de nov. de 2012
Respostas 4
Participantes 4