You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by albert kao <al...@gmail.com> on 2018/03/02 12:31:43 UTC
Jsp fields are null at startup
My jsp fields username and password are null at startup (jsp page is
loaded).
Why?
That make the "Invalid Username/Password. Please try again." message appear
when the page is loaded.
How to fix that?
Login.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application | ViralPatel.net</title>
</head>
<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>
LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String authenticate() {
return execute();
}
public String execute() {
if ((username != null) &&
(password != null) &&
this.username.equals("admin")
&& this.password.equals("admin123")) {
return "success";
} else {
addActionError(getText("error.login"));
return "error";
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Re: Jsp fields are null at startup
Posted by Lukasz Lenart <lu...@apache.org>.
2018-03-02 13:31 GMT+01:00 albert kao <al...@gmail.com>:
> My jsp fields username and password are null at startup (jsp page is
> loaded).
> Why?
> That make the "Invalid Username/Password. Please try again." message appear
> when the page is loaded.
> How to fix that?
Use two methods:
- execute() to display a form
- authenticate() to perform validation and login action
You must understand that flow in Struts is as follow:
request -> action -> jsp
so even opening a page, the execute() method is called
Regards
--
Ćukasz
+ 48 606 323 122 http://www.lenart.org.pl/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org