这是我的 Action 类:
public class ForgotPassword extends ActionSupport{
private String j_username;
public String execute() throws Exception {
System.out.println("FORGOT PASSWORD ACTION CALLED");
return SUCCESS;
}
public String getUsername() {
return j_username;
}
public void setUsername(String j_username) {
System.out.println("Username received is " + j_username);
this.j_username = j_username;
}
}
我在 struts.xml 中声明了默认拦截器,其中包括:
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
从 html 页面我将参数发送到操作类,如下所示:
<body>
<input type="text" id="j_username" name="j_username"/>
<div style="margin-top:20px">
<a class="forgot_password"
onclick="forgotPassword()" id="resetAnchor">Forgot Password</a>
</div>
<script type="text/javascript">
function forgotPassword() {
var ctx = '${pageContext.request.contextPath}';
var username = document.getElementById('j_username').value;
if (username !== null && username !== "") {
var username = document.getElementById('j_username').value;
console.log('username is ', username);
document.getElementById('resetAnchor').href = ctx + "/forgotPassword?j_username=" + username;
} else {
alert('please provider username');
}
}
</script>
</body>
一切看起来都很好,但我无法在 setter 方法中打印这一行:
System.out.println("Username received is " + j_username);
它只打印这一行:System.out.println("FORGOT PASSWORD ACTION CALLED");
以下是 struts.xml 中定义的操作:
<action name="forgotPassword" class="com.actions.ForgotPassword">
<interceptor-ref name="params"/>
<result name="success">/reset.jsp</result>
</action>
我错过了什么?
最佳答案
public String getJ_username() {
return j_username;
}
public void setJ_username(String j_username) {
System.out.println("Username received is " + j_username);
this.j_username = j_username;
}
只需更改您的 setter
和 getter
方法即可。因为它读取了方法。不是变量名。
关于java - 无法在 struts 2 操作类中获取查询字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738295/