整合Spring开发环境只需要引入spring-web-3.2.0.RELEASE.jar这个jar包就可以了,因为它已经帮我们做好了.

Spring整合web开发,不用每次都加载Spring环境了。


package cn.itcast.service;public class UserService {public void sayHello(){System.out.println("Hello Spring web.....");     }
}

package cn.itcast.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;*/
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.itcast.service.UserService;@SuppressWarnings("serial")
public class UserServlet extends HttpServlet {
//每次启动Servlet都会加载Spring的环境.每次运行都需要加载Spring的环境.Spring配置环境中的东西如果多了,每次加载Servlet就加载Spring环境肯定不行.public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/*    ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService =  (UserService) applicationContext.getBean("userService");userService.sayHello();*///得把代码改了,否则每次都是来获取Spring的环境.WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());//工具类WebApplicationContextUtilsUserService userService =  (UserService) applicationContext.getBean("userService");userService.sayHello();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}}

<?xml version="1.0" encoding="UTF-8"?>
<!-- 别去schema,schema是文件,本地的文件,你得引那个头 --><beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p"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 id="userService" class="cn.itcast.service.UserService"></bean>
</beans>

<?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_2_5.xsd" version="2.5"><display-name></display-name><!-- 配置监听器ContextLoaderListener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置全局初始化参数 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>UserServlet</servlet-name><servlet-class>cn.itcast.servlet.UserServlet</servlet-class></servlet><servlet-mapping><servlet-name>UserServlet</servlet-name><url-pattern>/userServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/6729544.html

day38 19-Spring整合web开发相关推荐

  1. JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试

    Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring?  Spring是分层的Java ...

  2. Spring整合web开发

    正常整合Servlet和Spring没有问题的 public class UserServlet extends HttpServlet {public void doGet(HttpServletR ...

  3. spring boot 整合web开发之文件上传、静态资源访问、异常处理、返回JSON数据

    目录 springboot 整合web开发 返回json数据 静态资源访问 文件上传 全局异常 1.返回json数据 springboot默认的是jackson-databind做为json处理器.也 ...

  4. spring整合mina开发web项目和简单mina客户端的使用

    场景要求在web项目中使用mina与一些客户端通讯. 一.maven引包 <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  5. spring整合cxf开发rest风格的webservice接口(客户端服务端)

    前面文章记录了在spring中如何整合cxf开发webservice客户端和服务端,其实,相较于原生开发方式,已经有了不少优化.rest风格的开发,作为一种极为流行的开发规范,可以帮助我们更加简洁高效 ...

  6. Spring在web开发中的应用

    (1)在 web 项目中要使用 spring 需要导入一个 jar 包: spring-web-4.2.4.jar包 (2)在 web.xml 文件中配置 Listener <listener& ...

  7. spring整合web

    既然说到web自然就有servlet: public class UserServlet extends HttpServlet { @SuppressWarnings("resource& ...

  8. spring boot web 开发示例

    一.创建Maven工程 创建maven工程,packaging 类型选择jar. 二.配置相关maven依赖. 1,首先你需要在pom中最上方添加spring boot的父级依赖,这样当前的项目就是S ...

  9. spring boot 整合web开发(二)

    目录 自定义错误页 CORS支持(前端跨域请求) 拦截器HandlerInterceptor 启动系统任务 springboot配置AOP 整合servlet.filter.listener 下图为本 ...

最新文章

  1. 如何用体验赢取用户信任?让 Waymo 来教教你
  2. 【pip install psycopg2安装报错】Error: pg_config executable not found.
  3. MySQL服务的启动与停止-使用图形界面工具
  4. 【high-speed-downloader】百度网盘不限速下载 支持 Windows 和 Mac
  5. 《JavaScript机器人编程指南》——1.2 NodeBot是什么,基本词汇还有哪些
  6. 服务器修改重生点,服务器设置重生点
  7. 放心!没人在意你使用的是命令式编程还是声明式编程
  8. 为什么都不想去中科创达_那些过年不想回家的人,都去了哪?
  9. 安卓手机上计算机的各按键功能,手机按键里那些你不知道的功能
  10. 微信开放平台申请流程讲解与注意事项(未完待续)
  11. cgi进程设置多少 宝塔_【存档】新手宝塔建站详细步骤
  12. matlab验证确认和测试,验证和确认快速入门
  13. Windows Vista for Developers——第三部分:桌面窗口管理器
  14. 方叫兽教你如何正确的赚钱
  15. 博士毕业论文英文参考文献换行_写毕业论文时,需要掌握这10个最实用的Word技巧...
  16. 下载chrome的.crx文件
  17. Linux没有默认设置root密码
  18. 缺少lib库文件解决方法
  19. Weblogic历史漏洞复现
  20. Python使用ffmpeg完美解决方案(避坑必看)

热门文章

  1. Qt 独立运行时伴随CMD命令窗口
  2. javascript设计模式系列 - LukeLin - 博客园
  3. Maven项目 之eclipse操作篇
  4. HTTP协议(3)浏览器的使用之查看源码
  5. 区块链教程Fabric1.0源代码分析流言算法Gossip服务端二
  6. 真的要做一辈子的程序员吗?来自10年程序员的心声
  7. 线性表的顺序存储结构之顺序表类的实现_Java
  8. maven打包指定main函数
  9. Struts2 文件上传,下载,删除
  10. Node.js有了新的管理者