博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day38 19-Spring整合web开发
阅读量:4310 次
发布时间:2019-06-06

本文共 2611 字,大约阅读时间需要 8 分钟。

整合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());//工具类WebApplicationContextUtils          UserService userService =  (UserService) applicationContext.getBean("userService");          userService.sayHello();    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {                doGet(request,response);    }}
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
This is the description of my J2EE component
This is the display name of my J2EE component
UserServlet
cn.itcast.servlet.UserServlet
UserServlet
/userServlet
index.jsp

 

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

你可能感兴趣的文章
MemCache在.NET中使用Memcached.ClientLibrary详解 转发 https://www.cnblogs.com/li150dan/p/9529112.html...
查看>>
DB2查找替换字符串
查看>>
java可变参数
查看>>
SQLServer2008设置开启远程连接
查看>>
C#连接Sybase数据库,Anywhere 8
查看>>
CSS layout入门
查看>>
排序算法—冒泡排序
查看>>
Exchange邮件系统日志查看及管理
查看>>
匿名访问windows server 2008 R2 文件服务器的共享
查看>>
is_authenticate 和 login_required判断用户是否登录
查看>>
购物车
查看>>
java之线程池
查看>>
25 Android中dip、dp、sp、pt和px的区别
查看>>
繁星——JQuery选择器之层级
查看>>
邮件发送
查看>>
LumiSoft.Net 收发邮件
查看>>
通过SQL语句直接实现Excel与数据库的导入导出
查看>>
jenkins-小知识点
查看>>
visio二次开发——图纸解析
查看>>
如何在Windows Mobile下使用.NET Compact Framework画透明图片
查看>>