在Spring中将字符串从jsp传递给Controller

时间:2014-10-14 16:21:54

标签: spring hibernate jsp spring-mvc jstl

我使用Spring + JPA + Hibernate创建了一个项目。我想将一个字符串从jsp传递给controller(removeUtente方法)。如果我点击链接,我会从数据库中删除该行。我在哪里做错了?对不起我的英文..控制器从jsp接收字符串并调用方法将其删除。 谢谢

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Lista utenti</h2>
<table border="1">
<c:forEach var="utente" items="${lista }">
    <tr>
        <td>
            <c:out value="${utente.id}"/>
        </td>
        <td>
            <c:out value="${utente.cognome}"/>
        </td>
        <td>
            <c:out value="${utente.nome}"/>
        </td>
        <td>
            <c:out value="${utente.eta}"/>
        </td>
        <td>
            <c:url var="url" value="/remove">
                <c:param name="id" value="${utente.id }"/>
            </c:url>
            <a href="${url}">click to delete</a>
        </td>
    </tr>
 </c:forEach>
</table>
</body>
</html>

UtenteController.java

package controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import persistence.UtenteDAO;
import bean.Utente;

@Controller
public class UtenteController {

@Autowired
UtenteDAO utenteDAO;

@RequestMapping(value="/add",method=RequestMethod.POST)
public String addUtente(@ModelAttribute Utente u){
    utenteDAO.inserisciUtente(u);
    return "index";
}//addUtente

@RequestMapping(value="/show",method=RequestMethod.GET)
public String getUtenti(ModelMap model){
    List<Utente> lista=utenteDAO.listaUtenti();
    model.addAttribute("lista", lista);
    return "showlista";
}//getUtenti

@RequestMapping(value="/remove",method=RequestMethod.GET)
public String removeUtente(@RequestParam String id){
    utenteDAO.rimuovi(id);
    return "showlista";
}//removeUtente
}//UtenteController

Utente.java

package bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="utente")
public class Utente {

@Id
@GeneratedValue
@Column(name="id")
private Integer id;

@Column(name="cognome")
private String cognome;

@Column(name="nome")
private String nome;

@Column(name="eta")
private Integer eta;

public Integer getId(){return id;}//getId

public void setId(Integer id){this.id=id;}//setId

public String getCognome(){return cognome;}//getCognome

public void setCognome(String cognome){this.cognome=cognome;}//setCognome

public String getNome(){return nome;}//getNome

public void setNome(String nome){this.nome=nome;}//setNome

public Integer getEta(){return eta;}//getEta

public void setEta(Integer eta){this.eta=eta;}//setEta

@Override
public String toString(){
    return "id:"+id+" cogn:"+cognome+" nome:"+nome+" eta:"+eta;
}//toString

}//Utente

UtenteDAOImpl.java

package persistence;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import bean.Utente;

@Transactional
public class UtenteDAOImpl implements UtenteDAO {

@PersistenceContext
EntityManager em;

public void inserisciUtente(Utente u){
    em.persist(u);
}//inserisciUtente

@SuppressWarnings("unchecked")
public List<Utente> listaUtenti(){
    Query q=em.createQuery("SELECT u FROM Utente u");   
    List<Utente> ris=q.getResultList();
    return ris;
}//listaUtenti

public void rimuovi(String idUtente){
    Query q=em.createQuery("DELETE FROM Utente AS u WHERE u.id =:id");
    q.setParameter("id", idUtente);
    q.executeUpdate();
}//rimuovi

}//inserisciUtente

0 个答案:

没有答案