基于注解的Spring整合SpringMVC开发流程?
来源:互联网 发布:淘宝集市店 编辑:程序博客网 时间:2024/06/05 14:23
先声明:
我使用maven管理项目
在pom文件中导入远程中央仓库的包(这里仅导入springwebmvc包即可)
(1)给出我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.edu.hdu</groupId> <artifactId>mavenDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mavenDemo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring-webmvc:导入它之后,spring依赖的包会自动被下载。 --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.9.RELEASE</version></dependency> </dependencies> <build> <finalName>mavenDemo</finalName> <plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins> </build></project>
(2)在web.xml下的监听器中,加载Spring配置文件applicationContext.xml,因为listener比servle先执行。
<!-- 在监听器里加载Spring配置文件 --><context-param><param-name>contextConfigLocation</param-name><paramvalue>classpath:config/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
(3)在applicationContext.xml添加组件扫描
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><context:component-scan base-package="controller" /><!--spring会扫描带有注解(@controller,@Service,@Repository)的类 --><mvc:annotation-driven /><!-- 启动mvc注解扫描 --></beans>
(3)在web.xml下的servlet中,加载前端控制器的映射文件dispatcherServlet-servlet.xml
<servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/dispatcherServlet-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
(4)在dispatcherServlet-servlet.xml中添加映射器和视图解析器。
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><!-- 配置映射器与视图解析器 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>
(5)在src/main/java下的controller包下的java类中添加注解(@controller,@Resource等等)
package controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class person {@RequestMapping(value="/dream")public String action(){return "happiness";}}
(6)在webapp/WEB-INF/下创建自己要访问的jsp网页,这里【插上一张项目结构图】。
阅读全文
0 0
- 基于注解的Spring整合SpringMVC开发流程?
- 基于注解的mybatis整合spring开发流程?
- spring mvc系列文章 - springmvc spring mybatis ibatis freemark整合开发(2.0版:基于注解)
- SSM(Spring,SpringMVC,MyBatis)整合开发流程
- 基于注解的SpringMVC整合JPA
- 基于注解的SpringMVC整合JPA
- 基于注解的SpringMVC整合JPA
- 基于注解的SpringMVC开发
- 创建基于注解的springmvc的流程
- s2sh: 基于注解的整合流程
- Spring与hibernate的整合(基于注解
- 基于注解的mybatis和spring整合
- 基于注解的mybatis和spring整合
- 基于注解的mybatis和spring整合
- 基于注解的spring boot整合Druid
- Spring+Hibernate基于注解整合的单元测试
- SpringMVC+Spring+Mybatis三大框架的整合流程(基于maven工程)
- SpringMVC+Spring+Mybatis基于Maven的整合
- sqlserver函数
- Oracle in exists
- 58 同城 iOS 客户端 Hybrid 框架探索
- java 实现http响应
- 视频学习笔记:Android ffmpeg解码多路h264视频并显示
- 基于注解的Spring整合SpringMVC开发流程?
- 链表——Java实现
- java-访问修饰符
- ngixn如何控制多进程连接的平衡
- 图解集合3:CopyOnWriteArrayList
- hibernate中merge的使用以及与update的区别
- ACM一些常用小技巧(博客收集+总结)
- hibernate一对一外键映射
- Oracle数据库的基本操作指令