• 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

Passing params from JSP to Action

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!

First of all, I'd like to congratulate to the people that has developed this web. It's very usefull for guys like me, inexperienced on Struts 2.

My question:

I have a JSP with the following code:

...........................
<s:form action="addpiece.action" method="post">
<s:textfield name="idPiece" label="ID Piece" size="10" />
<s:param name="username" value="%{#user}" />
<s:submit method="execute" value="Add Piece" />
</s:form>
...........................

This piece of code, allows the user to enter a value into "idPiece". This value will be catched correctly on the Action file "AddPieceAction.java" (where 'idPiece' and 'username' are correctly defined, with their get/set methods).
The trouble is with the second parameter: "username". I have into the JSP a variable called "user", whose value I want to pass as a parameter to the Action, but I don't get this work...

I've been looking on different forums, and I have found out that for pass from a JSP to an Action file the content of a defined variable, I have to use the characters %{#varName}, like in the code shown before.
But this does not work...

Anyone could help me on this?

Thanks in advance!

 
Ranch Hand
Posts: 94
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you need to use %{#user}. If user is property of your action class(i.e. declared in Action class with get/set methods) you can directly access it from value stack value="user"
or as struts 2 doc has stated

The HTTP protocol is text-based, but some tags have non-String attribute types, like bool or int. To make using non-String attributes intuitative, the framework evaulates all non-String attributes as an expression. In this case, you do not need to use the escape notation. (But, if you do anyway , the framework will just strip it off

you could use this value="%{user}". - link to docs
 
Daniel Rosa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick response!!

But what I'm trying to do is to send data from the JSP to the Action. I mean, the JSP code is running, and there is a form wiht fields that must be filled (in the code, idPiece).
Once the user has pressed the button (on the JSP file) with the label "Add Piece", another Action file will be executed (defined in struts.xml).

What I want to do is send TO THE ACTION FILE FROM THE JSP, the value of the textfield idPiece (it works), and the value of the param username (instead of introduce the user any data, I want to send to the action file the content of the JSP local variable user).

When the action file executes, I can see the value of "idPiece", but I'm not able to get the value of the JSP local variable "user".

I hope to have expressed the trouble in a better way.

Thanks in advance!!
 
Daniel Rosa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention that "user" var is a String.

And I have tried too to get the data using "%{user}" too, without success.

Thanks in advance!
 
Yogesh Lonkar
Ranch Hand
Posts: 94
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh.. then use <s:hidden name="username" value="%{user}"/> or <s:hidden name="username" value="user"/> in your instead of <s:param >
 
Daniel Rosa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer.

I finally made this works fine!

The trouble it was on the name of the variable on the Action file. Must be the same that on the JSP. I mean:


THIS DOESN'T WORK!!!
===============
Action file:
private String username;

JSP file:
String user = "John";
.......
<s:hidden name="username" value="%{user}"/>



THIS IT WORKS!!!
============
Action file:
private String username;

JSP file:
String username = "John";
.......
<s:hidden name="username" value="%{username}"/>


Thanks!



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic