wood burning stoves 2.0*
The moose likes Struts and the fly likes Passing params from JSP to Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing params from JSP to Action" Watch "Passing params from JSP to Action" New topic
Author

Passing params from JSP to Action

Daniel Rosa
Greenhorn

Joined: May 22, 2013
Posts: 7
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!

Yogesh Lonkar
Ranch Hand

Joined: Jul 17, 2012
Posts: 94

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


Learning some thing New Every Day
Daniel Rosa
Greenhorn

Joined: May 22, 2013
Posts: 7
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

Joined: May 22, 2013
Posts: 7
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

Joined: Jul 17, 2012
Posts: 94

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

Joined: May 22, 2013
Posts: 7
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!



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing params from JSP to Action