Bean名称用户的绑定和普通目标对象都不可用作请求属性

时间:2013-04-19 12:29:46

标签: spring spring-mvc

我想我已经在这个主题下引用了谷歌中的几乎所有链接,并在此网站上发布了关于此错误的两个问题,但仍无法找到解决此问题的方法...我正在尝试使用少量弹簧样本而且我是坚持这个示例程序如下

我的POJO课程如下

User.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接口类如下

UserDAO.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);
}

我正在使用hibernate模板。

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);

    }
}

我的控制器类如下

UserController2.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页面如下

UserForm.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>

我已将所需的spring taglib添加到jsp

和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

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

Keep getting: Neither BindingResult nor plain target object for bean name 'index' available as request attribute

,修改后的控制器类如下

UserController2.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但是错误仍然存​​在.modeAttribute和注释参数值正好是相同。

老实说,我不知道自己哪里出错了。有人帮忙......

1 个答案:

答案 0 :(得分: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");
            }