0

enter image description here

above is my Spring:4 MVC application with Hibernate integration.

from index page user goes to findUser.jsp by the following controller:

@RequestMapping(value = "/findUser", method = RequestMethod.GET)
public String findUserByName(@ModelAttribute("user") User user, BindingResult result, Model model) {
        model.addAttribute("user", user);
        return "findUser";
    }

the findUser.jsp is as follow:

<body>
    <form:form action="UserByName" method="post" modelAttribute="user">
        <table>
            <tr>
                <td><form:label path="id"> ID </form:label></td>
                <td><form:input path="id" /></td>
            </tr>           
            <tr>
                <td><input type="submit" value="Login" /></td>
            </tr>
        </table>
    </form:form>
</body> 

when the user enter and ID and hit the submit button it hits the controoler :

@RequestMapping(value = "/UserByName", method = RequestMethod.POST)
public String UserByName(@PathVariable("id") int id, Model model) {     
        model.addAttribute("user", this.userService.findByName(id));        
        return "usersByName";
    }

and UserService impl is

@Service
public class UserServiceImpl implements UserService {   
    @Autowired
    UserDao userDao;
    public boolean save(User user) {        
        return userDao.save(user);
    }   
    public int update(User user) {      
        return userDao.update(user);
    }
    @Transactional
    public User findByName(int id) {    
            return this.userDao.findByName(id);             
    }
} 

the method of userDao in userDaoImpl is:

public User findByName(int id) {         
         //User user = new User();       
         Session session = this.sessionFactory.getCurrentSession();      
         User u = (User) session.get(User.class, new Integer(id));       
         return u;
}

finally the controller display the usersByName.jsp which is:

<body>
<form:form modelAttribute="user">
    <c:forEach items="${user}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.name}</td>
            <td>${useremail}</td>           
            </tr>           
            </c:forEach>            
            </form:form>
</body>

am using Hibername and mySQL databse my model class is:

@Entity@Table(name = "USER")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "PASSWORD")
    private String password;
    @Column(name = "USER_ID")
    private String userId;
    @Column(name = "EMAIL")
    private String email;   
    @Column(name = "ROLE")
    private String role;
    @Column(name = "DEPARTMENT")
    private String department;

ApplicationContext is

     @ Configuration
        @ComponentScan("com.sat.spring")
        @EnableTransactionManagement
        public class A

pplicationContextConfig {
        @Bean(name = "viewResolver")
        public InternalResourceViewResolver getViewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setPrefix("/WEB-INF/views/");
            viewResolver.setSuffix(".jsp");     
            return viewResolver;
        }

        @Bean(name = "dataSource")  
        public DataSource getDataSource() {
            BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");     
            dataSource.setUrl("jdbc:mysql://localhost:3306/UniversityProject");
            dataSource.setUsername("root");
            dataSource.setPassword("root");
            return dataSource;
        }

        private Properties getHibernateProperties() {
            Properties properties = new Properties();
            properties.put("hibernate.show_sql", "true");
            properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");      
            return properties;
        }

        @Autowired
        @Bean(name = "sessionFactory")
        public SessionFactory getSessionFactory(DataSource dataSource) {
            LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);     
            sessionBuilder.addProperties(getHibernateProperties());     
            sessionBuilder.addAnnotatedClasses(User.class);     
            return sessionBuilder.buildSessionFactory();
    }
        @Autowired
        @Bean(name = "transactionManager")
        public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {       
            HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
            return transactionManager;
        }
        @Autowired
        @Bean(name = "userDao")
        public UserDao getUserDao(SessionFactory sessionFactory) {
            return new UserDaoImpl(sessionFactory);
        }
    }

what I am doing wrong?? I want the usersByName.jsp to display all user's info for that specific id from the USER mySQL database.

javaz
  • 81
  • 2
  • 4
  • 14
  • 1
    We need a crue about your error or a copy of your project... The project compile? Run? The tables are properly defined? Where are the search implementation? Show the exception , etc.. – Vitorlui Oct 20 '16 at 20:07
  • Everything looks fine. What is happening or what error are you getting? – Jerin Joseph Oct 20 '16 at 21:47
  • `user` is a reserved name in MySQL (see [here](http://dev.mysql.com/doc/refman/5.7/en/keywords.html). Easier/Better to pick another table name. If you really must/want (which I don't recommend as using reserved words as table names is trouble waiting to happen) see http://stackoverflow.com/questions/22256124/cannot-create-a-database-table-named-user-in-postgresql . – M. Deinum Oct 20 '16 at 22:06
  • Tomcat was stating "Don't know how to iterate over supplied "items" in <forEach> .".....Also, SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/UniProject08] threw exception.... – javaz Oct 21 '16 at 13:48

0 Answers0