-1

我想我已经在这个主题下引用了谷歌中的几乎所有链接,并且已经在这个网站上发布了两个关于这个错误的问题,但仍然找不到解决这个问题的方法……我正在尝试一些春季样品,但我被困住了一个示例程序如下

我的 POJO 类如下

用户.java

package project2;
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;


    @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;
    }

   }

我的 UserDAO 接口类如下

用户DAO.java

package project2;
import project2.User;
import java.util.List;

public interface UserDAO {

    public void saveUser(User user) ;
    public List<User> listUser() ;
    public void deleteUser(User user) ;
    public List<User> findUser(User user);
    public void updateUser(User user);
}

我正在使用休眠模板。

UserDAOImpl.java

package project2;
import project2.User;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class UserDAOImpl implements UserDAO {

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    public void saveUser(User user) {
        hibernateTemplate.saveOrUpdate(user);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<User> listUser() {
        return hibernateTemplate.find("from user");
    }

    @Override
    public void deleteUser(User user) {
         hibernateTemplate.delete(user);
    }

    @Override
    public void updateUser(User user) {
        hibernateTemplate.update(user);

    }
}

我的控制器类如下

用户控制器2.java

package project2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
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;

@Controller
public class UserController2{

    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");
          //i tried with ModelAndView("redirect:user.htm") as well
    }
    @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);
    }
 }

我的jsp页面如下

用户窗体.jsp

<body>
 <center> WELCOME TO CUSTOMER ACCESS SITE.PLEASE ENTER THE FOLLOWING 
  INFORMATION</center>
<form:form method="POST" action="add.htm"  modelAttribute="user">
   <table>
   <tr>
    <td><form:label path="name">Name</form:label></td>
    <td><form:input path="name" /></td>
</tr>

<tr>
    <td><form:label path="id">id</form:label></td>
    <td><form:input path="id" /></td>
</tr>

<tr>
    <td><form:label path="password">password</form:label></td>
    <td><form:input path="password" /></td>
</tr>
<tr>
    <td><form:label path="gender">Gender</form:label></td>
    <td><form:input path="gender" /></td>
</tr>
<tr>
    <td><form:label path="country">Country</form:label></td>
    <td><form:input path="country" /></td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" value="Submit"/>
    </td>
</tr>
 </table>  
</form:form>

</body>

我的 list.jsp 文件如下

<body>
<form:form method="POST"   modelAttribute="list">
  // i tried without the form tag as well
<table border="1">
 <tr>
   <th>Id: </th><td>${list.id}</td>
 </tr>
 <tr>
   <th>Name: </th><td>${list.name}</td>
       //i tried with ${user.name} as well
 </tr>
 <tr>
    <th>Age: </th><td>${list.age}</td>
 </tr>
 <tr>
    <th>Password: </th><td>${list.password}</td>
 </tr>
 <tr>
   <th>Gender</th><td>${list.gender}</td>  
 </tr>     
 <tr>
    <th>Country</th><td>${list.country}</td>  
 </tr>       

</table>
</form:form>
</body>

我已经在jsp中添加了所需的spring taglib

和spring xml文件如下

UserController2-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"/>


<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/indi"/>
    <property name="username" value="admin"/>
    <property name="password" value=""/>
</bean>

<bean id="mySessionFactory"   
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>Spring.project2.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="project2.UserDAOImpl">
     <property name="sessionFactory" ref="mySessionFactory"/>
</bean>

 <bean name="/user/*.htm" class="project2.UserController2" >
    <property name="userDAO" ref="myUserDAO" />
</bean>


 <bean name="indexController" lass="org.springframework.web.servlet.mvc.Parameterizab  
      leViewController" p:viewName="userForm" />

</beans>

而web.xml文件如下

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>UserController2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
 </servlet>
<servlet-mapping>
     <servlet-name>UserController2</servlet-name>
     <url-pattern>*.htm</url-pattern>
</servlet-mapping>

我的错误如下

org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither  
BindingResult nor plain target object for bean name 'user' available as request 
attribute

我还对此链接中指定的控制器类进行了一些修改

不断得到: Bean 名称“索引”的 BindingResult 和普通目标对象都不能用作请求属性

修改后的控制器类如下

用户控制器2.java

@Controller
@RequestMapping(value="/user")
     //also tried with @RequestMapping("user") and @RequestMapping("user.htm")

public class UserController2{

    private UserDAO userDAO;

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

        @RequestMapping(params = "add", method = RequestMethod.POST)
    public ModelAndView add( @ModelAttribute("add") User user,HttpServletRequest  
          request,HttpServletResponse response) throws Exception {
        userDAO.saveUser(user);
        System.out.println("hai");
        return new ModelAndView("redirect:list.htm");
    }

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


        @RequestMapping(params = "find", method = RequestMethod.POST)
        @Transactional
    public ModelAndView find(@ModelAttribute("find") User user,HttpServletRequest  
           request,HttpServletResponse response) throws Exception {  
                     userDAO.findUser(user);
                        return new ModelAndView("redirect:list.htm");
                }


    @RequestMapping(params = "update", method = RequestMethod.POST)
    @Transactional
    public ModelAndView update(@ModelAttribute("update") User user,HttpServletRequest  
           request,HttpServletResponse response) throws Exception {  
                     userDAO.updateUser(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);
    }

}

我尝试按照上一篇文章的建议添加一个虚拟用户,但仍然相同,我尝试在 jsp 页面中使用 commandName 而不是 modelAttribute,但错误仍然存​​在。modelAttribute 和注释参数值完全相同。

老实说,我不知道我要去哪里错了。有人可以帮忙吗?

4

1 回答 1

0

在每个 @ModelAttribute 参数之后,Spring 在下一个参数位置查找 BindingResult(顺序很重要)。所以这可能有效。

public ModelAndView find(@ModelAttribute("find") User user,BindingResult bindingResult, HttpServletRequest  
       request,HttpServletResponse response) throws Exception {  
                 userDAO.findUser(user);
                    return new ModelAndView("redirect:list.htm");
            }
于 2013-04-19T13:46:04.877 回答