简体   繁体   中英

Recieving 'null' values from a jsp page into Struts2 Action

I'm using Struts2+JSP as my J2EE platforms. Sometimes I get null values from struts text fields of my jsp page into 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. 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:

<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:

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
and also try setting empty value in your username textbox declaration like <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. Try to add the getter and setters to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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