How can I pass the username on the login page to the login success page?
There are three methods,
1. Use the default action transfer method.
2. Customize a vo and use this vo in the action.
3. Use ModelDriven.
The following is a separate description.
1. Use the default action transfer method.
The action file is as follows:
Package struts2.login;
Public class LoginAction {
Private String username;
Private String password;
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;
}
Public String execute (){
System. out. println (LoginAction. class. hashCode ());
If (username. inclusignorecase ("aaa ")&&
Password. equals ("aaaaaa ")){
Return "loginSuc ";
}
Else {
Return "loginFail ";
}
}
}
The successful login file is as follows:
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Taglib uri = "/struts-tags" prefix = "s" %>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
Welcome, <s: property value = "username"/> login successful.
2. Customize a vo and use this vo in the action.
Custom vo file name: LoginVO. java
File Content:
Package struts2.login;
Public class LoginVO {
Private String username;
Private String password;
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;
}
}
In the Action file, use this vo
File Content:
Package struts2.login;
Public class LoginAction {
Private LoginVO user = null;
Public String execute (){
System. out. println (LoginAction. class. hashCode ());
If (user. getUsername (). inclusignorecase ("aaa ")&&
User. getPassword (). equals ("aaaaaa ")){
Return "loginSuc ";
}
Else {
Return "loginFail ";
}
}
Public LoginVO getUser (){
Return user;
}
Public void setUser (LoginVO user ){
This. user = user;
}
}
The successful login file is as follows: www.2cto.com
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Taglib uri = "/struts-tags" prefix = "s" %>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
Welcome, <s: property name = "user. username"> login successful.
Note that the login file must also be modified.
The file content is as follows:
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
<Title> login2 </title>
<Form action = "login. action" method = "post">
Username: <input type = "input" name = "user. username"> <br>
Password: <input type = "input" name = "user. password"> <br>
<Input type = "submit" value = "login">
</Form>
3. Use ModelDriven.
We also need a vo, which is consistent with method 2, but the action Statement is different.
The content of the action file is as follows:
Package struts2.login;
Import com. opensymphony. xwork2.ModelDriven;
Public class LoginAction implements ModelDriven <LoginVO> {
@ Override
Public LoginVO getModel (){
// TODO Auto-generated method stub
Return user;
}
Private LoginVO user = new LoginVO ();
Public String execute (){
System. out. println (LoginAction. class. hashCode ());
If (user. getUsername (). inclusignorecase ("aaa ")&&
User. getPassword (). equals ("aaaaaa ")){
Return "loginSuc ";
}
Else {
Return "loginFail ";
}
}
}
On the login success page, you do not need to append the user prefix to the login file, that is, the content of the file is the same as that of method 1.
Summary of the three methods:
The first method is to put the form values in the action file. When the form submits many projects, the action content will become a lot of bloated. It is available when there are few projects.
The second method puts the form value in vo separately, which solves the problem of bloated action file and separates form and action. However, it must be identified on the configured and retrieved jsp pages.
The third method is based on the second method. By implementing specific interfaces, the set and get methods in action are removed, and the jsp page identifier is removed. Make the logic of background programs clearer
The author's blog