11-SpringBoot——Spring MVC基础-常用注解

Spring MVC基础-常用注解


【博文目录>>>】


【项目源码>>>】


【常用注解】


Spring MVC 常用以下几个注解。

(1) @Controller
@Controller 注解在类上,表明这个类是Spring MVC 的Controller,将其声明为Spring的一个Bean, Dispatcher Servlet 会自动扫描注解了此注解的类并将Web 请求映射到注解了@RequestMapping 的方法上。这里特别指出,在声明普通Bean的时候,使用@Component、@Service 、@Repository 和@Controller 是等等的,因为@Service 、@Repository、@Controller 都组合了@Compoment 元注解;但在Spring MVC 声明控制器Bean 的时候,只能使用@Controller。

(2)@RequestMapping
@RequestMapping 注解是用来映射Web 请求(访问路径和参数)、处理类和方法的。@RequestMapping 可以注解在类或方法上。注解在方法上的@RequestMapping 路径会继承注解在类上的路径,@RequestMapping 支持Serviet 的request 和response 作为参数,也支持对request和response 的媒体类型进行配置。

(3)@Response Body
@ResponseBody 支持将返回值放在response 体内,而不是返回一个页面。我们在很多基于Ajax 的程序的时候,可以以此注解返回数据而不是页面:此注解可放置在返回值前或者方法上。

(4)@RequestBody
@RequestBody 允许request 的参数在request 体中,而不是在直接链接在地址后面。此注解放置在参数前。

(5)@Path Variable
@Path Variable 用来接收路径参数,如/news/001 ,可接收001作为参数,此注解放置在参数前。

(6)@RestController
@RestController 是一个组合注解,组合了@Controller 和@ResponseBody ,这就意味着当你只开发一个和页面交互数据的控制的时候,需要使用此注解。若没有此注解,要想实现上述功能,则需自己在代码中加@Controller 和@ResponseBody 两个注解。

【代码实现】


package com.example.spring.framework.mvc.annotation.domain;

/**
 * Author: 王俊超
 * Date: 2017-07-11 22:07
 * All Rights Reserved !!!
 */
public class DemoObj {
    private Long id;
    private String name;

    public DemoObj() {
        super();
    }
    public DemoObj(Long id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
package com.example.spring.framework.mvc.annotation.web;

import com.example.spring.framework.mvc.annotation.domain.DemoObj;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Author: 王俊超
 * Date: 2017-07-11 22:10
 * All Rights Reserved !!!
 */
@Controller // 1
@RequestMapping("/anno") //2
public class DemoAnnoController {

    @RequestMapping(produces = "text/plain;charset=UTF-8")    // 3
    public @ResponseBody
    String index(HttpServletRequest request) { // 4
        return "url:" + request.getRequestURL() + " can access";
    }

    @RequestMapping(value = "/pathvar/{str}", produces = "text/plain;charset=UTF-8")// 5
    public @ResponseBody
    String demoPathVar(@PathVariable String str, //3
            HttpServletRequest request) {
        return "url:" + request.getRequestURL() + " can access,str: " + str;
    }

    @RequestMapping(value = "/requestParam", produces = "text/plain;charset=UTF-8") //6
    public @ResponseBody
    String passRequestParam(Long id,
            HttpServletRequest request) {

        return "url:" + request.getRequestURL() + " can access,id: " + id;

    }

    @RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8")//7
    @ResponseBody //8
    public String passObj(DemoObj obj,
            HttpServletRequest request) {

        return "url:" + request.getRequestURL()
                + " can access, obj id: " + obj.getId() + " obj name:" + obj.getName();

    }

    @RequestMapping(value = {"/name1", "/name2"}, produces = "text/plain;charset=UTF-8")//9
    public @ResponseBody
    String remove(HttpServletRequest request) {

        return "url:" + request.getRequestURL() + " can access";
    }

}
package com.example.spring.framework.mvc.annotation.web;

import com.example.spring.framework.mvc.annotation.domain.DemoObj;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author: 王俊超
 * Date: 2017-07-11 22:12
 * All Rights Reserved !!!
 */
@RestController //1
@RequestMapping("/rest")
public class DemoRestController {

    @RequestMapping(value = "/getjson",
            produces = {"application/json;charset=UTF-8"}) //2
    public DemoObj getjson(DemoObj obj) {
        return new DemoObj(obj.getId() + 1, obj.getName() + "yy");//3
    }

    @RequestMapping(value = "/getxml",
            produces = {"application/xml;charset=UTF-8"})//4
    public DemoObj getxml(DemoObj obj) {
        return new DemoObj(obj.getId() + 1, obj.getName() + "yy");
    }

}
package com.example.spring.framework.mvc.annotation.web;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * Author: 王俊超
 * Date: 2017-07-11 22:19
 * All Rights Reserved !!!
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.example.spring.framework.mvc.annotation.web")
public class MyMvcConfig {
}
package com.example.spring.framework.mvc.annotation.web;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * Author: 王俊超
 * Date: 2017-07-11 21:51
 * All Rights Reserved !!!
 */
public class WebInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(MyMvcConfig.class);
        ctx.setServletContext(servletContext);

        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher",
                new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值