---
title: "struts2学习笔记2--正确使用OGNL表达式"
author: "fxk2006"
date: "2012-12-12T10:08:54Z"
tags: []
categories: ["互联网"]
slug: "struts2-2-ognl"
draft: false
lastmod: "2012-12-12T10:08:54Z"
id: 1746295
url: "https://fxk2006.iteye.com/blog/1746295"
---

原文来自：http://struts.apache.org/2.x/docs/tag-syntax.html

 

常见错误写法：

 

```
<s:textfield key="state.label" name="state" value="ca"/>
```

 

 原因：在给textfield传入值的时候，struts会先去查找一个叫getCa的属性，这不是我们的意图，我们想做的是直接把ca当作一个字符串，填入textfield中。

 

解决办法：

可以用下面的写法，一个不推荐的写法：

 

```
<s:textfield key="state.label" name="state" value="'ca'" />
```

 

 

推荐的写法（用OGNL表达式）：

```
<s:textfield key="state.label" name="state" value="%{'ca'}" />
```