springmvc常用知识总结,不定期更新
1.@Controller
注解到类名上,表示该类是控制器。
2.@RequestMapping("/xxxx")
可以放在类名/方法名之上,表示访问请求该方法时的映射url。如果该方法类名有@RequestMapping,则访问该方法的url=项目名+类的RequestMapping+方法的RequestMapping。
3.@Resource
作用相当于@Autowired,只不过@Autowired按类型自动注入,而@Resource默认按byName注入。
@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。
所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。
如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
举例:如果Spring上下文中存在不止一个UserDao的bean时,就会抛出BeanCreationException异常;
如果Spring上下文中不存在UserDao类型的bean,也会抛出BeanCreationException异常。
我们可以使用@Qualifier配合@Autowired来解决。
①存在多个bean时,这样解决。
@Autowired
@Qualifier("userServiceImpl")
public IUserService userService;
②不存在时这样解决。
@Autowired(required = false)
public IUserService userService
4.@Service
@Service("Bean的名称") 其中双引号内为可选操作,可以不填,填了代表自定义业务名。整体含义为定义业务层bean。
@Service用在实现类的类名上。如
@Service("alarmManageService")
public class AlarmServiceImp implements AlarmManageService{}
5.@Repository
@Repository("Bean的名称")
定义DAO层Bean
6.@Component
定义Bean, 不好归类时使用。即除了常用的归类外,此Bean不属于其中任何一个,即可用Component修饰。
7.@InitBinder
用于绑定数据,比如前端页面有两个对象,一个student,一个course,切他俩的属性名都一样,此时即可使用该注解。
使用该注解对WebDataBinder 对象进行初始化,只对当前的Controller有效
@Controller
@RequestMapping("/classtest")
public class TestController {
// 绑定变量名字和属性,参数封装进类
@InitBinder("student")
public void initBinderUser(WebDataBinder binder) {
binder.setFieldDefaultPrefix("student.");
}
// 绑定变量名字和属性,参数封装进类
@InitBinder("course")
public void initBinderAddr(WebDataBinder binder) {
binder.setFieldDefaultPrefix("course.");
}
@RequestMapping("/methodtest")
@ResponseBody
public Map<String,Object> test(@ModelAttribute("student") Student student,@ModelAttribute("course") Course course){
Map<String,Object> map=new HashMap<String,Object>();
map.put("student", student);
map.put("course", course);
return map;
}
对日期类型进行转换
@InitBinder
public void InitBinder(WebDataBinder binder){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CustomDateEditor dateEditor = new CustomDateEditor(df, true);
binder.registerCustomEditor(Date.class,dateEditor);
}
}
8.前端小知识。jquery使用ajax时,可以设置同步或异步执行 async: true为异步 async: false是同步。
9.加入aop,可以用作日志相关。
(1).pom.xml引包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.3</version>
</dependency>
(2).springmvc.xml配置
<!--配置cglib代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
(3).创建自定义注解
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {
String method() default "";// 方法名
String flag() default "";// 预留标识
String describe() default "";// 操作描述
String type() default "";// 操作类型(如设备,证书,用户)
}
其中SystemLog内的方法是用作注解内的参数,如flag,type可让切面内容更加丰富。
(4).在需要进行切面的函数上添加如下注解
@SystemLog(method = <方法名>, flag = <自定义标识符>, describe = <自定义描述>, type = <自定义类型>)
(5).配置切面函数。首先创建一个类,例如 test ,然后创建一个函数,如qiemian。在qiemian函数进行如下配置。
@Pointcut("@annotation(<SystemLog自定义注解类全路径,如com.test.SystemLog>)")
public void test() {
}
(6).添加后置或前置或环绕等切面通知,例如:
@AfterReturning(value = "test()", returning = "result")
public Object doAfterReturning(JoinPoint joinPoint, Object result) {
}
注意,returning = "result"的result一定要和Object result这个参数的参数名一致。
如果需要获取request,可用如下方法:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
10.HTTPHeader

10.突然报错
Description Resource Path Location Type
Cannot change version of project facet Dynamic Web Module to 2.5. JBFEducation line 1 Maven Java EE Configuration Problem
解决方式:打开当前项目目录,进入.setting文件夹,修改org.eclipse.wst.common.project.facet.core.xml 中的为 <installed facet="jst.web" version="2.5"/> 即可。
11.常见http错误码
302 redirect: 302 代表暂时性转移(Temporarily Moved )。
意思就是访问网址A,但是网址A因为服务器端的拦截器或者其他后端代码处理的原因,会被重定向到网址B。
springmvc常用知识总结,不定期更新的更多相关文章
- AngularJS进阶(十二)AngularJS常用知识汇总(不断更新中....)
AngularJS常用知识汇总(不断更新中....) 注:请点击此处进行充电! app.controller('editCtrl',['$http','$location','$rootScope', ...
- 【linux入门必备】小白需要掌握的基础知识_不定期更新
因为博主对linux掌握暂时不需要太精通,遇到一个记录一个. 零碎 知识点: 杂类常用命令: 模糊匹配补齐 TAB 系统相关命令: 查阅手册 man 更新软件 sudo apt-get update ...
- python常用模块(不定期更新)
前言: 随着python开发项目越来越大,显然应该把不同功能的代码放到不同的.py文件里,每一个.py文件叫一个模块:模块分为(1)内置标准模块(2)第三方模块(3)自定义模块 这三种.这篇博客就是用 ...
- Java常用知识(长期更新)
1.0转义符 \n:换行 ,\n的作用和print后面的ln相同,可酌情使用 \t:一个制表位(一系列空格) 例: public class Schedule{ public static void ...
- PHP常用函数总结(不定期更新)
array_merge函数和两个数组相加的区别 array_merge函数,在出现相同的索引数组,会把两个数组中的索引数组,进行融合,以第一个数组的索引最大值往后相加,如果是关联数组,后面数组的值覆盖 ...
- Javascript常用函数收集(不定期更新)
str.replace('/正则表达式/','替换内容'); //正则替换str.match('/正则表达式/','替换内容'); //正则匹配 str.indexOf('查找代码'); //查找是否 ...
- C#学习笔记-基础知识篇(不定期更新)
1.父类必须包含构造函数么? 父类必须要有一个构造函数,有参无参都可以. 构造函数是对象的基本,没有构造函数就没有对象,若父类中显示的有参数的构造函数,在子类继承就必须写一个构造函数来调用父类的构造函 ...
- 个人常用eclipse快捷键,不定期更新
ctrl+f11 ==> runctrl+h ==> 全文检索main+enter ==>public static void main(String[] args) { } alt ...
- 不定期更新的IDEA功能整理
目录 不定期更新的IDEA功能整理 idea 命令 Preferences 和 Project Structure Keymap HTTP Proxy Postfix Completion 插件 插件 ...
随机推荐
- 与DSP通信时,RD&WR信号
/////////////////////////////////////////////////////////// :] rd,wr; :] dsp_data_out; 'hzzzz; // ...
- 分析无法进入Linux系统的原因
上文:http://www.cnblogs.com/long123king/p/3549701.html 1: static int __init kernel_init(void * unused) ...
- 高并发下的缓存架构设计演进及redis常见的缓存应用异象解决方案
待总结 缓存穿透 缓存击穿 缓存雪崩等
- Match & Catch CodeForces - 427D 后缀自动机水题
题意: 给出两个字符串a,b,求一个字符串,这个字符串是a和b的子串, 且只在a,b中出现一次,要求输出这个字符串的最小长度. 题解: 将a串放入后缀自动机中,然后记录一下每个节点对应的子串出现的次数 ...
- 微信小程序开发之https服务器搭建三步曲
本篇文章主要讲述3个方面的内容,如下: 1.SSL证书的获取 2.服务器 Nginx SSL 证书的配置. 3.如何兼容80端口和443端口以及为什么要同时兼容这两个端口. 1.SSL证书的获取 ht ...
- useradd -帐号建立或更新新使用者的资讯
总览 SYNOPSIS useradd [-c comment] [-d home_dir] [-e expire_date] [-f inactive_time] [-g initial_group ...
- Linux 进程间通信 信号(signal)
1. 概念: 1)信号是在软件层次上对中断机制的一种模拟,是一种异步通信方式 2)信号可以直接进行用户空间进程和内核进程之间的交互,内核进程也可以利用它来通知用户空间进程发生了哪些系统事件. 3)如果 ...
- Ubuntu下安装nfs,Windows下访问
Linux 下: 1. 在终端输入 sudo apt-get install portmap nfs-common nfs-kernel-server 2.建立客户机访问目录 sudo mkdir ...
- UVA11427 Expect the Expected 概率dp+全概率公式
题目传送门 题意:小明每晚都玩游戏,每一盘赢的概率都是p,如果第一盘就赢了,那么就去睡觉,第二天继续玩:否则继续玩,玩到赢的比例大于p才去睡:如果一直玩了n盘还没完成,就再也不玩了:问他玩游戏天数的期 ...
- 转 直接在浏览器运行Python代码
到这个链接将代码下载到本地,然后打开cmd,使用python运行此文件,然后不要关闭窗口: https://raw.githubusercontent.com/michaelliao/learn-py ...