从 applicationcontext.xml 获取 null 值
我正在开发 struts2 应用程序,后端使用 spring。
我们正在使用database.properties文件,条目如下:
jdbc.url=jdbc:mysql://localhost:3306/myDb
jdbc.username=root
jdbc.password=rooooot
jdbc.csvlocation=C:\myCSV
我在database.properties中添加了以下新条目
enhancePerf.Flag=true
在applicationcontext.xml中我正在获取如下值:-
<bean id="userLogin" scope="prototype"
class="com.hello.something.actions.UserLoginAction">
<property name="perfEnhance" value="${enhancePerf.Flag}"/>
</bean>
在UserLoginAction中声明全局变量perfEnhance并形成setter之后和 getters 方法相同,我仍然没有得到值。
我点击了以下链接:-
http://www.roseindia.net/tutorial /spring/spring3/web/applicationcontext.xml-properties-file.html
请告知。
I am working on struts2 application with spring for back end.
We are using database.properties file and the entries are as follows:
jdbc.url=jdbc:mysql://localhost:3306/myDb
jdbc.username=root
jdbc.password=rooooot
jdbc.csvlocation=C:\myCSV
I added the following new entry in database.properties
enhancePerf.Flag=true
In applicationcontext.xml I am fetching the value like this :-
<bean id="userLogin" scope="prototype"
class="com.hello.something.actions.UserLoginAction">
<property name="perfEnhance" value="${enhancePerf.Flag}"/>
</bean>
After declaring a global variable perfEnhance in UserLoginAction, and forming the setters and getters method of the same, I'm still not getting the value.
I followed the following link:-
http://www.roseindia.net/tutorial/spring/spring3/web/applicationcontext.xml-properties-file.html
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是您的
PropertyPlaceholderConfigurer
bean,请输入:这样,如果找不到该属性,它将会抱怨。否则,您的类路径中可能有另一个“database.properties”文件,但它根本没有这样的属性。
确保“path/to/database.properties”位于您的类路径中。如果
database.properties
本身就是你的类路径,那么不需要“path/to”=>只是classpath:database.properties
您还必须使用
ContextLoaderPlugin
将 Spring 配置为将 Action 作为 bean 进行管理,并且必须在 Struts 配置中使用 bean 名称。如果您的struts-config.xml
文件中有以下内容:您必须在
action-servlet.xml
中使用“/users”名称定义该 Action 的 bean:请采取看看 Spring Struts来自 Spring 官方文档的集成。
编辑以回答评论:
context
是一个 XML 命名空间,应在使用它的 XML 文件中定义:Instead of your
PropertyPlaceholderConfigurer
bean, put:This way if the property is not found, it will complain. Otherwise it seems that you may have another "database.properties" file in your classpath, that simply does not have such a property.
Make sure that "path/to/database.properties" is in your classpath. If
database.properties
itself is your class path, then no "path/to" is needed => justclasspath:database.properties
You also have to configure Spring to manage your Actions as beans, using the
ContextLoaderPlugin
, as well as you have to use bean names in Struts config. If you have the following in yourstruts-config.xml
file:You must define that Action's bean with the "/users" name in
action-servlet.xml
:Please take a look at Spring Struts Integration from official Spring's docs.
EDIT to answer the comment:
context
is an XML namespace that should be defined in the XML file where it is used: