• 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

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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please someone help me...
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your code inside code tags so it's legible.
 
hans meiser
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok sorry, now the code is in the right Tags!

I m still stuck with this problem.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic