您的位置:首页 > 其它

不用发送请求,直接在页面中获取数据,需要实现接口ServletContextAware

2015-12-24 16:11 761 查看
public class WebLoader implements ServletContextAware {

/**
* servletContext
*/
static public ServletContext servletContext;

/**
* @author scrum
* @created 2013-6-23 下午03:38:30
* @lastModified
* @history
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
}

/**
* @author xmzhu2
* @created 2013-6-6 下午5:14:46
* @lastModified
* @history
* @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
*/
public void setServletContext(ServletContext arg0) {
String startDate = DateUtil.getStartDate();
String endDate = DateUtil.getEndDate();
arg0.setAttribute("startDate", startDate  );
arg0.setAttribute("endDate", endDate  );
servletContext = arg0;
}
前台页面可以直接使用 el表达式获取 startDate 和endDate 的值

在 ????-servlet.xml 中还需要配置一下这个类,让springMVC 实例化这个类

这个类需要实现 ServletContextAware 接口 和 接口中的方法 setServletContext( ServeltContext sc );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: