THere is quite a lot 'wrong' with your setup.
YOur managers, that code makes me shudder. Don't write code like that..
For starters your dao shouldn't expose the sessionfactory, next to that you shouldn't be messing around with transactions yourself! Put a @Transactional at the top of your manager and remove all of the clutter. Currently your manager/service is also coupled to the web layer due to your exception throwing, something else you really don't want. Which will leave you with the following.Code:public List<Category> getListCategory() throws ServletException { List<Category> categories = null; try { productDao.sessionFactory.getCurrentSession().beginTransaction(); categories = productDao.getListCategory(); productDao.getSessionFactory().getCurrentSession().getTransaction().commit(); } catch (Throwable e) { if (productDao.getSessionFactory().getCurrentSession().getTransaction().isActive()) { productDao.getSessionFactory().getCurrentSession().getTransaction().rollback(); } throw new ServletException(e); } return categories; }
Now isn't that a lot better and easier to maintain.Code:@Transational public class ProductManager public List<Category> getListCategory() { return productDao.getListCategory(); }
The same goes for your dao's they are also coupled to the web stuff, ServletException again, and also one thing you NEVER should do is to prepare queries with adding strings together, this makes your vulnerable to sql injection. Use setparameter on the query object for that.
should be something like.Code:public List<Product> getSearchProduct(String searchWord) { return sessionFactory.getCurrentSession().createQuery("from Product WHERE name LIKE '%" + searchWord + "%' OR description LIKE '% " + searchWord + "%'").list(); }
FOllowing as I already suggested you are duplicating things in your xml.Code:public List<Product> getSearchProduct(String searchWord) { Query query = sessionFactory.getCurrentSession().createQuery("from Product WHERE name LIKE :searchword OR description LIKE :searchword"); query.setParameter("searchword", "%"+searchWord+"%"); return query.list(); }
The second declaration '/delivery_form.html'does absolutly nothing. It only takes memory. YOu can remove all those beans... Saves/cuts your configuration in half.Code:<bean id="DeliveryFormController" class="com.jshop.web.DeliveryFormController"> <property name="deliveryFormManager" ref="DeliveryFormManager" /> </bean> <bean name="/delivery_form.html" class="com.jshop.web.DeliveryFormController" />
Your controllers are also doint ugly things, I wonder why you have a manager/dao layer at all?!
Argh..... That should ONLY readCode:try { productManager.getProductDao().sessionFactory.getCurrentSession().beginTransaction(); products = productManager.getProductDao().getListProduct(); productManager.getProductDao().getSessionFactory().getCurrentSession().getTransaction().commit(); } catch (Throwable e) { if (productManager.getProductDao().getSessionFactory().getCurrentSession().getTransaction().isActive()) productManager.getProductDao().getSessionFactory().getCurrentSession().getTransaction().rollback(); throw new ServletException(e); }
And you would have the manager delegate it. You don't want that code.....Code:products = productManager.getListProducts();
Another note, you are using system.out for logging, another thing that isn't really nice/neat to do. You should use a logging framework (I expected either the use of commons-logging or slf4j judging from the jars in your project). System.out is slow, it is really slow, so if you use it a lot it will degrade your performance.
Next remove the component:scan because that is pretty much useless you don't have @Components/@Controller/@Service in your project, so no need for that.
You don't program to interfaces so your tx:annotation-driven needs to proxy-target-classes. Currently you don't have any (proper) transactions setup, hence your errors.
Move your validator, viewresolver and messagesource to the servlet.xml.
Haven't tried it but that should start your application and if you also apply the other things I mentioned your application will be easier to maintain.
I really hope that this isn't code that goes to production, because if I where a developer on the project I wouldn't give any guarantees for the quality, stability of the product.
If you only want it to get running do
1) add @Transactional to your managers
2) enable proxy-target-class on tx:annotation-driven
3) Move your validator, viewresolver and messagesource to the servlet.xml.
that should basically be it...
However if you want to improve the overal structure/architecture of your app, well take the above into account.




