好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解决Properties属性文件中的值有等号和换行的小问题

Properties属性文件中的值有等号和换行

Spring配置Shiro的过滤器时,有个filterChainDefinitions属性,值中有等号有换行,尝试写到Properties属性文件中遇到问题

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!-- 配置shiro过滤器 -->

< bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean" >

     <!-- 表示现在要配置的是一个安全管理器 -->

     < property name = "securityManager" ref = "securityManager" />

     <!-- 出现错误之后的跳转路径的配置 -->

     < property name = "loginUrl" value = "/login.html" />

     <!-- 认证失败之后的跳转路径页面 -->

     < property name = "unauthorizedUrl" value = "/login.html" />

     <!-- 登录成功之后的跳转访问路径 -->

     < property name = "successUrl" value = "/pages/welcome.jsp" />

 

     < property name = "filterChainDefinitions" >

          < value >

             /admin=authc

             /logout=logout

             /xxxxxx=user

          </ value >

     </ property >

</ bean >

Properties属性文件可以这样写

?

1

2

3

4

5

6

shiro.loginUrl=/login.html

shiro.unauthorizedUrl=/login.html

shiro.successUrl=/pages/welcome.jsp

shiro.filterChainDefinitions=/admin=authc \n\

                         /logout=logout \n\

                         /info=authc

后面的等号不需要转义,\n表示值中的换行,再加个转义符\表示值还没结束,这样就没问题了

?

1

2

3

4

5

6

7

8

9

10

< bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean" >

     < property name = "securityManager" ref = "securityManager" />

     < property name = "loginUrl" value = "${shiro.loginUrl}" />

     < property name = "unauthorizedUrl" value = "${shiro.unauthorizedUrl}" />

     < property name = "successUrl" value = "${shiro.successUrl}" />

 

     < property name = "filterChainDefinitions" >

         < value >${shiro.filterChainDefinitions}</ value >

     </ property >

</ bean >

处理properties文件中key包含空格和等号的情况

在properties文件中都是以key=value的方式存储的,在java代码中用java.util.Properties的load方法,存储在一个map中,当key中有空格和等号的时候,要用\(斜杠)进行转义,而用xml的话,就没有转义这么麻烦了,所以推荐使用xml了。

处理方案

Spike.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import java.io.IOException;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.Properties;

public class Spike {

  public static void main(String[] args) throws Exception {

   readProperties();

   System.out.println( "==================================================" );

   readXml();

  }

  private static void readProperties() throws IOException {

   Properties props = new Properties();

   InputStream inStream = Spike. class .getResourceAsStream( "Mock.properties" );

   props.load(inStream);

   Enumeration enums = props.propertyNames();

   while (enums.hasMoreElements()) {

    String key = (String) enums.nextElement();

    System.out.println( "Property--->>>>[" + key + "]    " + "Value--->>>>" + props.getProperty(key));

   }

  }

  private static void readXml() throws IOException {

   Properties props = new Properties();

   InputStream inStream = Spike. class .getResourceAsStream( "Mock.xml" );

   props.loadFromXML(inStream);

         Enumeration enums = props.propertyNames();

   while (enums.hasMoreElements()) {

    String key = (String) enums.nextElement();

    System.out.println( "Property--->>>>[" + key + "]    " + "Value--->>>>" + props.getProperty(key));

   }

  }

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://www.cnblogs.com/zhijian233/p/9881437.html

查看更多关于解决Properties属性文件中的值有等号和换行的小问题的详细内容...

  阅读:26次