为了确保这一点,我创建了这个测试操作:@Action (value = "dummy",结果 = {@Result(name="ok", type="httpheader", params={"status", "200"}) })@ParentPackage("默认")公共类 DummyAction {私人字符串 xTrace;公共字符串执行(){System.out.println( xTrace );返回确定";}公共字符串 getxTrace() {返回 xTrace;}公共无效 setxTrace(字符串 xTrace){this.xTrace = xTrace;}}我正在调用这个网址:localhost:8580/sagitarii/dummy?xTrace=thisisatest输出为 NULL,但是当我将 xTrace 更改为 xyTrace(以及 get、set 和 url)时,一切正常.我怎样才能使它起作用?* 编辑 *我已经尝试过使用这种格式的任何单词:iPad、iMac、iPhone、电子邮件……我认为这可能只是我的配置,但请在发布答案之前尝试一下. 解决方案 但是Struts2不能接受这种驼峰风格(只有第一个字符小写第二个大写)这实际上不是真的.Struts2 可以接收任何符合 JavaBeans 规范的变量名.但除了this规范的Java实现.(如果您想了解有关 JavaBeans 的更多信息,请参阅这篇文章JavaBean 究竟是什么?),它使用自己的实现.因为 Struts2 使用 OGNL 来访问 bean 的属性,所以根据 OGNL 语言,您应该知道什么是属性".什么是属性?粗略地说,OGNL 属性与 bean 属性相同,这意味着一对 get/set 方法或字段定义了一个属性(整个故事有点复杂,因为不同类型的对象的属性不同; 完整解释见下文).您可以在此处找到完整说明:参考属性.我不会深入研究OGNL如何处理对象的属性,但它应该遵循PropertyAccessor.不同类型的属性"有很多实现.OGNL 用于对象的一种是ObjectPropertyAccessor.这是它的作用的简单描述:PropertyAccessor 的实现,它使用对目标对象的类的反射来查找具有给定属性名称的字段或一对 set/get 方法.你可以通过浏览源代码找到它是如何做到的.我只想说它在使用 get/set 方法前缀之前将属性名称的第一个字母大写.所以,你问了我怎样才能使它起作用?更改属性的getter/setter的方法签名public String getXTrace() {返回 xTrace;}公共无效 setXTrace(字符串 xTrace){this.xTrace = xTrace;}更新:自 OGNL 3.0.11 起,单个小写字母不再大写.I'm using Datatables server side ajax pagination and need to pass some variables to server.My server is running Struts2 actions to handle this datatables requests.I'm facing some problems because datatables is passing predefined internal variables (like iDisplayStart, iDisplayLength, iColumns, sSearch), but Struts2 cannot receive this kind of camelcase style (just first one char lower and second upper case).To ensure this, I created this test action:@Action (value = "dummy", results = { @Result(name="ok", type="httpheader", params={"status", "200"}) } ) @ParentPackage("default")public class DummyAction { private String xTrace; public String execute () { System.out.println( xTrace ); return "ok"; } public String getxTrace() { return xTrace; } public void setxTrace(String xTrace) { this.xTrace = xTrace; }}I'm calling this URL:localhost:8580/sagitarii/dummy?xTrace=thisisatestThe output is NULL, but when I change xTrace to xyTrace (and get, set and url too) all goes fine.How can I make this to work?* EDIT *I already tried with any word with this format: iPad, iMac, iPhone, eMail, ...I think this could be just my configuration, but please give a try before post answers. 解决方案 but Struts2 cannot receive this kind of camelcase style (just first one char lower and second upper case)This is actually not true. Struts2 can receive any variable name that comply JavaBeans spec. But apart from the Java implementation of this spec. (if you want to learn more about JavaBeans, see this post What is a JavaBean exactly?), it's using its own implementation. Because Struts2 uses OGNL to access bean's properties you should know what a "property" is according OGNL language. What is a property? Roughly, an OGNL property is the same as a bean property, which means that a pair of get/set methods, or alternatively a field, defines a property (the full story is a bit more complicated, since properties differ for different kinds of objects; see below for a full explanation).And the full explanation you can find here: Referring to Properties.I will not dig into details how OGNL treats properties of the object but it should follow the contract for PropertyAccessor. There's a lot of implementations for different kind of "properties". The one that OGNL uses for objects is ObjectPropertyAccessor. Here's a merely description what it does: Implementation of PropertyAccessor that uses reflection on the target object's class to find a field or a pair of set/get methods with the given property name.How it's doing it you can find via browsing the source code. I'll just say that it's capitalizing the the first letter of the property name before using it with get/set method prefix. So, you have asked How can I make this to work?Change the method signature of the getter/setter for properties public String getXTrace() { return xTrace;}public void setXTrace(String xTrace) { this.xTrace = xTrace;}UPDATE:Since OGNL 3.0.11 the single lowercase letter is not uppercased. 这篇关于Struts2 传递变量案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 11:00