简体   繁体   English

从jsp页面接收'null'值到Struts2 Action

[英]Recieving 'null' values from a jsp page into Struts2 Action

I'm using Struts2+JSP as my J2EE platforms. 我正在使用Struts2 + JSP作为我的J2EE平台。 Sometimes I get null values from struts text fields of my jsp page into actions. 有时我从我的jsp页面的struts文本字段中获取null值为actions。 for example in the login page, despite of validating my forms and preventing of entering invalid data such as null or blank, I still get null value for the username field instead of the actual entered value. 例如,在登录页面中,尽管验证了我的表单并且阻止输入无效数据(例如null或空白),我仍然会获得用户名字段的空值而不是实际输入的值。 has anyone come across such an occasion? 有没有人遇到这样的场合?

here is a piece of code in which this error happens: 这是一段发生此错误的代码:

public class LoginAction extends ActionSupport {

private static final long serialVersionUID = 1L;
private String username;
private String password;

@SuppressWarnings({ "unchecked", "deprecation", "static-access" })
public String execute() {

    try {
        CacheLogger.getInstance().AddEnteredUsernameEvent(username);
        UserSession userSession = UserSession.getInstance();

        userSession.getPlayerById(username.toLowerCase()); //THE ONE WHERE I GET NULL VALUE (sometimes)

the respective JSP page: 相应的JSP页面:

<s:form action="login.action" method="post" onsubmit="document.getElementsByName('password').item(0).value=sha1Hash(document.getElementsByName('password').item(0).value);return validateLogin()" style="text-align:center;">
<s:textfield name="username" maxlength="50" label="نام کاربری" tabindex="1" onfocus="hideMessage()" cssClass="loginTextField" />
<s:password  name="password" maxlength="255" label="رمز عبور" tabindex="2" onfocus="hideMessage()" cssClass="loginTextField"/>
<input type="submit" name="Submit" value="ورود" class="submit" tabindex=3 onclick="javascript:$('#exception').css('display','none');$('#actionmessage').css('display','none');"/>
</s:form>

the piece of javaScript Validator: 一段javaScript验证器:

function validateLogin()
{   
    var username = document.getElementsByName('username').item(0).value;
    var password = document.getElementsByName('password').item(0).value;
    var input=/^[a-zA-Z]+[a-zA-Z0-9]+[a-zA-Z0-9]+\d*[a-zA-Z0-9]*$/;

    if(!username)
    {
        showErrorMessage('خطا! نام کاربری را وارد نکرده اید',26);
        return false;
    }
    else
    if(!password)
    {
        showErrorMessage('خطا! رمز عبور را وارد نکرده اید',25);
        return false;
    }
    else
    if(!input.test(username))
    {
        showErrorMessage('خطا! در قسمت "معین خر است" کاراکتر غیر مجاز وارد کرده اید',49);
        return false;
    }
    return true;
}

There may be multiple emements having name usename in your jsp page instead of using document.getElementsByName for getting text box value try to get using form reference document.formid.username.value 在jsp页面中可能有多个emements具有name usename,而不是使用document.getElementsByName获取文本框值,尝试使用表单引用document.formid.username.value
and also try setting empty value in your username textbox declaration like <s:textfield .... value="" /> 并尝试在用户名文本框声明中设置空值,如<s:textfield .... value="" />

In the LoginAction class, you will need to have the getter and setter method for the attributes you want to receive from the jsp page. 在LoginAction类中,您需要为要从jsp页面接收的属性使用getter和setter方法。 Try to add the getter and setters to it. 尝试添加getter和setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM