Ever Since I posted my tutorial for creating web applications using MyBatis, Spring, Ext JS and Eclipse, traffic has flooded to the tutorial. In this post I am trying to give answers to most of the queries/questions through which users have been reaching my tutorial.
Note: Some of my answers are related to the terminology that I have introduced in the tutorial for simple understanding. So if you face any problems while understanding the answers, do read the tutorial here or post your question as a comment. I will try to answer it as soon as possible. It may even help others having the same doubt. So I highly encourage you to post any doubt related to the tutorial or these FAQs in the comments section.
Q. Java Web application structure in Eclipse Helios?
Ans: For developing web application in Eclipse Helios I suggest you to chose “Dynamic Web Project” and leave it to the IDE to chose default structure for building such applications. Alternatively, you can also chose “Maven Project” for developing which simplifies building the project by allowing us to write dependencies in pom.xml. You can then add the src files and Webcontent as explained in the tutorial. To create maven projects easily using eclipse, I use m2e plugin.
Q. How to use MyBatis when developing web app?
Ans: In order to initialize it you must set sqlSessionFactory as an application context attribute as I described in tutorial and add it to listener attribute in web.xml.
Q. How to pass parameter from Controller to view during compile time?
Ans: As far as I know, this is not possible. However, if you are trying to pass server side parameters to controller to view, ModelAndView object serves this purpose. If you want to send studentName to your view you can send it using
This means you pass name to your view and in your view you can access it using studentName.
Q. How to pass parameter from Controller to view during run time?
Ans: If you want to pass multiple parameter you can achieve this using JSON object just add this library to your java build path or add this lines for dependencies if you are using maven project:
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <type>jar</type> <classifier>jdk15</classifier> <scope>compile</scope> </dependency>
For sending parameters you ucan use:
response.setContentType("application/json;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); JSONObject jsonResult = new JSONObject(); jsonResult.put("studentName", name); response.getWriter().write(jsonResult.toString()); response.getWriter().close();
Q. How to install Tomcat and use it with Eclipse?
Ans: For this I suggest you to visit this. To debug web applications in eclipse that are running in tomcat, I use sysdeo eclipse tomcat launcher plugin.
Q. How to pass object of a class as a parameter in mybatis queries
Ans: I explained this in the tutorial in great depth. I suggest you to go through the tutorial once again.
Q. How to get rid of “FATAL ERROR: myBatis could not be initialized”
Ans: This error comes due to error in Mapper.xml in which you have written your SQL queries. I would suggest you to add System.out.println(e); in catch part of listener class.
Q. How to include listener class in web.xml?
Ans: To initialize listener class on application start up add following lines of code in web.xml (as used in my project/ replace com.batch2k7.listener.CustomServletContextListener with your class name)
<listener> <listener-class> com.batch2k7.listener.CustomServletContextListener </listener-class> </listener>