springMVC注解启用及优化
使用注解的原因
最方便的还是启用注解
注解方便,而且项目中很流行。
配置文件尽量减少,主要使用注解方式。
Springmvc的注解是在2.5版本后有了注解,如何开启注解配置文件
Web.xml文件中不需要修改,只修改springmvc配置文件
新建一个springmvc的配置文件,取名为springAnnotation-servlet.xml
web.xml文件中的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/springMVCAnnotation-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<!-- 前端控制器的配置 自此请求已经交个spring web mvc框架处理 然后配置spring -->
springAnnotation-servlet.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- 注解扫苗包 -->
<context:component-scan base-package="com.tgb.web.controller.annotation" />
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
配置分两步
1)配置扫描包
这个配置文件的书写需要一个扫描包。
在springAnnotation-servlet.xml中配置
<context:component-scan base-package="com.tgb.web.controller.annotation"></context:component-scan>
这个配置的意思是:
Spring再启动的时候,会默认扫描自动扫描包下的所有的类,为每个注解分配一个mapping。
上面的配置是,在启动的时候会扫描com.tgb.web.controller.annotation下所有的包
2)配置spring注解的两个bean
<beanclass="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<beanclass="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
此两句可以可以使用mvc标签优化注解:
<!-- 开启注解 -->
<mvc:annotation-driven/>
这两个bean的功能
1)AnnotationMethodHandlerAdapter
是方法映射的,不同方法有不同url请求,根基类找方法。
2)DefaultAnnotationHandlerMapping
DefaultAnnotationHandlerMapping根据扫描的包下面找类,即通过url找类
注意:
一定要把包找对,我就犯过错,两个包都在org.springframework.web.portlet目录下找的类,我用的是两个包分别是
org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping
其实应该是org.springframework.web.servlet目录下找类。
两个包分别是:
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
新建一个类(userController)来实现注解
新建包
新建一个包,下面的controller都使用注解
新建controller的包,在这个包下的controller都使用配置文件。
选择com.tgb.web.controller,邮件点击new-other-package,填新建的包名为annotation。
新建类userController(GET请求)
注解打开后,看看注解如何使用。
userController类实现如下:
package com.tgb.web.controller.annotation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@RequestMapping(value="/user/addUser",method=RequestMethod.GET)
public ModelAndView adduser(){
String result = "this is adduser------";
return new ModelAndView("/annotation","result",result);
}
@RequestMapping(value="/user/delUser",method=RequestMethod.GET)
public ModelAndView delUser(){
String result = "this is delUser------";
return new ModelAndView("/annotation","result",result);
}
}
Spring常用注解:
- 类的注解
类注解用到了@Controller
@Controller表明下面是个Controller类
org.springframework.stereotype.Controller
@Component
@Target(value={TYPE})
@Retention(value=RUNTIME)
@Documented
- 方法注解
方法注解用到了@RequestMapping
@RequestMapping表明下面的方法是个Controller的方法。
org.springframework.web.bind.annotation.RequestMapping
详细介绍:
@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
RequestMapping注解有六个属性,下面我们把她分成三类进行说明。
1、 value, method;
value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3、 params,headers;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
@Mapping
@Target(value={METHOD, TYPE})
@Retention(value=RUNTIME)
由上面的信息可以看出,注解类RequestMapping的返回值是Mapping,方法参数有2个,第一个是Method,第二个是Type,即Method是请求的方法,Type是请求的类似是get还是post。
@RequestMapping(value="/user/delUser",method=RequestMethod.GET)
新建一个view——annotation.jsp
新建一个view来实现userController的效果
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'annotation.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h>springMVC注解1</h>
<br/>
<div>
${result }
</div>
</body>
</html>
springMVC注解启用及优化的更多相关文章
- 6.SpringMVC注解启用
SpringMVC注解可以帮助我们快速地注入 属性和参数 提高开发效率. 由于 有相当一部分人讨厌xml配置方式 注解可以覆盖 xml则不能 使用注解比xml规范化,因为很多注解都是java的规范的范 ...
- Springmvc注解启用
http://www.blogbus.com/wanping-logs/235898637.html 使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用 ...
- springMVC(注解版笔记)
springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...
- springMVC 注解版
http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...
- SpringMVC注释启用
这篇文章是我学习的网络视频SpringMVC写的过程. 谢谢公布各位前辈的视频 以下评论SpringMVC几个关键步骤,注意事项启用: 首先需要加载配置文件(假设请使用自定义路径) <? xml ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(四)单元测试实例
日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(三)代码测试>讲了不为和不能两个状态,针对不为,只能自己调整心态了,而对于不能,本文会结合一 ...
- SpringMVC注解HelloWorld
今天整理一下SpringMVC注解 欢迎拍砖 @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是 ...
- SpringInAction--Spring Web应用之SpringMvc 注解配置
Spring MVC 是当前Web服务器中常用的结构,今天就来学习这相关的知识,首先上图——Spring请求的时候所经历的坎坷之路: (书上原话,算是解释..) 在请求离开浏览器时① ,会带有用户所请 ...
- springMVC注解初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
随机推荐
- codeforces Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)// 二分的题目硬生生想出来ON的算法
A. Road to Cinema 很明显满足二分性质的题目. 题意:某人在起点处,到终点的距离为s. 汽车租赁公司提供n中车型,每种车型有属性ci(租车费用),vi(油箱容量). 车子有两种前进方式 ...
- CodeFirst进行数据迁移之添加字段
一.为模型更改设置 Code First 数据迁移 1.工具->库程序包管理器->程序包管理器控制台->输入"Enable-Migrations" 或者 Ena ...
- 阅读笔记Multi-task Learning for Stock Selection [NIPS1996]
Multi-task Learning for Stock Selection Joumana Ghosn and Yoshua Bengio 摘要 用人工神经网络预测未来回报以便于做出对应的金融决 ...
- 安装运行okvis
1. 安装依赖项 sudo apt-get install cmake //cmake sudo apt-get install libgoogle-glog-dev // glog是Googl ...
- MBProgressHud添加自定义动画
在使用自定义view时,若直接使用,如下 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud ...
- 利用Kinect将投影变得可直接用手操控
Finally 总算是到了这一天了!假期里算法想不出来,或者被BUG折磨得死去活来的时候,总是YY着什么时候能心情愉快地坐在电脑前写一篇项目总结,今天总算是抽出时间来总结一下这神奇的几个月. 现在回过 ...
- Linq group
using System;using System.Collections.Generic;using System.Linq; public class MyClass{ public static ...
- Firefox每次刷新时自动清空缓存的设置方法
当我们开发网页应用时候,为了保证每次看到的页面是最新的,需要在刷新页面时清除页面缓存. 如果每次都手动清除比较麻烦,好在多数浏览器都支持自动清除缓存的功能. IE下我们可以将缓存设置为"每次 ...
- 记一次创建LVM的日志记录
先上一张鸟哥LVM的图.感觉这张最清楚了. #以下以Xshell的日志记录系统直接记录.上面添加了一些个人理解的注释 [BEGIN] 2016/9/13 9:22:24 #先查看下硬盘的情况. [ro ...
- 百度UEditor基本使用
1 首先奉上链接其http://ueditor.baidu.com/website/index.html 更多更详细内容在其官方api上,本文只是一个归类总结性文章. 2 下载链接http://ued ...