首发地址:http://inmethetiger.iteye.com/blog/1678794
一个注解版的Spring mvc程序和一个非注解版的Spring mvc程序比起来有和区别
1:控制器不需要集成,而是在类上采用@Controller,并在方法上用@RequestMapping,表明对应处理类。
2:在xxxx-servlet.xml中增加如下代码:
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="org.spring.controller"></context:component-scan>
3:在xxxx-servlet.xml中删除控制器的声明,由容器自动寻找
4:将handlerMapping改为,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping。
--------------------------------------------------------------------------------------------------------------------------------------------------
下面是一个简单实例。未改动的地方就不贴代码了,主要是把改动的地方帖代码
控制器:改成注解方式
package org.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controllerpublic
class LoginController {
@RequestMapping(value = "/login.do")
protected ModelAndView anyName(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("login", "success");
mv.setViewName("success");
return mv;
}
}
因为,这个不好注释,当然也因为我懒,怎么修改,可以参考前面例子中的代码
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="org.spring.controller"></context:component-scan>
<!-- HandlerMapping -->
<!-- 帮助DispatchServlet进行web请求的url到具体处理类的匹配,默认为BeanNameUrlHandleMapping -->
<!-- <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean> -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<!-- Controller -->
<!-- 控制器,自己实现,收集参数,绑定参数到命令对象,调用业务对象,选择视图模型,返回视图,模型,数据 -->
<!-- <bean name="/login.do" class="org.spring.controller.LoginController"></bean> -->
<!-- ViewResover -->
<!-- DispatcherServlet根据Controller中指定的逻辑视图查找相应的视图实现 -->
<bean id="defaultViewResover"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2"></property>
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
注意,被注释掉的部分。
当然,这是最浅显的基于注解的Spring mvc。
比如其他的注解符号,不如参数传递等都没有涉及。
分享到:
相关推荐
spring mvc mybatis 注解版 可直接使用
spring-mvc注解详情 ,网页版介绍注解,如果不好看,可以搜索里面的关键句子,百度就出来原文了,这方便没有网络的朋友学习
如果使用spring mvc 3.2+和servelt 3+容器(比如tomcat8),那么web.xml和applicationContext.xml都不是必须的,可使用基于注解的配置: 基于配置的集成例子源代码:
spring mvc +hibernate注解版测试增,删,改,查 简单xml配置应用
spring spingmvc 集成elasticSearch 5.5.x版本 ,基本的增删改查.完成
Spring MVC AOP通过注解方式拦截Controller等实现日志管理demo版本2
spring mvc hibernate 实现事务管理 jar包管理用maven,启动服务器用jetty,里边还有memcached数据库,但是本例子没有用到。
工具类数据校验 jsp自定义标签 Spring自定义注解 默认requestMapping 1.1.2 代码生成器 1.1.3 首页修改 dateformat.js 时间参数转换 SpringMVC配置文件集中 快递参数接口 1.1.4 des加解密字符串和文件 1.1.5 redis...
使用Spring、SpringMvc、Mybatis-plus搭建的后端框架,使用全注解方式进行配置,ssm框架搭建,详细的整合教程
参考Spring MVC Web框架,实现一个MVC Web框架.利用Java EE技术参考Spring ...- Spring为展现层提供的基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一 - Spring3.0后全面超越Struts2,成为最优秀的MVC框架
啥都不说了:包含有以下文档,都是哥这几年学的 1.51CTO下载-spring 2.0 api ...3.Spring 3.x企业应用开发实战 4.spring+MVC中文教程pdf 5.Spring2.5....6.SpringGuide ...11.使用注解方式配置的spring mvc 3.0
不想多说什么,spring mvc+myBatis_config通过xml的配置版,如需要其他请参考spring mvc+myBatis_annotate注解版,可直接使用
基于maven构建,采用注解方式,对项目要求不高的可以使用
4.程序中使用的是spring mvc注解的方式,可扩展的地方也很多,我是从网上下的maven项目,然后自己改成web项目,除了整个架子外,其他的都是我自己的东西 5.目前能实现的功能就是走一个流程,适合初学者研究 我深知...
Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对Struts 2的超越。现在越来越多的开发团队选择了Spring MVC。
完整的描述了整个整合的过程,并且对其拆分讲解,没有非常繁琐的原理,都是针对项目讲解
所以有些公司开发使用的就是早期的 Spring Cloud,而在后期公司想进行版本升级的时候,改成使用Spring Cloud OpenFeign,但 Feign 使用的是他自己独有的那一套注解,如果我们想全部进行修改成 Spring MVC注解的话,...
springmvc独立运行jar包集合,经过测试可以运行,Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对Struts 2的超越。现在...
Spring MVC基础入门 什么是MVC 什么是SpringMVC Spring MVC 快速入门 Spring MVC 执行流程分析 Spring MVC 三大组件 Spring MVC 核心源码分析 @RequestMapping注解 Spring MVC请求参数封装 Spring MVC 基本类型封装 ...