Spring-Spring MVCNo Comments

The following example show how to write a simple web based application which makes use of HTML forms using Spring Web MVC framework.

Here is the content of Student.java file:

Following is the content of StudentController.java file:

Here the first service method student(), we have passed a blank Student object in the ModelAndView object with name “command” because the spring framework expects an object with name “command” if you are using <form:form> tags in your JSP file. So when student() method is called it returns student.jsp view.

Second service method addStudent() will be called against a POST method on the HelloWeb/addStudent URL. You will prepare your model object based on the submitted information. Finally a “result” view will be returned from the service method, which will result in rendering result.jsp

Following is the content of Spring Web configuration file web.xml

Following is the content of another Spring Web configuration file HelloWeb-servlet.xml

Following is the content of Spring view file student.jsp

Following is the content of Spring view file result.jsp

Finally, follwing is the list of Spring and other libraries to be included in your web application. You simply drag these files and drop them inWebContent/WEB-INF/lib folder.

  1. commons-logging-x.y.z.jar
  2. org.springframework.asm-x.y.z.jar
  3. org.springframework.beans-x.y.z.jar
  4. org.springframework.context-x.y.z.jar
  5. org.springframework.core-x.y.z.jar
  6. org.springframework.expression-x.y.z.jar
  7. org.springframework.web.servlet-x.y.z.jar
  8. org.springframework.web-x.y.z.jar
  9. spring-web.jar

Once you are done with creating source and configuration files, export your application. Right click on your application and use Export > WAR File option and save your SpringWeb.war file in Tomcat’s webapps folder.

Now start your Tomcat server and make sure you are able to access other web pages from webapps folder using a standard browser. Now try a URL http://localhost:8080/SpringWeb/student and you should see the following result if everything is fine with your Spring Web Application:

After submitting required information click on submit button to submit the form. You should see the following result if everything is fine with your Spring Web Application:

That’s how to Form Handling in Spring MVC and if you have any questions, feel free to ask me 🙂


Be the first to post a comment.

Add a comment