0

我正在为后端开发带有 spring 的 struts2 应用程序。
我们正在使用 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

请指教。

4

1 回答 1

0

而不是你的PropertyPlaceholderConfigurer豆子,放:

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

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

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

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

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

您必须在以下位置使用“/users”名称定义该 Action 的 bean action-servlet.xml

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

请查看Spring官方文档中的Spring Struts Integration 。

编辑回答评论:

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">
于 2011-10-14T04:52:54.310 回答