我什么时候使用就有点糊涂了${...}
比较#{...}
.Spring的文档仅使用#{...}
,但有很多示例使用${...}
.此外,当我开始使用SpEL时,我被告知要使用${...}
它并且工作正常.
对于那些困惑的人来说,我将如何使用它
@Component
public class ProxyConfiguration {
@Value("${proxy.host}")
private String host;
@Value("${proxy.port}")
private String port;
:
}
Run Code Online (Sandbox Code Playgroud)
和一些属性文件:
proxy.host=myproxy.host
proxy.port=8000
Run Code Online (Sandbox Code Playgroud)
我的问题是:
Spring 3引入了一种新的表达式语言(SpEL),可以在bean定义中使用.语法本身已经很好地指定了.
不清楚的是,SpEL如何与先前版本中已存在的属性占位符语法进行交互.SpEL是否支持属性占位符,或者我是否必须结合两种机制的语法并希望它们结合起来?
让我举一个具体的例子.我想使用属性语法${x.y.z}
,但添加了由elvis运算符提供的"默认值"语法来处理${x.y.z}
未定义的情况.
我尝试了以下语法但没有成功:
#{x.y.z?:'defaultValue'}
#{${x.y.z}?:'defaultValue'}
第一个给了我
在'org.springframework.beans.factory.config.BeanExpressionContext'类型的对象上找不到字段或属性'x'
这表明SpEL不承认这是一个属性占位符.
第二个语法抛出异常说占位符不被识别,所以占位符解析器是被调用,但未能如预期,因为没有定义属性.
文档没有提到这种交互,所以这样的事情是不可能的,或者它没有记录.
有人设法做到了吗?
好的,我已经为此设计了一个小型,独立的测试用例.这一切都按原样运作:
首先,bean定义:
<?xml version="1.0" encoding="UTF-8"?>
<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
">
<context:property-placeholder properties-ref="myProps"/>
<util:properties id="myProps">
<prop key="x.y.z">Value A</prop>
</util:properties>
<bean id="testBean" class="test.Bean">
<!-- here is where the magic is required -->
<property name="value" value="${x.y.z}"/>
<!-- I want something like this
<property name="value" value="${a.b.c}?:'Value B'"/>
-->
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
然后,琐碎的bean类:
包装测试;
public …
Run Code Online (Sandbox Code Playgroud) 如果我有:
@Autowired private ApplicationContext ctx;
Run Code Online (Sandbox Code Playgroud)
我可以使用其中一个getBean方法获取bean和资源.但是,我无法弄清楚如何获得属性值.
显然,我可以创建一个具有@Value属性的新bean,如:
private @Value("${someProp}") String somePropValue;
Run Code Online (Sandbox Code Playgroud)
我在ApplicationContext对象上调用什么方法来获取该值而不自动装配bean?
我通常使用@Value,但是有一种情况是SPeL表达式需要是动态的,所以我不能只使用注释.
我有一个看起来像这样的XML
<element1>
<element2>
<element3>
<element4>Hello</element4>
<element5>World</element5>
</element3>
<element3>
<element4>Hello2</element4>
<element5>World2</element5>
</element3>
<element3>
<element4>Hello3</element4>
<element5>World3</element5>
</element3>
</element2>
</element1>
Run Code Online (Sandbox Code Playgroud)
我试图使用Xpath得到这样的结果:
Hello.World
Hello2.World2
Hello3.World3
Run Code Online (Sandbox Code Playgroud)
我在下面使用了concat函数但是没有得到正确的结果.
Concat功能:
concat(/element1/element2/element3/element4/text(),".", /element1/element2/element3/element5/text())
Run Code Online (Sandbox Code Playgroud)
结果我得到了:
Hello.World
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到正确的结果?我正在使用XPath与Camel Spring DSL.
编辑:
XQuery,XSLT和SPel中的解决方案也很受欢迎.
编辑
我尝试了字符串连接,但它不起作用:
字符串连接函数:
string-join((/element1/element2/element3/element4/text(), /element1/element2/element3/element5/text()),".")
Run Code Online (Sandbox Code Playgroud)
结果我得到了:
Hello.Hello2.Hello3.World.World2.World3
Run Code Online (Sandbox Code Playgroud) 是否可以使用Spring @Value将属性文件中的值映射到HashMap.
目前我有类似的东西,映射一个值不是问题.但我需要在HashMap过期中映射自定义值.这样的事情可能吗?
@Service
@PropertySource(value = "classpath:my_service.properties")
public class SomeServiceImpl implements SomeService {
@Value("#{conf['service.cache']}")
private final boolean useCache = false;
@Value("#{conf['service.expiration.[<custom name>]']}")
private final HashMap<String, String> expirations = new HashMap<String, String>();
Run Code Online (Sandbox Code Playgroud)
属性文件:'my_service.properties'
service.cache=true
service.expiration.name1=100
service.expiration.name2=20
Run Code Online (Sandbox Code Playgroud)
像这个键映射是否可行:值集
name1 = 100
name2 = 20
我想使用@Value注释来注入Double属性,例如:
@Service
public class MyService {
@Value("${item.priceFactor}")
private Double priceFactor = 0.1;
// ...
Run Code Online (Sandbox Code Playgroud)
并使用Spring属性占位符(属性文件):
item.priceFactor=0.1
Run Code Online (Sandbox Code Playgroud)
我得到例外:
org.springframework.beans.TypeMismatchException:无法将类型'java.lang.String'的值转换为必需类型'java.lang.Double'; 嵌套异常是java.lang.NumberFormatException:对于输入字符串:"$ {item.priceFactor}"
有没有办法使用来自属性文件的Double值?
java spring spring-el spring-properties property-placeholder
这是我正在迭代的代码:
<tr th:each="category : ${categories}">
<td th:text="${category.idCategory}"></td>
<td th:text="${category.name}"></td>
<td>
<a th:href="@{'/category/edit/' + ${category.id}}">view</a>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
它指向的URL应该是 /category/edit/<id of the category>
它说无法解析表达式:
<tr th:each="category : ${categories}">
<td th:text="${category.idCategory}"></td>
<td th:text="${category.name}"></td>
<td>
<a th:href="@{'/category/edit/' + ${category.id}}">view</a>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud) 如果属性和属性存在,是否有一种简单的方法在百万富翁中显示属性属性的内容?如果我的html页面中存在属性"summary"的属性"error",我想展示它:
<span th:text="${error.summary}">error summary</span>
Run Code Online (Sandbox Code Playgroud)
如果没有属性"error",则会引发以下错误:
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'summary' cannot be found on null
目前我正在使用以下方法,这似乎太复杂了.
<span th:if="${error != null and error.summary != null}"><span th:text="${error.summary}">error summary</span></span>
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来实现这一目标?
我正在使用带弹簧的Thymeleaf模板引擎,我想显示通过多行textarea存储的文本.
在我的数据库中,多行字符串存储为"\n",如下所示:"Test1 \nTest2 \n ......"
用th:text得到了:"Test1 Test2"没有换行符.
如何使用Thymeleaf显示换行符并避免手动"\n"替换为<br />然后避免使用th:utext(这种开放形式到xss注入)?
谢谢 !
spring-el ×10
spring ×9
java ×4
thymeleaf ×2
spring-boot ×1
spring-mvc ×1
xml ×1
xpath ×1
xquery ×1
xslt ×1