wood burning stoves 2.0*
The moose likes Struts and the fly likes struts 2: how can i pass a Vector as a parameter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts 2: how can i pass a Vector as a parameter?" Watch "struts 2: how can i pass a Vector as a parameter?" New topic
Author

struts 2: how can i pass a Vector as a parameter?

hans meiser
Greenhorn

Joined: Dec 29, 2008
Posts: 22
Good morning!

i have a JSP Page, on which i iterate over a vector, to display some values:

<s:form action="Employee_Add_Buzzword" method="POST">
<s:iterator value="buzzwordVector">
<s:property value="buzzword"/>
</s:iterator>
s:textfield label="Schlagwort" name="buzzword" size="60"/>

<s:submit value="Hinzuf├╝gen" cssClass="InputButton"/>


</s:form>

there is also a textfield, and a submit button, where i would like to add new words to this vector, and reload the page.

My action class looks like this:





public class EmployeeCreateBuzzwordAction extends ActionSupport implements SessionAware, ValidationAware
{
// braucht es bei jeder action zur identifikation
private static final long serialVersionUID = 1L;
private Map session;
private Exception ex = null;

private String categoryDescription = null;
private ArrayList categories = null;
private String hintText = null;
private String buzzword = null;
private String buzzwordText = null;
private Vector<String> buzzwordVector = null;

public String addBuzzword()
{
try
{
if(buzzwordVector == null)
{
buzzwordVector = new Vector<String>();
}
buzzwordVector.add(buzzword);
DataLoadUnit dataLoadUnit = new DataLoadUnit();
categories = dataLoadUnit.LoadCategories();
return Action.SUCCESS;

} catch (Exception e)
{
ex = e;
return Action.ERROR;
}
}

My problem is, that in my action class the buzzwordVector has always the value "null". This is ok for the first time when i add a new Word, but after that, it should be the same vector as before.

Hope someone can help me!

THank you
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hi,

just get your text filed value using

String newValue = request.getParameter("buzzword");

for that you have to implement your class to ServletRequestAware for that add in your class


public class EmployeeCreateBuzzwordAction extends ActionSupport implements SessionAware, ValidationAware,ServletRequestAware
{
private HttpServletRequest request;

public void setServletRequest(HttpServletRequest request) {
this.request = request;
}


and use String newValue = request.getParameter("buzzword"); to your action method

and use buzzwordVector.add(newValue);


}



THis will solve your problem.




Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's absolutely no reason to tie yourself to the servlet specification to accomplish this. Even if you wanted to bypass the normal Struts 2 mechanisms and use the request map you could implement RequestAware and access the request parameter map directly and have an easier-to-test class.

That aside, do you have a buzzwordVector getter and setter in your action class?
hans meiser
Greenhorn

Joined: Dec 29, 2008
Posts: 22
yes i have the getter and setter in my class. And in the other classes it works perfect.. but here it doesnt... any idea?

Maybe it is a problem, that i have the pageLoad() Method and the addBuzzword() Method in the same action?

Here is the whole ActionClass again:







hans meiser
Greenhorn

Joined: Dec 29, 2008
Posts: 22
please someone help me...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please post your code inside code tags so it's legible.
hans meiser
Greenhorn

Joined: Dec 29, 2008
Posts: 22
Ok sorry, now the code is in the right Tags!

I m still stuck with this problem.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't think there's any reason buzzwordVector would get set with this code, although without the action configuration it's hard to say. If the form submits to the addBuzzword method you'll get a vector of one buzzword--actions are instantiated for each request.

One way to do this might be to just have a List<String> buzzwords, include hidden form fields using array notation, and the action method would just append the new buzzword to the end of the buzzword list. Less code, and uses the framework's type conversion to good advantage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts 2: how can i pass a Vector as a parameter?