Reading Notes for Spring 3 Core Components Tutorial Part VI (Chapter 22)

  |   Source

This post is transferred from my old Hexo blog site, created on 2014.

This is a reading note from Spring Framework 3.1 Tutorial pdf, created by tutorialspoint. This pdf contains information for Spring 3 Core Basics, which is very useful for understanding defination and practive in Spring programming.

This note includes Chapter 22 in tutorial.

Spring Web MVC Framework

The Dispacher Servlet


Required Configuration


<!--First: You set up dispacher servlet in web.xml-->
<web-app id="testWebApp">
    <display-name>Test Web App</display-name>
        <!--Since we define TestServelt as dispacther servlet here, Spring will now try to find a servlet called TestServlet-servlet.xml in WebContent/WEB-INF directory-->

    <!--servlet-mapping here means all jsp files should be handled by TestServlet-->

    <!--You can also define your own servlet name instead of [servlet-name]-servelet.xml-->
    <!--make sure you add this listener if you want to define your own-->

<beans xmlns="...">
    <!--Now we create beans and set up ViewResolver, using [servlet-name]-servlet.xml-->
    <context:compont-scan base-package="com.sample"/>
    <!--The line above means we want search Spring MVC Annotation such as @Controller or @RequestMapping in com.sample package-->

    <!--We want to search for /WEB-INF/jsp folder to get .jsp files to resolve view names. For example, a logical view named hello should be a /WEB-INF/jsp/hellp.jsp-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolover">
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp"/>


You need @Controller annotation to make a controller class, and use @RequestMapping to get path and define type of request.


public class testController{
    @RequestMapping(value="/test", method=RequestMethod.GET)
    public String printHello(ModelMap model){
        model.addAttribute('message','Hello World!'); //Use ${message} in jsp file to get model attribute
        return "index"; //Return a view and string is the logical view name, which is defined in ViewResolver above

Form Submmision Example


<form:form method="POST" action="/HelloWorld/Student" modelAttribute="Person">
<!--You can also do action="addStudent", which is corresponding controller-->
            <td><form:label path="name">Name</form:label></td>
            <td><form:input path="name"/></td>
            <td><form:label path="age">Age</form:label></td>
            <td><form:input path="age"/></td>
            <td><form:label path="id">Id</form:label></td>
            <td><form:input path="id"/></td>
            <td colspan="2"><input type="commit" value="Submit"/></td>


//Assume we hava web.xml, testServlet-servlet.xml, as example above

public class testController{

    //This step has other ways to implement it
    @RequestMapping(value="/student", method=RequestMethod.GET)
    public ModelAndView student(){
        return new ModelAndView("student","studentModel",new Student());
        //Here we first create a Student object and go to student view(i.e student.jsp).
        //Note "studentModel" is a model idetifier. Spring use it to recognize "<form:form>" value"

    public String addStudent(@ModelAttribute("studentModel")Student student, ModalMap model){
        //Since we have set studentModel is a Student object, Spring get attributes from form, and set it to a Student object.
        //So now you can directly use student.getname();
        model.addAttribute("name", student.getname());
        //age, id is the same

        return "result"; //Now you can use ${name}, ${age} and ${id} in result.jsp
Comments powered by Disqus