Spring Basic Concepts

springmvc

// SpringMVC - Basic Concepts

@Override
public int hashCode() {
    return id;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Todo other = (Todo) obj;
    if (id != other.id)
        return false;
    return true;
}

Front Controller Pattern -> Dispatcher Servlet
Component scan
@Service and @Autowired are for dependency injection
ModelMap: This is basically the model object which is a way for controller to 
    pass data to the view.
The "application context" configuration file
Form and data binding using the command / model object
InitBinder: consistently format a field or a class

@Controller
@RestController

View Resolver: ( org.springframework.web.servlet.view.InternalResourceViewResolver ), 
    configured inside the application context configuration file, with the prefix of 
    /WEB-INF/views/ and the suffix of jsp. This allows the controller method to 
    return just a short string containing just the name of the JSP file, and omitting 
    the prefix and suffix.

Create the model class.  The model class contains just the private member variables,
    the setter and getter methods, the equals method, and the hashCode method.  For
    the hashCode method, if the object has an ID field, and it is of type int, we 
    can just return that. We should use Eclipse to generate the stubs for hashCode
    and equals methods.

Create the service class corresponding to the model class.  The service class would
    contain methods that are responsible for creating the model instances, deleting
    the model instances, and retrieving the model instances from the database.

To send a redirect, the controller method would:

return "redirect:list-todos"

When we do a redirect using "redirect:", Spring MVC automatically serialize all 
the attribute values in the model map and append that to the URL. To prevent that, 
we need to invoke model.clear().

@RequestMapping // Specify the URL and HTTP method for a particular.  For example:
@RequestMapping(value = "/login", method = RequestMethod.GET)
@RequestMapping(value = "/login", method = RequestMethod.POST)
@RequestMapping(value = "/todo/{id}", method = RequestMethod.GET)
                // Use @PathVariable("id") int id
                // to pass this value to the controller method
@ResponseBody   // Treat the return value of a controller method as the body of the response
@RequestParam   // Pass request parameter to a controller method
                // @RequestParam String password

@SessionAttributes("name")      // This annotation should be added to the controller class
                // Whatever we put into the model map instance under the same name will
                // also be automatically putted into the session object.  For example:
                // model.put("name",name) will also put the user's name into the session
                // object under the key "name".  This is also how our controller method
                // can retrieve a value from the session (retrieve it from the model map
                // object with model.get.

@SessionAttributes({"attributeA", "attributeB", "attributeC"} )

When we add the @ResponseBody annotation to a method in our controller, this 
annotation indicates that the return value of our method should be treated as 
the body of the response. If we do not add this annotation, the Spring MVC 
dispatcher servlet will treat the return value as the name
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License