java - 无法在 struts 2 操作类中获取查询字符串参数

标签 java struts2

这是我的 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;
}

只需更改您的 settergetter 方法即可。因为它读取了方法。不是变量名。

关于java - 无法在 struts 2 操作类中获取查询字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738295/

相关文章:

java - JFace TreeViewer 在展开/折叠时保存树节点的状态

java - <s :form> to decide in the action class 中的两个按钮

java - ExtJS 成功操作未被调用

java - 异常启动过滤器 struts2 - 尝试添加 JAR,但结果相同

java - Struts 2 - HTTP 状态 500 - 找不到名称为 java.io.InputStream

java - 在 PRG 模式中如何在刷新成功页面上删除 ActionMessage

java - 匿名结果 :NaN

c# - 允许用户内联编写脚本,.net 或 java 有哪些内联脚本引擎?

java - 我应该如何对长函数进行单元测试?

java - 验证业务规则