English 中文(简体)
阅读春瓦底等级的财产档案内容
原标题:Reading the content of property file in Spring Validator Class

我正在使用春季3.0。 Can i读到一个当地淹没人的财产档案:C:propmylogin.properties(不在网络应用/应用服务器下)

mylogin.properties

login.heading=Login
login.username=Username
login.password_blank=Password cannot be blank

I want to use the above in my Validator class(java bean class). Please Note : I using MultiAction Controller not SpringFormController

 public class MyUserValidator implements Validator 
    {

@Override
    public void validate(Object command, Errors errors) {
         String msg=null;
     //some conditions checked
         String msg=login.password_blank;-------->get from property file ??
        errors.rejectValue("User",msg,"defaultvalue");   

}

然而,我们却使用资源棒,不能确定如何在有效器内使用这一工具?

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" lazy-init="true"> 
       <property name="basenames"> 
       <list> 

      <value>${deployment.properties.path}/xyzsite/mylogin</value>   

 </list> 
 </property> 
<property name="cacheSeconds" value="${deployment.properties.cache_in_seconds}"/> 
</bean> 

如果使用资源障碍,答案u中究竟有什么变化?

for example if i want to set a PageSize int pagesize=5; ---> get this from property file

mylogin.properties

login.heading=Login
login.username=Username
login.password_blank=Password cannot be blank
login.page_row_size=5
最佳回答

Alright got this sorted by doing following mav= new ModelAndView mav.addAllObjects(errors.getModel());

以及

问题回答

傲慢电文通常最好作为 JavaResourceBundle和JSTL的标准组合处理,但从档案系统的任意地点装载这些财产赢得了一定的工作。

因此,你不得不做更多的手工工作。 最简单的是注入财产档案的位置,然后从中提取财产:

public class MyUserValidator implements Validator {

    private Resource propertiesLocation;

    @Required
    public void setPropertiesLocation(Resource propertiesLocation) {
        this.propertiesLocation = propertiesLocation;
    }

    @Override
    public void validate(Object command, Errors errors) {
        try {
            Properties props = PropertiesLoaderUtils.loadProperties(propertiesLocation);

            String key = "login.password_blank";
            String errorMessage = props.getProperty(key);
            errors.rejectValue("User", key, errorMessage);
        } catch (IOException e) {
            // handle me
        }
    }
}

和在座:

<bean id="userValidator" class="com.x.MyUserValidator">
  <property name="propertiesLocation" value="file:/prop/mylogin.properties"/>
</bean>

注:

errors.rejectValue("User", key, errorMessage);

由于你不再使用<代码>ResourceBundle,你需要在“过失错误信息”中作为第3条理由予以通过。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签