-->

How to access url parameters in struts2

2019-03-22 00:13发布

问题:

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.

回答1:

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}



回答2:

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