一、配置式开发

  在我们之前的学习中,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. centos和ubuntu的网络属性配置

        一. centos的网络配置    1.  修改 /etc/sysconfig/network-scripts/ifcfg-IFACE:             DEVICE:此配置文件应用到 ...

  2. 前后端分离——token超时刷新策略

    前言 记录一下前后端分离下————token超时刷新策略! 需求场景 昨天发了一篇记录 前后端分离应用——用户信息传递 中介绍了token认证机制,跟几位群友讨论了下,有些同学有这么一个疑惑:toke ...

  3. 利用Python爬取电影网站

    #!/usr/bin/env python #coding = utf-8 ''' 本爬虫是用来爬取6V电影网站上的电影资源的一个小脚本程序,爬取到的电影链接会通过网页的形式显示出来 ''' impo ...

  4. MFC Windows程序开发究竟是什么?

    MFC Windows程序开发究竟是什么? MFC是一个Application Framework.他是一个完整的程序模型. 什么是MFC?MFC是微软公司出品一个Application Framew ...

  5. HDU-1160-FatMouse's Speed(线性DP,LIS)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. SparkSQL中的自定义函数UDF

    在Spark中,也支持Hive中的自定义函数.自定义函数大致可以分为三种: UDF(User-Defined-Function),即最基本的自定义函数,类似to_char,to_date等 UDAF( ...

  7. 1091 N-自守数 (15 分)

    // 建一个判断函数,接受两个整形的变量,再通过循环按位判断相等与否,主体函数中调用被调函数,建立一个判断变量.#include <iostream> using namespace st ...

  8. python学习,day3:函数式编程,递归和高阶函数

    # coding=utf-8 # Author: RyAn Bi def calc(n): #递归 print(n) if int(n/2) > 0: #设置条件,否则会循环999 次,报错, ...

  9. c#Task类。实现异步的一种方式

    Task和Task<TResult>是c#提供的一种实现异步功能的2个类.Task<TResult>继承Task类,有返回参数. 1.基本用法 不嵌套利用静态方法创建和运行任务 ...

  10. Linux下which命令使用详解(转)

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置. f ...