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容器管理 @ ...
随机推荐
- SQL Server 根据树状结构表生成以/号分割的路由字符串
很多情况下,我们有必要把树形结构进行数据梳理.比如,要方便的过滤出一个父节点下的所有子节点等等... 这个时候,我们可以生成一个路径表字符串,在应用时只需要对该字符串进行索引即可达成目的. 目标:按图 ...
- Java几个基本概念
To xj 编译:test.java->test.class反编译:test.class->test.java打jar包:test.class->test.jar打war包:test ...
- BZOJ 3110 [Zjoi2013]K大数查询 (CDQ分治+树状数组)
题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是 ...
- python 去除字符串的首末两端的空白字符
my_str = " adsffff adsfsad " my_str.strip() 使用strip()默认将 str 两端的空白字符去除掉 同时还有rstrip() 和 lst ...
- Java中将图片保存到数据库中
在实际的开发中,我们可能需要将图片.影音等文件直接保存到数据库中,然后通过编程方式将数据读出进行使用.例如将读出的图片数据显示出来,将读出的电影文件播放出来. 二进制数据直接保存到文件和从文件中读出非 ...
- 算法导论-MIT笔记
第一部分 Analysis of Algorithms 算法分析是关于计算机程序性能(performance)和资源利用的理论研究 1 What's more important than perfo ...
- Spring注解大全,汇总版
Spring使用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...
- 简单创建一个完整的struts2框架小程序
要完成一个struts2框架的搭建, 1.首先应该从官网上下载最新的jar包,网络连接:http://struts.apache.org/download.cgi#struts2514.1,选择下载F ...
- 编程开发之--java多线程学习总结(1)问题引入与概念叙述
1.经典问题,火车站售票,公共票源箱,多个窗口同时取箱中车票销售 package com.lfy.ThreadsSynchronize; /** * 解决办法分析:即我们不能同时让超过两个以上的线程进 ...
- localstrage、cookie、session等跨域和跨页面监听更新问题
localstrage.cookie.session等跨域和跨页面监听更新问题