• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts2 - How to declare name/scope for a Bean

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. Im new into struts2, and i would like to know how I can declare a Bean.

I need this because i wont save my data/object (like the Bean) into a Map/Session object. It's annoying, and working with Struts (which is MVC) i prefeer manage Beans.

So, after, i think access to the attribute by using (for example) <s:property value="bean.attributeName" />

How can i do this?

Cheers and thanks
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Define your bean as an attribute with getter/setter methods in the action going to the page for property tags. If it is for a form element you will want to define the attribute in the action going to the page and the action processing the submission of the page. When OGNL sees the property referencing fooBean it will look for a reference to it and find it in the action with the getFooBean() method.

Action

private FooBean fooBean;

public FooBean getFooBean()
{
return fooBean;
}

JSP
<s:property value="fooBean.method" escape="false"/>

I hope this helps.

Pete
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want to do that. I think you should try/have a look "ModelDriven" interface.
 
Peter Searls
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In our case we decided not to use ModelDriven because we have situations where an action has the ability to transfer control to more than one jsp. In that case 'getModel()' wouldn't work very well if you have a different bean for each page.

It was just a matter of choice, but you are right that ModelDriven is a good alternative.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic