SpringMVC笔记:annotation注解式开发
一、配置式开发
在我们之前的学习中,springmvc使用配置式开发模式,需要在核心配置文件中springmvc.xml注册大量的bean来注入controller控制器,工作繁琐容易出错,下面我们学习一下注解式开发来简化我们的工作。
。。。
案例:
1.控制器
public class MyMultiController extends MultiActionController {
/*第一个方法*/
public String doFirst(HttpServletRequest request, HttpServletResponse response){
System.out.println("执行方法一!!!");
return "first";
}
/*第二个方法*/
public String doSecond(HttpServletRequest request, HttpServletResponse response){
System.out.println("执行方法二!!!");
return "second";
}
}
2.spring注册
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!--控制器映射-->
<bean class="cn.springmvc.day02handler.MyPropertiesController" id="myPropertiesController">
<property name="methodNameResolver" ref="methodNameResolver"></property>
</bean>
<!--方法名称解析器-->
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" id="methodNameResolver">
<!--<property name="paramName" value="action"></property>-->
</bean>
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">myPropertiesController</prop>
</props>
</property>
</bean>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.访问
localhost:8080/hello.do?action=doFirst
-----------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!--porperties-->
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver" id="propertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/first.do">doFirst</prop>
<prop key="/second.do">doSecond</prop>
</props>
</property>
</bean>
<!--控制器映射器-->
<bean class="cn.springmvc.day02handler.MyPropertiesController" id="myPropertiesController">
<property name="methodNameResolver" ref="propertiesMethodNameResolver"></property>
</bean>
<!--映射器配置-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/*.do">myPropertiesController</prop>
</props>
</property>
</bean> <!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀 prefix-->
<property name="prefix" value="/"></property>
<!--后缀 suffix-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
访问:
localhost:8080/first.do | second.do
----------------------------------------------------
二、注解式开发
1.在控制器的类和方法上添加注解
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @Controllerpublic class MyAnnontation {
/*方法一*/
@RequestMapping("/first.do")
public ModelAndView getModel(HttpServletRequest request, HttpServletResponse response){
System.out.println("注解方法一!");
ModelAndView mv = new ModelAndView("first");
return mv;
} /*方法一*/
@RequestMapping("/second.do")
public ModelAndView getView(HttpServletRequest request, HttpServletResponse response){
System.out.println("注解方法二!");
ModelAndView mv = new ModelAndView("second");
return mv;
}
}
2.配置文件中添加注解驱动<mvc:annotation-driven/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描controller-->
<context:component-scan base-package="cn.springmvc.annotation"></context:component-scan> <!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean> <!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
3.简单访问
。。。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Annotation</title>
</head>
<body>
<h1>未标明命名空间</h1>
<form action="/first.do" method="get">
<input type="submit" value="执行方法一">
</form>
<form action="/second.do" method="get">
<input type="submit" value="执行方法一">
</form>
<hr/>
<h1>已标明命名空间</h1>
<form action="/annotation/first.do" method="get">
<input type="submit" value="执行方法一">
</form>
<form action="/annotation/second.do" method="get">
<input type="submit" value="执行方法一">
</form>
</body>
</html>
三、注解式开发中引入命名空间来避免重名方法的混淆
1.在类名上添加RequestMapping("/name")注解即可区分
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @Controller
/*命名空间*/
@RequestMapping("/annotation")
public class MyAnnontation {
/*方法一*/
@RequestMapping("/first.do")
public ModelAndView getModel(HttpServletRequest request, HttpServletResponse response){
System.out.println("注解方法一!");
ModelAndView mv = new ModelAndView("first");
return mv;
} /*方法一*/
@RequestMapping("/second.do")
public ModelAndView getView(HttpServletRequest request, HttpServletResponse response){
System.out.println("注解方法二!");
ModelAndView mv = new ModelAndView("second");
return mv;
}
}
SpringMVC笔记:annotation注解式开发的更多相关文章
- Hibernate5笔记9--Hibernate注解式开发
Hibernate注解式开发: (1)注解式开发的注意点: Hibernate中使用注解,主要是为了替代映射文件,完成“类到表,属性到字段”的映射. JPA提供了一套功能强大的注解.Hibernat ...
- SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...
- SSM-SpringMVC-16:SpringMVC中小论注解式开发之访问方式篇
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 访问方式可以指定,打个比方,你通过get方式进入登陆页面,通过post发送ajax数据库校验或者post提交 ...
- SSM-SpringMVC-15:SpringMVC中小论注解式开发之通配符篇
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 此处改了下标题,小论,为什么不说大话了呢?哎,质量不够啊,通配符篇提取不出更多可以讲的滔滔不绝的套路 通配符 ...
- SpringMVC 注解式开发
SpringMVC的注解式开发是指,处理器是基于注解的类的开发.对于每一个定义的处理器,无需再配置文件中逐个注册,只需在代码中通过对类与方法的注解,便可完成注册.即注解替换是配置文件中对于处理器的注册 ...
- 《SpringMVC从入门到放肆》九、SpringMVC注解式开发(简单参数接收)
上一篇我们学习了注解式开发的配置方式并写了一个小Demo跑起来.今天我们来学习注解开发的参数接收.处理器方法中的常用参数有五类,这些参数会在系统调用时由系统自动赋值,即程序员可以在方法中直接使用.具体 ...
- 《SpringMVC从入门到放肆》八、SpringMVC注解式开发(基本配置)
上一篇我们结束了配置式开发,配置式开发目前在企业中用的并不是很多,大部分企业都在使用注解式开发,所以今天我们就来学习注解式开发.所谓SpringMVC注解式开发是指,处理器是基于注解的类的开发方式.对 ...
- 《SpringMVC从入门到放肆》十一、SpringMVC注解式开发处理器方法返回值
上两篇我们对处理器方法的参数进行了分别讲解,今天来学习处理器方法的返回值. 一.返回ModelAndView 若处理器方法处理完后,需要跳转到其它资源,且又要在跳转资源之间传递数据,此时处理器方法返回 ...
- 3.2.3 SpringMVC注解式开发
SpringMVC注解式开发 1. 搭建环境 (1) 后端控制器无需实现接口 , 添加相应注解 Controller类添加注解 @Controller //该注解表将当前类交给spring容器管理 @ ...
随机推荐
- JAVA 中的 StringBuilder 和 StringBuffer 适用的场景是什么?
JAVA 中的 StringBuilder 和 StringBuffer 适用的场景是什么? 最简单的回答是,stringbuffer 基本没有适用场景,你应该在所有的情况下选择使用 stringbu ...
- [AS3.0] 解决Number类型计算不精确问题
看下面代码运行结果: trace(256.08+123.1); //379.17999999999995 trace(256.08-123.11); //132.96999999999997 trac ...
- Android 线刷小白教程
Android 线刷小白教程 再说一遍,绝不使用刷机精灵等软件. 一.概念 安卓系统一般把rom芯片分成7个区,如果再加上内置sd卡这个分区,就是8个: hboot分区----------负责启动. ...
- 异步 JavaScript - 事件循环
简评:如果你对 JavaScript 异步的原理感兴趣,这里有一篇不错的介绍. JavaScript 同步代码是如果工作的 在介绍 JavaScript 异步执行之前先来了解一下, JavaScrip ...
- ubuntu15.04中安装mysql和mysql-workbench
本文主要讲解mysql在ubuntu中的安装和使用,以及mysql workbench的安装和使用.mysql是一个开源的数据库软件,甲骨文的一个产品,是一个性能较好的数据库软件.mysql work ...
- D01——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D01 20180705内容纲要: 1 PYTHON介绍 2 PYTHON变量定义规则 3 PYTHON文件结构 4 PYTHON语句及语法 5 字符编码 6 ...
- c#-MVC基础操作-数据的展示及增删改、登录页面及状态保持
一.数据展示 1.View代码: <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynam ...
- gettimeofday
作用: 需要打印代码执行到某处的时间,或者需要计算程序执行的时间差(精确到微妙级).这时会用到gettimeofday函数,它可以返回自1970-01-01 00:00:00到现在经历的秒数. 原型: ...
- 【学习小记】KD-Tree
Preface 听说KD树实在是个大神器 可以解决多维空间多维偏序点权和,可以求某个点的空间最近最远点 就二维平面上的来说,复杂度在\(O(n\log n)\)到\(O(n\sqrt n)\)不等 嫌 ...
- Jmeter创建FTP测试计划
创建FTP测试计划 在这一章,你将学习如何创建一个基础的测试计划来测试FTP站点.你将在一个FTP站点上的两个文件中创建四个用户来发送请求.并且,你将告诉用户运行测试两次.所以,总的请求数是(4个用户 ...