Java spring新手,无法使用hibernate删除用户

Java spring新手,无法使用hibernate删除用户,java,spring,java-ee-6,Java,Spring,Java Ee 6,我是spring的新手,正在做一个例子。该示例演示了如何使用hibernate将数据添加到表中并列出它,我尝试添加额外的删除选项,但改为添加其更新记录。我将在下面发布完整的代码。(2)我使用jsp和Servlet以及许多jQueryAjax开发简单的web应用程序,我想在spring中使用ajax,我是否能够以相同的方式编写代码,或者我必须学习一些不同的东西。坦率地说,看到春天的图案后,我很害怕 dispatcher-servlet.xml <?xml version="1.0" enco

我是spring的新手,正在做一个例子。该示例演示了如何使用hibernate将数据添加到表中并列出它,我尝试添加额外的删除选项,但改为添加其更新记录。我将在下面发布完整的代码。(2)我使用jsp和Servlet以及许多jQueryAjax开发简单的web应用程序,我想在spring中使用ajax,我是否能够以相同的方式编写代码,或者我必须学习一些不同的东西。坦率地说,看到春天的图案后,我很害怕

dispatcher-servlet.xml

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/production"/>
        <property name="username" value="tpsl_admin"/>
        <property name="password" value="admin"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.org.domain.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="myUserDAO" class="com.org.dao.UserDAOImpl">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    <bean name="/user/*.htm" class="com.org.web.UserController" >
        <property name="userDAO" ref="myUserDAO" />
    </bean>

    <!--
    The index controller.
    -->
    <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>
UserController.java

package com.org.web;
import com.org.dao.UserDAO;
import com.org.domain.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class UserController extends MultiActionController {

    private UserDAO userDAO;

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
        @RequestMapping(params = "add", method = RequestMethod.POST)
    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response, User user) throws Exception {
        userDAO.saveUser(user);
        return new ModelAndView("redirect:list.htm");
    }
        @RequestMapping(params = "delete", method = RequestMethod.POST)
        @Transactional
        public ModelAndView delete(HttpServletRequest request,
            HttpServletResponse response, User user) throws Exception {
        userDAO.deleteUser(user);
        return new ModelAndView("redirect:list.htm");
    }

    public ModelAndView list(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("userList", userDAO.listUser());
        modelMap.addAttribute("user", new User());
        return new ModelAndView("userForm", modelMap);
    }
}
好的,代码就是这样,我使用的ide是netbeans,sprinf版本是3.1.1。Hibernate3.0。有人请告诉我哪里我需要改变,我正在尝试,因为2天失败。如果可能的话,请解释我自己的代码。我有很多疑问

谢谢

userForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
.even {
    background-color: silver;
}
</style>
<title>Registration Page</title>
</head>
<body>

    <form:form action="add.htm" commandName="user" method="post">
    <table>

        <tr>
            <td>User Name :</td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:password path="password" /></td>
        </tr>
        <tr>
            <td>Gender :</td>
            <td><form:radiobutton path="gender" value="M" label="M" /> 
                            <form:radiobutton path="gender" value="F" label="F" /></td>
        </tr>
        <tr>
            <td>Country :</td>
            <td><form:select path="country">
                <form:option value="0" label="Select" />
                <form:option value="India" label="India" />
                <form:option value="USA" label="USA" />
                <form:option value="UK" label="UK" />
            </form:select></td>
        </tr>
        <tr>
            <td>About you :</td>
            <td><form:textarea path="aboutYou" /></td>
        </tr>
        <tr>
            <td>Community :</td>
            <td><form:checkbox path="community" value="Spring" label="Spring" /> 
                            <form:checkbox path="community" value="Hibernate" label="Hibernate" />
                            <form:checkbox path="community" value="Struts" label="Struts" />
                        </td>
        </tr>
        <tr>
            <td></td>
            <td><form:checkbox path="mailingList" label="Would you like to join our mailinglist?" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="add" value="Register"></td>
                        <td colspan="2"><input type="button" name="delete" value="Delete"></td>
        </tr>
    </table>
</form:form>
<c:if test="${fn:length(userList) > 0}">
    <table cellpadding="5">
        <tr class="even">
                        <th>ID</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Country</th>
            <th>About You</th>
                        <th>Mailing List</th>
                        <th>Community</th>
        </tr>
        <c:forEach items="${userList}" var="user" varStatus="status">
            <tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
                                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.gender}</td>
                <td>${user.country}</td>
                <td>${user.aboutYou}</td>
                                <td>${user.mailingList}</td>
                                <td>${user.community}</td>
            </tr>
        </c:forEach>
    </table>
</c:if>
</body>
</html>

.甚至{
背景颜色:银色;
}
注册页
用户名:
密码:
性别:
国家:
关于你:
社区:
身份证件
名称
性别
国家
关于你
邮件列表
社区
${user.id}
${user.name}
${user.gender}
${user.country}
${user.aboutYou}
${user.mailingList}
${user.community}

在您的dispatcherservlet.xml中,请对下面的行进行注释

    <prop key="hibernate.hbm2ddl.auto">create</prop>
创建

每当您重新启动服务器时,它都会在服务器启动时自动创建表。请检查正确的映射以删除用户,并设置打印或断点并调试问题,查看您的代码hibernateTemplate.delete(user)应该可以正常工作。

您遇到了什么错误?显示错误和
userForm.jsp
,也许你的url映射错误添加你的打印桩跟踪,因此获取错误类型和行号可能值得查看Matt Raible的或至少相关的,以了解事物是如何联系在一起的,以及可用的不同框架如何帮助你构建应用程序。我没有收到任何错误,只是删除正在更新而不是删除用户,当我重新启动(关闭并打开)应用程序时,数据库中的数据也会丢失。我将在上面的问题中附加userForm.jsp。谢谢你
package com.org.domain;

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

@Entity
@Table(name="USER")
public class User {

    private Long id;
    private String name;
    private String password;
    private String gender;
    private String country;
    private String aboutYou;
    private String[] community;
    private Boolean mailingList;

    @Id
    @GeneratedValue
    @Column(name="USER_ID")
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @Column(name="USER_NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Column(name="USER_PASSWORD")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name="USER_GENDER")
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

    @Column(name="USER_COUNTRY")
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

    @Column(name="USER_ABOUT_YOU")
    public String getAboutYou() {
        return aboutYou;
    }
    public void setAboutYou(String aboutYou) {
        this.aboutYou = aboutYou;
    }

    @Column(name="USER_COMMUNITY")
    public String[] getCommunity() {
        return community;
    }
    public void setCommunity(String[] community) {
        this.community = community;
    }

    @Column(name="USER_MAILING_LIST")
    public Boolean getMailingList() {
        return mailingList;
    }
    public void setMailingList(Boolean mailingList) {
        this.mailingList = mailingList;
    }

}
package com.org.web;
import com.org.dao.UserDAO;
import com.org.domain.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class UserController extends MultiActionController {

    private UserDAO userDAO;

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
        @RequestMapping(params = "add", method = RequestMethod.POST)
    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response, User user) throws Exception {
        userDAO.saveUser(user);
        return new ModelAndView("redirect:list.htm");
    }
        @RequestMapping(params = "delete", method = RequestMethod.POST)
        @Transactional
        public ModelAndView delete(HttpServletRequest request,
            HttpServletResponse response, User user) throws Exception {
        userDAO.deleteUser(user);
        return new ModelAndView("redirect:list.htm");
    }

    public ModelAndView list(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("userList", userDAO.listUser());
        modelMap.addAttribute("user", new User());
        return new ModelAndView("userForm", modelMap);
    }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
.even {
    background-color: silver;
}
</style>
<title>Registration Page</title>
</head>
<body>

    <form:form action="add.htm" commandName="user" method="post">
    <table>

        <tr>
            <td>User Name :</td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:password path="password" /></td>
        </tr>
        <tr>
            <td>Gender :</td>
            <td><form:radiobutton path="gender" value="M" label="M" /> 
                            <form:radiobutton path="gender" value="F" label="F" /></td>
        </tr>
        <tr>
            <td>Country :</td>
            <td><form:select path="country">
                <form:option value="0" label="Select" />
                <form:option value="India" label="India" />
                <form:option value="USA" label="USA" />
                <form:option value="UK" label="UK" />
            </form:select></td>
        </tr>
        <tr>
            <td>About you :</td>
            <td><form:textarea path="aboutYou" /></td>
        </tr>
        <tr>
            <td>Community :</td>
            <td><form:checkbox path="community" value="Spring" label="Spring" /> 
                            <form:checkbox path="community" value="Hibernate" label="Hibernate" />
                            <form:checkbox path="community" value="Struts" label="Struts" />
                        </td>
        </tr>
        <tr>
            <td></td>
            <td><form:checkbox path="mailingList" label="Would you like to join our mailinglist?" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="add" value="Register"></td>
                        <td colspan="2"><input type="button" name="delete" value="Delete"></td>
        </tr>
    </table>
</form:form>
<c:if test="${fn:length(userList) > 0}">
    <table cellpadding="5">
        <tr class="even">
                        <th>ID</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Country</th>
            <th>About You</th>
                        <th>Mailing List</th>
                        <th>Community</th>
        </tr>
        <c:forEach items="${userList}" var="user" varStatus="status">
            <tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
                                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.gender}</td>
                <td>${user.country}</td>
                <td>${user.aboutYou}</td>
                                <td>${user.mailingList}</td>
                                <td>${user.community}</td>
            </tr>
        </c:forEach>
    </table>
</c:if>
</body>
</html>
    <prop key="hibernate.hbm2ddl.auto">create</prop>