3

I am working on a struts2 project. I have created url with in my project and have passed parameters using tags. My question is how do i read the parameter in the actions? also if do the same would i be able to see the parameters as query string. i ask because i am not able to and i saw it in one of the tutorials.

SonOfTheEARTh
  • 1,858
  • 5
  • 21
  • 23

2 Answers2

5

Typically, you will interact with parameters in your actions by using fields on your actions, exposed by setters. Assume the following URL maps to my example Struts2 action:

URL

http://localhost/myAction?firstName=SonOfTheEARTh

Action Code

public class MyAction extends ActionSupport {
    private String firstName;

    public String execute() throws Exception {
        // do something here
        return SUCCESS;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }
}

JSP

Using Struts tags: <s:property value="firstName"/>

Using JSP EL/JSTL: ${action.firstName}

Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
  • does that mean we can send objects(other than String objects) as parameters. Also is it a good practice. also should the parameter be visible with url. because in mycase the url does not show the parameters – SonOfTheEARTh Dec 19 '10 at 05:10
  • Yes, you can use other types of objects other than String. If it is a custom type, you'll need to create a Struts2 type converter for it. – Steven Benitez Dec 19 '10 at 05:18
0

EDITED answer: It's based on naming conventions of your parameter. Take a look at this link and follow how they set "oldName" parameter.

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
  • i am working with struts2. i forgot to mention that. by url tag i meant . to which you can pass parameters but i am not knowing how to retrieve them. – SonOfTheEARTh Dec 18 '10 at 13:20