google Analytics

Wednesday, September 15, 2010

Spring 3.0 @Controller Overview

@Controller
Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications,
through the @PathVariable annotation and other features
Spring will use any class annotated with the @Controller annotation as a Controller in Spring MVC support.


=============================================================
 

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 *
@author NathA1
Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications,
through the @PathVariable annotation and other features
Spring will use any class annotated with the @Controller
annotation as a Controller in Spring MVC support.
 */
 

@Controller
/**
The @RequestMapping annotation is used to map URLs to the Controller class or aparticular method
If you want to use the @RequestMapping annotation at the method level,
you need to configure DefaultAnnotationHandlerMapping. This is done
by default for DispatcherServlet, but if you are implementing your own handler adapter, you will
have to define AnnotationMethodHandlerAdapter

@RequestMapping Configuration  in Spring

<?xml version="1. 0" encoding="UTF-8" ?>
<context: component-scan base-package="com.test.web" />
<bean class="org.springframework.web.➥
servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org. springframework. web. ➥
servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

 */
@RequestMapping(value="/account")

public class AccountController {

    private Map<Long, Account> accounts = new ConcurrentHashMap<Long, Account>();
   
    @RequestMapping(method=RequestMethod.GET)
    public String getCreateForm(Model model) {
        model.addAttribute(new Account());
        return "account/createForm";
    }
   
    @RequestMapping(method=RequestMethod.POST)
    public String create(@Valid Account account, BindingResult result) {
        if (result.hasErrors()) {
            return "account/createForm";
        }
        this.accounts.put(account.assignId(), account);
        return "redirect:/account/" + account.getId();
    }
   
    @RequestMapping(value="{id}", method=RequestMethod.GET)
    public String getView(@PathVariable Long id, Model model) {
        Account account = this.accounts.get(id);
        if (account == null) {
            throw new ResourceNotFoundException(id);
        }
        model.addAttribute(account);
        return "account/view";
    }

}

==========================================================================


ANish

No comments:

Post a Comment