30083

Is there a way to bind OGNL with Struts2 UI tags

Question:

I'm developing a webapp using Struts2. I used to work with Struts1

is there a way to reference a session object directly to the JSP so that if I change the value in the formular, the value in the referenced session object will also be updated.

<s:textfield name="%{#session.order.amount}"/>

for e.g. I have a session object order which has attribute amount with getter and setter.

it seems like, after I put some values into the textfield and then submit the page, the value in the session didn't get updated.

currently I'm using another approach which involves session.put() inside the setter of an action attributes. Personally I dislike my current solution.

Answer1:

You could try having the name be the <em>string</em> session.order.amount and make your action implement SessionAware, and expose the session. Currently you're setting the name to the <em>value</em> of session.order.amount.

I don't know if it would work, and you might need to use array/collection notation, but off the top of my head I don't know why it <em>wouldn't</em> work.

That said, I feel like direct view-layer <em>writes</em> into web app internals is a Bad Idea.

Answer2:

From the Struts1 where form bean placed to the session scope by default or just by setting an attribute value scope="session" and everything was good.

In the Struts2 you have not form beans nor <em>session scoped</em> beans. And to use <em>session scoped</em> beans you need to either implement it yourself, or use other frameworks like Spring, Guice or CDI where <em>session scoped</em> is implemented and available to user.

On the other hand Struts2 is in heavy use of interceptors, which provide you additional features like <a href="http://struts.apache.org/release/2.3.x/docs/scope-interceptor.html" rel="nofollow">scope interceptor</a>, or <a href="http://struts.apache.org/release/2.3.x/docs/scoped-model-driven-interceptor.html" rel="nofollow">scopedModelDriven interceptor</a> that allows you to put some action properties to the session scope. It will initialize the properties each time the action is executed referencing objects are put on the session scope.

Without this helper interceptors you could always intercept a session object by implementing SessionAware (see <a href="https://struts.apache.org/docs/how-do-we-get-access-to-the-session.html" rel="nofollow">How do we get access to the session</a>) and initialize the properties somewhere when the action is executed, i.e. <a href="https://struts.apache.org/docs/prepare-interceptor.html" rel="nofollow">prepare()</a> method, because it's comming after the session map is injected. Putting initializer to the accessors is a bad idea.

Recommend

  • Why Custom Control is not visible in activity_main.xml
  • SQL case when set variable
  • Compile time checking of conditional compilation symbols in C# (see example)?
  • Why can I not initialize an array by passing a pointer to a function?
  • Dynamic Programming - top-down vs bottom-up
  • Regex: match only outside parenthesis (so that the text isn't split within parenthesis)?
  • Chinese and Japanese character encoding issues when exporting HTML to PDF
  • Reduce Mono runtime size
  • PXAction seemingly does nothing
  • Disable div click on Ajax start and re-enable it on Ajax complete
  • How to count amount of elements in a row of a matrix in C
  • Command line installation of Code Signing certificates, .p12 files, and mobileprovisions
  • jquery code not working without breakpoint
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • saving file generated by TCPDF
  • Django simple Captcha “No module named fields” error
  • Create DicomImage from scratch using Dcmtk
  • d3 v4 drag and drop with TypeScript
  • How to do unit test for HttpContext.Current.Server.MapPath
  • C# - Serializing and deserializing static member
  • Bug in WPF DataGrid
  • Excel - Autoshape get it's name from cell (value)
  • Incrementing object id automatically JS constructor (static method and variable)
  • Check if a string to interpolate provides expected placeholders
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Web-crawler for facebook in python
  • RestKit - RKRequestDelegate does not exist
  • AT Commands to Send SMS not working in Windows 8.1
  • Traverse Array and Display in markup
  • PHP: When would you need the self:: keyword?
  • A cron job substitute?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Acquiring multiple attributes from .xml file in c#
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • How can I use threading to 'tick' a timer to be accessed by other threads?