JAVA记录-@Controller和RequestMapping注解代码介绍
1.spring-mvc.xml加入配置
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.net.xinfang.controller" />
2.@controller/@RequestMapping/@RequestParam写法
@Controller
@RequestMapping(value = "/cte") ---代表第一层url
public class CntoEnController { @RequestMapping(value="/getcte", method = {RequestMethod.GET}) ---代码第二次url
public String test(HttpServletRequest request,Model model,
@RequestParam(value="langType", defaultValue="zh") String langType){ ---请求的值传给langType
return "cte";
}
} 完整请求路径为:http://ip地址:端口号/项目名/cte/getcte?langType=zh
3.@RequestMaping写法
@RequestMapping(value="",method={"",""},headers={},params={"",""})
参数:
value:设置访问地址
method:设置访问方式,常用的method=RequestMethod.POST,和method=RequestMethod.GET
headers:头域,可以设置浏览器支持的格式
params:访问参数设置
注解作用:
用来定义访问的url。可以是方法级别的,也可以是类级别的。两者可以协同工作,缩小选择范围。也可以隐藏代码的真实路径,更加具有安全性和可读性。
@Autowired
注解作用:
可以对成员变量、方法和构造函数进行标注,来完成自动装配工作。可以消除get,set方法。
可以使用@Autowired注解来减少代码量。首先,在applicationContext中加入:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
然后,在变量上添加@Autowired注解,并去掉相应的getter和setter方法:
public class ClasServiceImpl implements ClasService{
@Autowired
private ClasDAO clasDAO;
...
}
并且在applicationContext中将相应的<property></property>标签去掉:
<bean id="clasService" class="com.school.service.ClasServiceImpl">
</bean>
Spring启动时,AutowiredAnnotationBeanPostProcessor会扫描所有的Bean,当发现其中有@Autowired注解时,就会找相应类型的Bean,并且实现注入。
@ResponseBody
注解作用:
直接放在方法上,表示返回类型将会直接作为Http响应字节流输出,可以用于Ajax。
@RequestParam(required=,value="",defaultValue="")
参数:
required:参数是否必须,boolean类型,默认为true
value:传递的参数名称,String类型,可选项,有值则对应方法的参数
defaultValue:参数没有传递时为参数默认指定的值
@Override
伪代码,表示重写(当然不写也可以),不过也有好处:
1、可以当注释用,方便阅读;
JAVA记录-@Controller和RequestMapping注解代码介绍的更多相关文章
- 0054 Spring MVC的@Controller和@RequestMapping注解
@Controller注解 该注解用来指示一个类是一个控制器,在Spring的配置xml文件中开启注解扫描 <context:conponent-scan base-package=" ...
- RequestMapping 注解的解析、匹配、注册
RequestMapping 注解的解析.匹配.注册 1)创建 RequestMappingHandlerMapping 实例时会触发 afterPropertiesSet 调用. 2)读取容器中所有 ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...
- SpringMVC中@Controller和@RequestMapping用法和其他常用注解
一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...
- SpringMVC中@Controller和@RequestMapping用法和其他常用注解(转)
一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...
- Java基础笔记 – Annotation注解的介绍和使用 自定义注解
Java基础笔记 – Annotation注解的介绍和使用 自定义注解 本文由arthinking发表于5年前 | Java基础 | 评论数 7 | 被围观 25,969 views+ 1.Anno ...
- SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别
@Controller 处理http请求 @Controller //@ResponseBody public class HelloController { @RequestMapping(valu ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别
SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...
- springmvc注解@Controller和@RequestMapping
Spring从2.5版本引入注解,从而让开发者的工作变得非常的轻松 springmvc注解Controller org.springframework.stereotype.Controller注解类 ...
随机推荐
- Filter学习:项目第八阶段
public interface Filter A filter is an object that performs filtering tasks on either the request ...
- Linux shell 菜鸟学习笔记....
20171123 Linux shell 基础学习笔记1. shell 的开始 一般是 #!/bin/bash 通过 #! 来唯一指定使用的shell路径 其他的 # 都表示注释.2. shell 的 ...
- Jquery 组 标签页
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- 第八周PSP 新折线图和饼图 个人时间管理
1.PSP DATE START-TIME END-TIME EVENT DELTA TYPE 4.18 15.36 16.10 读构建执法 走神5min 29mi ...
- 关于python 自带csv库的使用心得 附带操作实例以及excel下乱码的解决
因为上次帮我们产品处理过一个文件,他想生成能excel处理操作的.但是上次由于时间非常紧张,所以并没有处理好. 正好无聊就来好好研究一下 ,找算法要了几个 csv文件.来好好玩一玩. 全篇使用了pyt ...
- Lodop打印控件中PRINT_INITA()和PRINT_PAGESIZE()宽高
Lodop中有两个初始化语句,PRINT_INIT()和PRINT_INITA(),PRINT_INITA()多了四个参数,前两个是整体偏移值,第三四参数是宽高,这个宽高是指打印设计可视化编辑区域的宽 ...
- Dictionary CPU 100%
昨天服务器的CPU突然100%,此服务已经运行几年了,都平安无事.既然问题出现当然要找出这个遗留多年的小概率问题.出现cpu 100% 一般就是哪里出现了无法跳出的死循环. 1.获取进程的内存信息 服 ...
- OpenGL 使用 PBO 高速复制屏幕图像到内存或者纹理中
如果你想给游戏做个截图功能,或者想把屏幕图像弄成一个纹理,你就非常需要 PBO 了 通常情况下,你想把屏幕图像的像素数据读到内存需要用 glReadPixels 然后 pixels 参数传进去一块内存 ...
- SpringBoot+MyBatis多数据源使用分页插件PageHelper
之前只用过单数据源下的分页插件,而且几乎不用配置.一个静态方法就能搞定. PageHelper.startPage(pageNum, pageSize); 后来使用了多数据源(不同的数据库),Page ...
- LOJ #2541. 「PKUWC 2018」猎人杀(容斥 , 期望dp , NTT优化)
题意 LOJ #2541. 「PKUWC 2018」猎人杀 题解 一道及其巧妙的题 , 参考了一下这位大佬的博客 ... 令 \(\displaystyle A = \sum_{i=1}^{n} w_ ...