1.Controller层

@Controller注解

@Controller用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping。
context:component-scan扫描

<context:component-scan base-package = "com.host.app.web.controller">
<context:include-filter type = "annotation" expression = "org.springframework.stereotype.Controller" />
</context:component-scan >

@RequestMapping

使用@RequestMapping来映射Request请求与处理器
以使用@RequestMapping来映射URL到控制器类,或者是到Controller控制器的处理方法上。
当@RequestMapping标记在Controller类上的时候,里面使用@RequestMapping标记的方法的请求地址都是相对于类上的@RequestMapping而言的;
当Controller类上没有标记@RequestMapping注解时,方法上的@RequestMapping都是绝对路径。这种绝对路径和相对路径所组合成的最终路径都是相对于根路径“/”而言的。
如果这个注解加到类上

@Controller
@RequestMapping("/test")
public class MyController {
  @RequestMapping("/showView")
  public ModelAndView showView() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("viewName");
    modelAndView.addObject(" 需要放到 model 中的属性名称 ", " 对应的属性值,它是一个对象 ");
    return modelAndView;
  }
}

请求地址变为/test/showView

@RequestMapping(value = "testHeaders", headers = { "host=localhost",
    "Accept" })
public String testHeaders() {
  return "headers";
}

@PathVariable

取出urlRestFul变量值
例子:http://localhost/app/{variable1}/index.html,这个模板里面包含一个变量variable1。
实现URL的RestFul风格

headers属性
使用headers属性可以通过请求头信息来缩小@RequestMapping的映射范围。
headers属性的用法和功能与params属性相似。在上面的代码中当请求/testHeaders.do的时候只有当请求头包含Accept信息,且请求的host为localhost的时候才能正确的访问到testHeaders方法。

@RequestMapping(value = "testHeaders", headers = { "host=localhost",
    "Accept" })
public String testHeaders() {
  return "headers";
}

@RequestParam

例子:/requestParam.do?name=hello&age=1

使用 @RequestParam绑定HttpServletRequest请求参数到控制器方法参数

@RequestMapping("requestParam")
public String testRequestParam(@RequestParam(required=false) String name, @RequestParam ("age") int age) {
  return "requestParam";
}

在@RequestParam中除了指定绑定哪个参数的属性value之外,还有一个属性required,它表示所指定的参数是否必须在request属性中存在,默认是true,表示必须存在,当不存在时就会报错。

@Value

Spring通过注解获取*.porperties文件的内容,除了xml配置外,还可以通过@value方式来获取。
使用方式必须在当前类使用@Component或者@Controller,xml文件内配置的是通过pakage扫描方式例如:<context:component-scanbase-package="pakage_name"/>

public class AbnormalWarningBulletinJob extends AbstractWantJob {
  // 访问共享文件遵循smb协议
  private static final String SMB_PREIFX = "smb:";
  
  @Autowired(required=false)
  @Qualifier
  private SmsSendService sendService;
  
  @Value("${abnormal.warning.bulletin}")
  private String smb_url;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nier6088

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值