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

 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)...");
	}

	@Override
	public void attributeAdded(ServletContextAttributeEvent args) {
		System.out.println("ServletContextAttributeListener---(attributeAdded)...");
	}

	/**
	 * 后台更新分类数据的时候将清空ServletContext,此方法将监听,并重新添加blogTypeList
	 */
	@Override
	public 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)...");
	}

	@Override
	public 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");
不过这样很麻烦每次都需要触发这个清空的事件才能更新。不过比重启服务器好多了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值