开发者

Accessing a Javabean property using <s:property>

开发者 https://www.devze.com 2023-03-13 21:06 出处:网络
I have a JSP and I want to fill some fields with information from my Action class. In my Action class I have a PersonDTO object.

I have a JSP and I want to fill some fields with information from my Action class. In my Action class I have a PersonDTO object. It's some example code only to get a picture of the design

int id = 4;
Person result = findMyRowFromDb(id);    
PersonDTO personDTO = new PersonDTO(result);
return "fillForm"

So in my JSP I want to get the values from my object. I write something like

<input type="text" name="PersonName" id="PersonName"
    value='<s:property value="personDTO.name开发者_运维知识库"/>'/>

name is a field of my PersonDTO object. But nothing is displayed. Can anyone help me?


Yes as doctrey mentioned, to display the <s:property value="personDTO.name"/> value, personDTO have to be an instance variable of your action class and also in PersonDTO class there should be a getter method for the name attribute. If so you will see your result.

And also you don't have to use <s:property> here, you can use <s:textfield> tag like following,

<s:textfield id="PersonName" name="personDTO.name"/>


As Steven and nmc said for every variable you want to access on your page you need to expose it with a getter method like getPersonDTO() in your case.
But assuming your code is in execute method the personDTO is a local variable and cannot be returned by a getter. So first you have to declare it as instance variable then provide a getter method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号