SpringMVC(2)—SpringMVC整合Spring的HelloWorld
一、这是一个SpringMVC框架的案例HelloWorld
功能:HelloWorld
二、SpringMVC运行流程
1.流程
请求——>springDispatcherServlet的url-pattern检查SpringMVC中是否存在对应的映射
无:检查是否配置的<mvc:default-servlet-handler/>
无:404, 控制台打印 No mapping found for HTTP request with URI[/xx/xx] in DispatcherServlet
有:寻找目标资源
有:——>HandlerMapping获取HandlerExceptionChain对象——>获取HandlerAdapter对象——>调用拦截器的PreHandle方法
——>调用目标Handler的目标方法得到ModelAndView对象——>调用拦截器的postHandle方法,有无异常:
有:由HandlerExceptionResolver组件处理异常,得到新的ModelAndView对象,——>无:———>由ViewResolver组件根据ModelAndView对象得到实际的view——>渲染视图——>调用拦截器的afterCompletion方法
三、在Spring的环境下使用SpringMVC
问题:1.需要进行Spring整合SpringMVC吗
2.还是否需要加入SpringIOC容器
3.是否还要在web.xml文件中配置启动Spring IOC容器的的ContextLoaderListener
答案:
1. 需要: 通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).
实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao.
2. 不需要: 都放在 SpringMVC 的配置文件中. 也可以分多个 Spring 的配置文件, 然后使用 import 节点导入其他的配置文件
问题:
若 Spring 的 IOC 容器和 SpringMVC 的 IOC 容器扫描的包有重合的部分, 就会导致有的 bean 会被创建 2 次.
答案:
方法1. 使 Spring 的 IOC 容器扫描的包和 SpringMVC 的 IOC 容器扫描的包没有重合的部分,若分模块开发这种方法可能会不太合适
方法2. 使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解
所以使用方法2来解决SpringMVC整合Spring时,出现一个注解被两次扫描的问题
步骤
1.在SpringMVC配置文件中,配置扫描@Controller这个注解
2.在Spring配置文件中配置不扫描@Controller这个注解
注意:
**SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean.
返回来呢 ? 反之则不行. Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean!**
四、代码
1.添加jar包
classmate-0.8.0.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-fileupload-1.2.1.jar
commons-io-2.0.jar
commons-logging-1.1.1.jar
commons-logging-1.1.3.jar
hibernate-validator-5.0.0.CR2.jar
hibernate-validator-annotation-processor-5.0.0.CR2.jar
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
jboss-logging-3.1.1.GA.jar
jstl.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
standard.jar
validation-api-1.1.0.CR1.jar
2.页面
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="helloworld">Hello World</a>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Hello World!</h3>
</body>
</html>
other.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Other page!</h3>
</body>
</html>
3.Controller
HelloWorld.java
package com.Spring.mvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
@Autowired
private UserService userService;
public HelloWorld() {
System.out.println("Hello World Constructor...");
}
@RequestMapping("/helloworld")
public String hello(){
System.out.println("Hello World!");
return "success";
}
}
4.Service
UserService.java
package com.Spring.mvc;
import org.springframework.stereotype.Service;
//SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean,反之则不行
/*@Autowired
private HelloWorld helloWorld;*/
@Service
public class UserService {
public UserService() {
System.out.println("UserService Constructor...");
}
}
5.配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Demo20_SpringMVC_HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 1.配置Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2.配置SpringMVC的配置文件 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
bean.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"
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-4.0.xsd">
<!-- Spring IOC 容器 -->
<!-- 1.自动扫描的包 -->
<!-- <context:component-scan base-package="com.Spring.mvc"></context:component-scan> -->
<context:component-scan base-package="com.Spring.mvc" >
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 2.配置数据源,整合其他框架,事务等 -->
</beans>
springmvc.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: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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- Spring MVC的IOC容器 -->
<!-- 1.自动解析的包 -->
<!-- <context:component-scan base-package="com.Spring.mvc"></context:component-scan> -->
<context:component-scan base-package="com.Spring.mvc" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 2.视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 3.为了能够访问,没有Mapping映射的页面,添加了<mvc:default-servlet-handler/>
但是使用了这个配置之后@RequestMapping映射作用失效,所以又配置这个<mvc:annotation-driven></mvc:annotation-driven>
-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 4.为了整合Spring IOC容器,需要在web.xml中配置启动Spring IOC容器的Listener -->
</beans>
SpringMVC(2)—SpringMVC整合Spring的HelloWorld的更多相关文章
- 整合spring,springmvc和mybatis
我创建的是maven项目,使用到的依赖架包有下面这些: <dependencies> <dependency> <groupId>org.springframewo ...
- spring和springmvc之间的整合
一.springmvc就是运行在spring的环境下,这两者是否需要进行整合,即:是不是要把service .dao . 事务 .和其它框架的整合放在springmvc的配置文件中.这样子在技术层面上 ...
- 搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)
整合Spring和SpringMVC 之前已经整合了spring和mybatis,现在在此基础上整合SSM. 项目目录: 思路:SpringMVC的配置文件独立,然后在web.xml中配置整合. (1 ...
- 搭建Spring + SpringMVC + Mybatis框架之二(整合Spring和Mybatis)
整合Spring和Mybatis 首先给出完整的项目目录: (1)引入项目需要的jar包 使用http://maven.apache.org作为中央仓库即可. Spring核心包,mybatis核心包 ...
- spring+springmvc+maven+mybatis整合
jar包依赖:网址search.maven.org 1.spring :spring-core 2. myhabits:myhabits 3.整合spring和myhabits: ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM
写在前面的话 承接前文<Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven>,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普 ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(八)mysql中文查询bug修复
写在前面的话 在测试搜索时出现的问题,mysql通过中文查询条件搜索不出数据,但是英文和数字可以搜索到记录,中文无返回记录.本文就是写一下发现问题的过程及解决方法.此bug在第一个项目中点这里还存在, ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十一)SVN服务器进阶
日常啰嗦 上一篇文章<Spring+SpringMVC+MyBatis+easyUI整合基础篇(十)SVN搭建>简单的讲了一下SVN服务器的搭建,并没有详细的介绍配置文件及一些复杂的功能, ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十二)阶段总结
不知不觉,已经到了基础篇的收尾阶段了,看着前面的十几篇文章,真的有点不敢相信,自己竟然真的坚持了下来,虽然过程中也有过懒散和焦虑,不过结果还是自己所希望的,克服了很多的问题,将自己的作品展现出来,也发 ...
随机推荐
- combination sum(I, II, III, IV)
II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...
- 前端本地存储localStorage
1.突破cookie 4K限制,一般浏览器支持5M 2.增 删 改 查 <!DOCTYPE html> <html lang="en"> <head& ...
- Nessus忘记密码的解决
进入到Nessus安装目录下
- net core体系-web应用程序-4asp.net core2.0 项目实战(1)-4项目前端说明
本文目录1. 摘要2. UI界面展示 3. 主要技术点4. 总结 1. 摘要 平时比较忙,写一篇文章可能跨度好几天,希望各位多多包涵.闲言少叙直接进入正题. 2. UI界面 NCMVC用的就图二, ...
- python模块安装查看、包制作
一. 模块安装 ubuntu : apt-get install python-pip redhat: yum install python-pip pip install 模块 pip instal ...
- 基于C语言的Socket网络编程搭建简易的Web服务器(socket实现的内部原理)
首先编写我们服务器上需要的c文件WebServer.c 涉及到的函数API: int copy(FILE *read_f, FILE * write_f) ----- 文件内容复制的方法 int Do ...
- HTML的5种空格表示
HTML提供了5种空格实体(space entity),它们拥有不同的宽度,非断行空格( )是常规空格的宽度,可运行于所有主流浏览器.其他几种空格( )在不同浏览器中宽度各异. ...
- 【未解决】Linux下PHP安装扩展Mysql的问题
[步骤分析] 1.在PHP源码包下定位到指定位置:源码包/ext/mysqli 注1:网上很多地方红字部分是mysql,其实PHP5以后已经不用mysql了. 2.运行:/usr/local/php/ ...
- JMeter-java.lang.OutOfMemoryError: PermGen space错误
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被 ...
- C#多线程和线程池问题
static void Main(string[] args) { Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法 threadA ...