如何在 Struts 中的动作 class 的准备方法中获取 URL 参数 2
How to get URL parameter in prepare method of action class in Struts 2
我在 Preparable
接口的准备方法中得到 null
作为 URL 参数名称,而参数在 URL.
中设置
URL我正在尝试访问,
http://localhost:8080/basic-struts/registerInput.action?userid=1
public class Register extends ActionSupport implements Preparable {
private String userid;
public void prepare() throws Exception {
// Call the service, load data,
// every time even if validation fails
System.out.println("----------");
System.out.println(userid); //<-------null
System.out.println("----------");
}
public String getUserId() {
return userid;
}
public void setUserId(String userid) {
this.userid = userid;
}
}
注意:- 我在日志中收到如下错误,
2021-07-01 09:28:53,929 ERROR [qtp1182469998-22] interceptor.ParametersInterceptor (ParametersInterceptor.java:238) - Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'userid' on 'class org.apache.struts.register.action.Register: Error setting expression 'userid' with value ['1', ]
将参数 userid
设置为操作 class 变量区分大小写。对应的setter方法应该是
public void setUserid(String userid) {
this.userid = userid;
}
@Dave Newton 的建议很有帮助,在他分享的 link 上,我得到了线索并开始运作,
我更改了我的 struts.xml 并添加了拦截器 paramsPrepareParamsStack
到我的操作中,如下所示,
<action name="registerInput" class="org.apache.struts.register.action.Register" method="input" >
<result name="input">/register.jsp</result>
<interceptor-ref name="paramsPrepareParamsStack"/>
</action>
我在 Preparable
接口的准备方法中得到 null
作为 URL 参数名称,而参数在 URL.
URL我正在尝试访问,
http://localhost:8080/basic-struts/registerInput.action?userid=1
public class Register extends ActionSupport implements Preparable {
private String userid;
public void prepare() throws Exception {
// Call the service, load data,
// every time even if validation fails
System.out.println("----------");
System.out.println(userid); //<-------null
System.out.println("----------");
}
public String getUserId() {
return userid;
}
public void setUserId(String userid) {
this.userid = userid;
}
}
注意:- 我在日志中收到如下错误,
2021-07-01 09:28:53,929 ERROR [qtp1182469998-22] interceptor.ParametersInterceptor (ParametersInterceptor.java:238) - Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'userid' on 'class org.apache.struts.register.action.Register: Error setting expression 'userid' with value ['1', ]
将参数 userid
设置为操作 class 变量区分大小写。对应的setter方法应该是
public void setUserid(String userid) {
this.userid = userid;
}
@Dave Newton 的建议很有帮助,在他分享的 link 上,我得到了线索并开始运作,
我更改了我的 struts.xml 并添加了拦截器 paramsPrepareParamsStack
到我的操作中,如下所示,
<action name="registerInput" class="org.apache.struts.register.action.Register" method="input" >
<result name="input">/register.jsp</result>
<interceptor-ref name="paramsPrepareParamsStack"/>
</action>