java struts2中如何访问url参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4478147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:34:10  来源:igfitidea点击:

How to access url parameters in struts2

javastruts2http-request-parameters

提问by SonOfTheEARTh

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.

我正在做一个 struts2 项目。我在我的项目中创建了 url 并使用标签传递了参数。我的问题是如何读取动作中的参数?此外,如果这样做,我是否能够将参数视为查询字符串。我问是因为我不能,我在其中一个教程中看到了它。

回答by Steven Benitez

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:

通常,您将通过在操作上使用由 setter 公开的字段来与操作中的参数进行交互。假设以下 URL 映射到我的示例 Struts2 操作:

URL

网址

http://localhost/myAction?firstName=SonOfTheEARTh

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

JSP

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

使用 Struts 标签: <s:property value="firstName"/>

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

使用 JSP EL/JSTL: ${action.firstName}

回答by CoolBeans

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

编辑的答案:它基于参数的命名约定。看看这个链接并按照他们如何设置“oldName”参数。