从 applicationcontext.xml 获取 null 值

发布于 2024-12-10 05:23:49 字数 911 浏览 0 评论 0原文

我正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2024-12-17 05:23:49

而不是您的 PropertyPlaceholderConfigurer bean,请输入:

<context:property-placeholder location="classpath:path/to/database.properties"
                              ignore-unresolvable="false"/>

这样,如果找不到该属性,它将会抱怨。否则,您的类路径中可能有另一个“database.properties”文件,但它根本没有这样的属性。

确保“path/to/database.properties”位于您的类路径中。如果database.properties本身就是你的类路径,那么不需要“path/to”=>只是 classpath:database.properties

您还必须使用 ContextLoaderPlugin 将 Spring 配置为将 Action 作为 bean 进行管理,并且必须在 Struts 配置中使用 bean 名称。如果您的 struts-config.xml 文件中有以下内容:

<action path="/users" .../> 

您必须在 action-servlet.xml 中使用“/users”名称定义该 Action 的 bean:

<bean name="/users" .../>

请采取看看 Spring Struts来自 Spring 官方文档的集成

编辑以回答评论:

context 是一个 XML 命名空间,应在使用它的 XML 文件中定义:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util.xsd">

Instead of your PropertyPlaceholderConfigurer bean, put:

<context:property-placeholder location="classpath:path/to/database.properties"
                              ignore-unresolvable="false"/>

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 => just classpath: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 your struts-config.xml file:

<action path="/users" .../> 

You must define that Action's bean with the "/users" name in action-servlet.xml:

<bean name="/users" .../>

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:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util.xsd">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文