当前位置: 代码迷 >> 综合 >> 使用Servlet监听器动态更新select下拉列表
  详细解决方案

使用Servlet监听器动态更新select下拉列表

热度:13   发布时间:2023-09-23 11:15:44.0

 html  layui +thymeleaf 内容是博客分类(自己正在写的这个博客需要用到这个)

	<select id="blogTypeId" lay-verify="required" name="blogTypeId"lay-search><option value="selected"></option><option th:each="blogType:${application.blogTypeList}"th:value="${blogType.id}" th:text="${blogType.typeName}">博客类型A</option></select>

监听器 

实现 ServletContextListener接口达到初始化加载sleect option

但是每次更新需要重服务器,太麻烦,修改也不能及时自己更新

因为这个数据是放在application中的所有可以再实现一个接口ServletContextAttributeListener,再监听修改操作

监听器

package cn.edu.jxnu.blog.listener;import java.util.List;import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;import cn.edu.jxnu.blog.domin.BlogType;
import cn.edu.jxnu.blog.service.BlogTypeService;@Component
/*** @Description 监听程序初始化*/
public class InitBlogTypeData implements ServletContextAttributeListener,ServletContextListener {private static ApplicationContext applicationContext;public void contextInitialized(ServletContextEvent sce) {// 先获取servlet上下文ServletContext application = sce.getServletContext();// 获取spring web上下文applicationContext = WebApplicationContextUtils.getWebApplicationContext(application);// System.out.println("上下文:"+applicationContext);BlogTypeService blogTypeService = applicationContext.getBean(BlogTypeService.class);// System.out.println("blogTypeService="+blogTypeService);List<BlogType> blogTypeList = blogTypeService.getBlogTypeData();application.setAttribute("blogTypeList", blogTypeList);System.out.println("ServletContextListener---(contextInitialized)...");}public void contextDestroyed(ServletContextEvent sce) {System.out.println("ServletContextListener---(contextDestroyed)...");}@Overridepublic void attributeAdded(ServletContextAttributeEvent args) {System.out.println("ServletContextAttributeListener---(attributeAdded)...");}/*** 后台更新分类数据的时候将清空ServletContext,此方法将监听,并重新添加blogTypeList*/@Overridepublic void attributeRemoved(ServletContextAttributeEvent args) {// 先获取servlet上下文ServletContext application = args.getServletContext();// 获取spring web上下文applicationContext = WebApplicationContextUtils.getWebApplicationContext(application);// System.out.println("上下文:"+applicationContext);BlogTypeService blogTypeService = applicationContext.getBean(BlogTypeService.class);// System.out.println("blogTypeService="+blogTypeService);List<BlogType> blogTypeList = blogTypeService.getBlogTypeData();application.setAttribute("blogTypeList", blogTypeList);System.out.println("ServletContextAttributeListener---(attributeRemoved)...");}@Overridepublic void attributeReplaced(ServletContextAttributeEvent args) {System.out.println("ServletContextAttributeListener---(attributeReplaced)...");}}
同时每次在controller的操作触发该监听 

controller方法签名类似这种,传入一个request 

@RequestMapping(value = "/delete")public String deleteBlog(@RequestParam(value = "ids", required = false) String ids,HttpServletResponse response, HttpServletRequest httpServletRequest)

在请求返回的同时触发监听器事件,清空或者改变application键值。 这里是选择直接删除
ServletContext application = httpServletRequest.getServletContext();application.removeAttribute("blogTypeList");
不过这样很麻烦每次都需要触发这个清空的事件才能更新。不过比重启服务器好多了。




  相关解决方案