Spirng MVC中常用的注解

1、@Controller
告诉Ioc容器该类是一个Controller
例子:
@Controller
public class AddressController {
2、@Autowired
@Autowired 注解可以用于“传统的”setter 方法,如下例:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
    }

    // ...
}
这个注解也可以用于以属性为参数/多个参数的方法

public class MovieRecommender {

    private MovieCatalog movieCatalog;
   
    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}
@Autowired注解甚至可以用于构造器与字段:

public class MovieRecommender {

    @Autowired
    private MovieCatalog movieCatalog;
   
    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}
也可以一种提供来自ApplicationContext的特殊类型的所有 beans,注解字段或者方法,例如:

public class MovieRecommender {

    @Autowired
    private MovieCatalog[] movieCatalogs;

    // ...
}

3、@RequestMapping
根据路径映射到不同的方法上
例子:
@RequestMapping(value = "/member/listaddress.html", method = RequestMethod.GET)
public String getMyAdresses(Model m, HttpServletRequest request) {
4、@PathVariable
获得路径上的参数
例子:
@RequestMapping(value = "/member/address/{id}.html", method = RequestMethod.GET)
public String getEditMyAdresses(@PathVariable("id") String id, Model m, HttpServletRequest request) {
5、@RequestParam
获得request的参数(这个参数是http协议中的消息内容)
例子:
@RequestMapping(value = "/member/updateaddressui.html", method = RequestMethod.GET)
public String updateAddressUI(Model m, HttpServletRequest request, @RequestParam("sid") String sid, @ModelAttribute MemReceptInfoVO memReceptInfoVO) {
6、@ModelAttribute
从servlet的四个容器中获得属性(page,request,session,application)
例子:
public String addMyAddress(Model m, HttpServletRequest request, @ModelAttribute MemReceptInfoVO memReceptInfoVO) {

你可能感兴趣的:(html,mvc,servlet,IOC)