这是我的动作类:
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页面我将params发送到这样的动作类:
<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>
我错过了什么?
答案 0 :(得分:1)
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
方法即可。因为它读取方法。不是变量名。
答案 1 :(得分:1)
我认为问题在于使用setter / getter方法。
你可以尝试一下。
public String getJ_username() {
return j_username;
}
public void setJ_username( String j_username ) {
this.j_username = j_username;
}
为避免此类错误,我建议您使用IDE帮助,而不是自己创建setter / getter方法。
如果您使用 Eclipse ,则可以按照以下步骤操作。