一、配置式开发

  在我们之前的学习中,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注解式开发的更多相关文章

  1. Hibernate5笔记9--Hibernate注解式开发

    Hibernate注解式开发: (1)注解式开发的注意点: Hibernate中使用注解,主要是为了替代映射文件,完成“类到表,属性到字段”的映射.  JPA提供了一套功能强大的注解.Hibernat ...

  2. SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...

  3. SSM-SpringMVC-16:SpringMVC中小论注解式开发之访问方式篇

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 访问方式可以指定,打个比方,你通过get方式进入登陆页面,通过post发送ajax数据库校验或者post提交 ...

  4. SSM-SpringMVC-15:SpringMVC中小论注解式开发之通配符篇

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 此处改了下标题,小论,为什么不说大话了呢?哎,质量不够啊,通配符篇提取不出更多可以讲的滔滔不绝的套路 通配符 ...

  5. SpringMVC 注解式开发

    SpringMVC的注解式开发是指,处理器是基于注解的类的开发.对于每一个定义的处理器,无需再配置文件中逐个注册,只需在代码中通过对类与方法的注解,便可完成注册.即注解替换是配置文件中对于处理器的注册 ...

  6. 《SpringMVC从入门到放肆》九、SpringMVC注解式开发(简单参数接收)

    上一篇我们学习了注解式开发的配置方式并写了一个小Demo跑起来.今天我们来学习注解开发的参数接收.处理器方法中的常用参数有五类,这些参数会在系统调用时由系统自动赋值,即程序员可以在方法中直接使用.具体 ...

  7. 《SpringMVC从入门到放肆》八、SpringMVC注解式开发(基本配置)

    上一篇我们结束了配置式开发,配置式开发目前在企业中用的并不是很多,大部分企业都在使用注解式开发,所以今天我们就来学习注解式开发.所谓SpringMVC注解式开发是指,处理器是基于注解的类的开发方式.对 ...

  8. 《SpringMVC从入门到放肆》十一、SpringMVC注解式开发处理器方法返回值

    上两篇我们对处理器方法的参数进行了分别讲解,今天来学习处理器方法的返回值. 一.返回ModelAndView 若处理器方法处理完后,需要跳转到其它资源,且又要在跳转资源之间传递数据,此时处理器方法返回 ...

  9. 3.2.3 SpringMVC注解式开发

    SpringMVC注解式开发 1. 搭建环境 (1) 后端控制器无需实现接口 , 添加相应注解 Controller类添加注解 @Controller //该注解表将当前类交给spring容器管理 @ ...

随机推荐

  1. jmeter5.1.1启动提示not able to find java executable or version的解决办法

    安装jmeter5.1.1完成后,启动报错not able to find java executable or version,如下图所示 解决办法: 1.在环境变量PATH的最后添加如下内容:%S ...

  2. eclipse打包jar文件

    论文仿真做线性回归分类在人脸识别中应用与研究,在单机下实现LRC算法后,又在Hadoop云平台下实现了该算法.在比较实验结果时候需要放在相同硬件条件下比较.但是LRC单机算法是在windows下的ec ...

  3. php性能优化三(PHP语言本身)

    0.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册 ...

  4. [转] 检查更新时出错:无法启动更新检查(错误代码为 4: 0x80070005 — system level)

    Google浏览器Chrome更新到时候提示错误:检查更新时出错:无法启动更新检查(错误代码为 4: 0x80070005 -- system level),很有可能是Chrome更新服务被禁用了,我 ...

  5. 爬虫实战4:用selenium爬取淘宝美食

    方案1:一次性爬取全部淘宝美食信息 1. spider.py文件如下 __author__ = 'Administrator' from selenium import webdriver from ...

  6. 使用python 模仿mybinlog 命令 二进制分析mysql binlog

    出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. ...

  7. 前端视频插件Aliplayer播放器简单使用(基于地址播放)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. 课堂练习:ex 4-20

    一.习题要求 • 定义一个复数类Complex. • 有相加,输出,模计算函数. • 模计算要求结果保存在第一个复数中. 二.习题内容 //complex.h # ifndef COMPLEX_H # ...

  9. java四行代码实现图片下载

    如下: InputStream in = new URL("http://www.updown/thumbnail.jpg).openStream(); Path temp = Paths. ...

  10. python 全栈开发:逻辑运算

    基础运算符 逻辑运算: 优先级:()> not > and >or 数字转bool值,0为False,非零的数字为True. 1. print(2 > 1 and 1 < ...