当前位置: 首页 > wzjs >正文

网站建设及网络推广简单网站建设优化推广

网站建设及网络推广,简单网站建设优化推广,开网站做商城怎么样,上那个网站做测试用例一、自行创建监听器来模拟springweb架构中的监听器流程 监听器的作用:当web服务器开始启动时,使用监听器监听初始化周期,并将spring配置文件放置在Servlet的最大域中。一旦服务器启动后就可以直接从域中使用gertAtribute方法去获取到spring的…

一、自行创建监听器来模拟springweb架构中的监听器流程

监听器的作用:当web服务器开始启动时,使用监听器监听初始化周期,并将spring配置文件放置在Servlet的最大域中。一旦服务器启动后就可以直接从域中使用gertAtribute方法去获取到spring的配置文件applicationContext.xml。

然后会设置一个工具类,通过工具类作为中介来返回配置文件信息。

自行配置逻辑应是如下:

首先是Controller层

public class UserController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {//调用applicationContext配置文件首先通过工具类WebApplicationContextUtils//再通过该类中的getApplicationContext从最大域中获得监听器上传的getApplicationContext配置文件内容//调用顺序为:WebApplicationContextUtils  ->  getApplicationContext// ->  (ApplicationContext) servletContext.getAttribute("applicationContext")ApplicationContext applicationContext =WebApplicationContextUtils.getApplicationContext(req.getServletContext());userService = (UserService) applicationContext.getBean("userService");userService.save();}

然后是web.xml配置文件

<web-app><display-name>Archetype Created Web Application</display-name>
  <context-param><param-name>contextConfigLocation</param-name><param-value>applicationContext.xml</param-value></context-param>
<!--  监听器配置--><listener><listener-class>listener.ContextLoaderListener</listener-class></listener>
  <servlet><servlet-name>UserController</servlet-name><servlet-class>com.hxy.usercontroller.UserController</servlet-class></servlet><servlet-mapping><servlet-name>UserController</servlet-name><url-pattern>/userController</url-pattern></servlet-mapping>
</web-app>

监听器类的创建

public class ContextLoaderListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {ServletContext servletContext = sce.getServletContext();String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");sce.getServletContext().setAttribute("applicationContext", contextConfigLocation);System.out.println("ContextLoaderListener contextInitialized");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {}
}
工具类的创建
//设置一个工具,用于返回spring配置类:applicationContext
//外部只需要调用这个类的方法getApplicationContext,就能够得到返回的applicationContext
public class WebApplicationContextUtils {public static ApplicationContext getApplicationContext(ServletContext servletContext) {return (ApplicationContext) servletContext.getAttribute("applicationContext");}
}

二、使用spring框架中的监听器直接创建项目

只需将web.xml中的监听器配置文件改为spring-web框架自带的,同时在Controller层获取配置文件时调用spring框架自带的工具类即可,省去了创建监听器以及工具类的过程

三、总结

使用监听器的作用是简化提取配置文件的过程,方便操作。

监听器的作用在于监听web服务器的初始化过程,将配置文件进行传递,从而方便后续的获取。

通过监听器的模式,实现了解耦,如果修改了配置文件的名称或内容,将不必再在代码中重复修改,这个过程只需要在web.xml配置文件中进行相应修改即可。

http://www.dtcms.com/wzjs/115532.html

相关文章:

  • 一个网站做数据维护需要多久seo网站培训
  • wordpress设置2个网站吗东莞做网站的联系电话
  • 网站建设的banner图小程序定制
  • 在线免费开网站百度seo优化哪家好
  • 男女做暖暖的免费观看网站网络营销是学什么
  • html企业网站系统百度一下打开
  • 足球网站建设八零云自助建站免费建站平台
  • 网站制作需要哪些百度小说搜索风云榜排名
  • 优秀网站网址怎么做seo网站关键词优化
  • 海口网站建设介绍cpa推广平台
  • 某企业网络营销方案seo综合排名优化
  • 石景山 网站建设seo项目经理
  • 母婴网站源码dede外贸网站建设推广公司
  • 米各庄有做网站的吗电商网站有哪些
  • 应式网站谷歌浏览器官网下载
  • 介绍婚纱网站的ppt怎么做网络推广方案有哪些
  • 查网站有没有做推广哈尔滨优化网站方法
  • wordpress 手机管理员密码seo外包 靠谱
  • 建设网站需要多少钱济南兴田德润地址链爱交易平台
  • 织梦网站建设教程seo工作内容有哪些
  • 商河便宜做网站的公司专业网络推广公司
  • 专业性网站 ip网店推广实训报告
  • 网站建设的技术方案模板下载百度seo排名推广
  • 长治网站制作2020十大网络热词
  • asp.net网站开发介绍重庆网站推广联系方式
  • 网站简易后台营销推广活动方案
  • 做科技汽车的视频网站宣传推广方案模板
  • 河南郑州网站建设公司网站自动收录
  • 杭州市做网站央视新闻
  • 做网站比较好的数字百度一下百度主页官网