美文网首页
@Controller、@RequestMapping以及@Re

@Controller、@RequestMapping以及@Re

作者: 我弟是个程序员 | 来源:发表于2017-07-13 15:01 被阅读0次

举例:

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.df.test.pojo.Customer;
import com.df.test.service.ICustomerService;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private ICustomerService customerService;

    @RequestMapping(value = "/showUser1")
    public String toIndex(HttpServletRequest request, Model model) {
        int userId = Integer.parseInt(request.getParameter("id"));
        Customer customer = this.customerService.getUserById(userId);
        model.addAttribute("customer", customer);
        return "showUser";//返回的视图名称
    }
}

假设本项目的名称叫做demo,上述控制器对应请求则是:

http://localhost:8080/demo/user/showUser1?id=1
  • @Controller注解用来注明一个类是控制器。
  • @RequestMapping注解用来映射一个请求,可以修饰一个类,也可以修饰一个函数。
下面来说说@RequestMapping注解的属性:
  • value属性,将URL映射到方法上或是类上,其值也可以是个空字符串,因为默认属性是value,所以当只有一个属性时,可以value可以省略。String[] 类型
  • method属性,用来支出该方法只处理那些类的请求,如:GET,POST,PUT。RequestMethod[]类型
@RequestMapping(value = "/showUser",method=RequestMethod.POST) //表示仅处理post请求
  • consumes属性,指定处理请求的提交类型(Content-Type)。String[] 类型
@RequestMapping(value = "/showUser",consumes="application/json") //表示仅处理Content-Type ="application/json" 的请求
  • produces属性,该属性指定返回的内容类型,返回的内容类型必须是request请求头(Accept)中所包含的类型。String[] 类型
@RequestMapping(value = "/showUser",produces="application/json") 

该方法处理request请求中Accept头中包含了"application/json",并且,指明了返回内容类型为"application/json"。

  • params属性,指明了request请求中包含了某些参数值时,才让该方法处理。String[] 类型
@RequestMapping(value = "/showUser",params = "myparam=myvalue") 

该方法仅处理request中参数包含了指定key为"myparam"值为"myvalue" 的请求。

  • headers属性,指明了request请求中包含了某些指定的header值,才让该方法处理。String[] 类型
@RequestMapping(value = "/showUser",headers= "Referer=http://www.google.com") 

该方法仅处理request中header包含了指定"Referer"请求头和对应值为"http://www.google.com" 的请求。

下面看另一例来讲解@RequestParam注解:

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.df.test.pojo.Customer;
import com.df.test.service.ICustomerService;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private ICustomerService customerService;

    @RequestMapping("/showUser1")
    public String toIndex(@RequestParam("id")String id, Model model) {
        //int userId = Integer.parseInt(request.getParameter("id"));
        Customer customer = this.customerService.getUserById(Integer.parseInt(id));
        model.addAttribute("customer", customer);
        return "showUser";
    }
}

假设本项目的名称叫做demo,上述控制器对应请求则是:

http://localhost:8080/demo/user/showUser1?id=1

@RequestParam注解,用于将request请求参数中的值,赋给方法中的形参。以下是属性说明

  • value 属性,请求参数的名称。是默认属性,当注解只有一个属性时,可以省略。String类型
  • required属性,参数是否必须。boolean类型
  • defaultValue属性,如果没有传参而使用的默认值。String类型
@RequestParam(value="loginname",required=ture,defaultValue="root")

以上是@Controller和@RequestMapping的具体用法和常用属性的讲解。

相关文章

网友评论

      本文标题:@Controller、@RequestMapping以及@Re

      本文链接:https://www.haomeiwen.com/subject/somwhxtx.html