public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private MessageStore mStore;
public HelloWorldAction(){
mStore = new MessageStore();
}
@Override
public String execute() throws Exception {
System.out.println("executing...");
mStore.setMessage("asdasdasd");
return SUCCESS;
}
public MessageStore getMStore() {
return mStore;
}
public void setMStore(MessageStore mStore) {
this.mStore = mStore;
}
}
上面的Action类有一个MessageStore对象mStore,在JSP页面用struts标签取值语句如下:
<s:property value="mStore.message"/>
若如上Action类写的get、set方法就能正常渠道对象的属性值,但是用Eclipse自动生成的getmStore()、setmStore()方法则不符合struts2的取值规则,必须协成getMStore()、setMStore()格式才能正常取值,因为
struts标签获取action属性值是通过getXXX方法。根据javabean的命名规则,属性名的第一个单词(注意哦问题就出现在这里 谁让你的第一个单词就一个字母了)的第一个字母大写 你的属性pId用Eclipse自动生成的方法为 getpId()和setpId;根据get方法的解析规则,指挥找get紧挨则的大些字母以后还原成属性(当然然还原不回去了,get后面的是小写嘛)。
本文深入探讨了在使用Struts2框架时,Action类中属性获取规则与命名规范的重要性,特别关注了get、set方法的使用以及属性名称的命名规则,帮助开发者正确获取Action类属性。
223

被折叠的 条评论
为什么被折叠?



