在春季更新应用程序范围的数据?
我有一个带有 spring 和 hibernate 的应用程序,我想用此数据更新 Jsp 页面。 我每次都用这个类更新应用程序范围,
public class PutDataInApplication implements ServletContextAware{
int i = 0;
javax.servlet.ServletContext servletContext;
@Scheduled(fixedDelay=2)
public void shout(){
setServletContext(servletContext);
}
@Override
public void setServletContext(ServletContext servletContext) {
// TODO Auto-generated method stub
servletContext.setAttribute("value", i++);
}
}
我想在 jsp 中使用它
Value is :: ${applicationScope.value}
,但它只显示 Value is :: 0
我想每次都显示新数据。如何做到这一点,i 值是递增 serServletContext() 方法。 实际上,我必须调用一种方法来代替 i,但如果我每次都显示更新的 i,我也可以这样做。 ** 与服务器推送方法的任何使用 **
I have an application with spring with hibernate, and I want to update the Jsp page with this data.
I am updating application scope every time with this class
public class PutDataInApplication implements ServletContextAware{
int i = 0;
javax.servlet.ServletContext servletContext;
@Scheduled(fixedDelay=2)
public void shout(){
setServletContext(servletContext);
}
@Override
public void setServletContext(ServletContext servletContext) {
// TODO Auto-generated method stub
servletContext.setAttribute("value", i++);
}
}
I want to use this in jsp with this
Value is :: ${applicationScope.value}
but it showing only Value is :: 0
I want to show new data every time. How to do this,i value is incrementing serServletContext() method.
In real I have to call one method in the place of i, but if i show updated i every time i can do that also.
** any use with server - push method **
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是我没有完全遵循你的场景。
但是调用
servletContext.setAttribute("value", i++);
只会为名为“value”的属性设置 0,然后递增
i
。开始编辑
这是代码片段中发生的情况。
i=0
shout()
被调用setServletContext(..)
setServletContext(..)
被调用,i
的值i
会递增,即; i=1另外,您看到的行为与 spring 无关,只是您以一种棘手的方式使用后缀运算符(variable++)。
结束编辑
如果您希望在调用
servletContext.setAttribute
时看到i
递增,则必须使用前缀运算符 (++variable)尝试
servletContext.setAttribute("value", ++i);
阅读后缀/前缀运算符和 检查此示例程序。
每次设置全局
i
始终初始化为 0。看起来您的目标是获取“value”属性的整数值,然后递增它。May be I did not follow your scenario completely.
But calling
servletContext.setAttribute("value", i++);
would just set 0 for attribute named "value" and then increment
i
.Begin edit
Here is what is happening in your code fragment.
i=0
shout()
gets calledsetServletContext(..)
setServletContext(..)
gets calledi
i
gets incremented i.e; i=1Also the behavior you see is not related to spring, it is just that you are using postfix operator ( variable++) in a tricky way.
End edit
If you want to see
i
incremented while you callservletContext.setAttribute
you must use prefix operator ( ++variable)try
servletContext.setAttribute("value", ++i);
Read up on postfix/prefix operators and check this example program.
Plus each time you are setting the global
i
which is always initialized to 0. Looks like your goal is rather to get integer value of "value" attribute and then increment it.