一、配置式开发

  在我们之前的学习中,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. 【12c OCP】CUUG OCP认证071考试原题解析(36)

    36.choose the best answer View the Exhibits and examine the structures of the PRODUCTS, SALES, and C ...

  2. Java中Io流操作-File类的常用操作-创建文件,创建文件夹

    package com.hxzy.IOSer; import java.io.File;import java.io.IOException; public class Demo03 { public ...

  3. LOJ#6047. 「雅礼集训 2017 Day10」决斗(set)

    题面 传送门 题解 这么简单一道题我考试的时候居然只打了\(40\)分暴力? 如果我们把每个点的\(a_i\)记为\(deg_i-1\),其中\(deg_i\)表示有\(deg_i\)个数的\(A_i ...

  4. 【vim】正常模式下的一般操作

    正常模式一般用于浏览文本,其实也就是通过键盘命令让光标在文本中跳来跳去,在任何模式下按一次或两次<Esc>会进入正常模式. 基本思想 vim对光标的定位操作非常精确和高效,这是它的一个非常 ...

  5. Objective-C Associated Objects 的实现原理

    我们知道,在 Objective-C 中可以通过 Category 给一个现有的类添加属性,但是却不能添加实例变量,这似乎成为了 Objective-C 的一个明显短板.然而值得庆幸的是,我们可以通过 ...

  6. 网络基础 04_IP编址

    1 IP地址简介 什么是IP地址 在IP网络中,任何一个节点都需要一个唯一的IP IPV4 :32位 点分十进制 2 IP编址分类 有类编址 IP地址的类别 IP地址类型 网络地址:指代网络的地址.在 ...

  7. 1059 C语言竞赛 (20 分)

    #include <iostream> #include <iomanip> #include <cmath> using namespace std; <& ...

  8. swagger注释API详细说明

    API详细说明 注释汇总 @RequestMapping此注解的推荐配置 value method produces 示例: @ApiOperation("信息软删除") @Api ...

  9. Ubuntu+Mac使用飞鸽传书iptux进行互通

    iptux不能直接与Mac版的IPMessage进行文件传输,但是可以和Windows的IPMessage进行互通.如果要实现Ubuntu和Mac下互通,就必须编译同一套代码,因为使用C++写的,所以 ...

  10. VS2015 release模式下进行debug调试

    有时候软件发布,又不得不调试其中的某个dll模块, 这时候就需要在发布的release版本的软件中来调试其中的dll模块了. vs2015设置: 1.Release模式下右键工作属性,选择C/C++, ...