How to validate a form just if user has submitted something (with Struts2)
I'm developing a very simple Struts2 aplication, and I get this strange (and easy to resolve, I'm sure) problem:
When I access directoly to login url, it shows login form (username and password text fields) with these errors:
Name field is required
Password is required
The field are empty, because it'开发者_Python百科s the first time the user get into it, so I'd like to tell to Struts2 validation system not to validate unless user submit the login form with username and password. I'm sure I'm overlooking or forgeting something.
Login.java action code:
package actions.access;
import actions.base.BaseAction;
import data.Webuser;
public class Login extends BaseAction {
private Webuser user;
public String execute() {
if (user != null) {
session = this.getSession();
if (session.get("user-logged") == null ) {
session.put("user-logged", user.getId());
}
}
return SUCCESS;
}
public void validate() {
if (user != null) {
Webuser realUser = services.getWebuserByNickname(user.getNickname());
if (realUser.getPass().equals(user.getPass())) {
user.setId(realUser.getId());
} else {
addFieldError("user.nickname", "Nickname or password are wrong");
}
}
}
public Webuser getUser() {
return user;
}
public void setUser(Webuser user) {
this.user = user;
}
}
Login-validation.xml code:
<field name="user.nickname">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Login name is required</message>
</field-validator>
</field>
<field name="user.pass">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Password is required</message>
</field-validator>
</field>
Any kind of help will be appreciated. Thanks in advance.
What you are looking for is to use the same action to show a form and then (after submit) process the form.
public class LoginAction {
@SkipValidation
public String execute() throws Exception {
return INPUT; // shows the form
}
public void validate() {
// do your validations here...
}
public String submit() throws Exception {
// process the form
// redirect somewhere
}
}
Your form should submit to "login!submit" (that's Dynamic Method Invocation, which invokes your submit()
method). You don't need to use two actions for this.
you need two actions.
Your Login Action which redirect to your login.jsp.
A LoginSubmit Action which handle the login proccess.
for the second one you can define your validation.
You require one action which redirect to login.jsp and another action which handles Login process and validation. Inside first action do not write anything just set result=success.
public class IndexAction extends ActionSupport
{
public String execute() throws Exception
{
return SUCCESS;
}
}
Entries in Struts.xml are like follows:
<action name="login" class="com.test.web.LoginAction">
<result name="input">/pages/login.jsp</result>
<result name="success" >/pages/welcome.jsp</result>
</action>
<action name="index" class="com.test.web.IndexAction">
<result name="success">/pages/login.jsp</result>
</action>
One way to avoid this issue is, in your web.xml file have an entry for welcome file list. This should point to your login page. As soon you start your application this login page will open with out doing any kind of action and validation.
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
After starting your server, you can access your application by giving url in below format
"http://localhost:8080/ApplicationContext"
Here 8080 - is default HTTP port, it can be different in your case.
ApplicationContext- is the context of your application.
I think other thing should work fine.
精彩评论